diff --git a/README.md b/README.md index a5196d4..99789bc 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +I've upgraded scripts to use `Python 3` because macOS 12.3 dropped `Python 2`. And I prefer to use a short version of command like `dec` and `bin` instead of `decimal` and `binary`. + +Original README: + # NSC *Number System Converter -- an [Alfred](http://www.alfredapp.com/) extension* * * * diff --git a/alp/core.py b/alp/core.py index 0902d26..ad4bd80 100755 --- a/alp/core.py +++ b/alp/core.py @@ -21,11 +21,12 @@ def bundle(): infoPath = os.path.abspath("./info.plist") if os.path.exists(infoPath): - info = plistlib.readPlist(infoPath) - try: - gBundleID = info["bundleid"] - except KeyError: - raise Exception("Bundle ID not defined or readable from info.plist.") + with open(infoPath, 'rb') as inputFile: + info = plistlib.loads(inputFile.read()) + try: + gBundleID = info["bundleid"] + except KeyError: + raise Exception("Bundle ID not defined or readable from info.plist.") else: raise Exception("info.plist missing.") diff --git a/alp/item.py b/alp/item.py index 724d051..da41e6d 100755 --- a/alp/item.py +++ b/alp/item.py @@ -1,9 +1,9 @@ # -*- coding: utf-8 -*- -from xml.etree import ElementTree as ET import copy import random import alp.core as core +import json class Item(object): @@ -55,36 +55,42 @@ def get(self): return data def feedback(items): - feedback = ET.Element("items") def processItem(item): - itemToAdd = ET.SubElement(feedback, "item") + json_item = {} data = item.get() - for (k, v) in data["attrib"].iteritems(): + for (k, v) in data["attrib"].items(): if v is None: continue - itemToAdd.set(k, v) + json_item[k] = v - for (k, v) in data["content"].iteritems(): + for (k, v) in data["content"].items(): if v is None: continue if k != "fileIcon" and k != "fileType": - child = ET.SubElement(itemToAdd, k) - child.text = v + json_item[k] = v if k == "icon": + icon_item = {} if "fileIcon" in data["content"].keys(): if data["content"]["fileIcon"] == True: - child.set("type", "fileicon") + icon_item["type"] = "fileicon" if "fileType" in data["content"].keys(): if data["content"]["fileType"] == True: - child.set("type", "filetype") + icon_item["type"] = "filetype" + return json_item + final_items = {} + items_array = [] if isinstance(items, list): for anItem in items: - processItem(anItem) + final_item = processItem(anItem) + items_array.append(final_item) else: - processItem(items) + final_item = processItem(items) + items_array.append(final_item) - print ET.tostring(feedback, encoding="utf-8") + final_items["items"] = items_array + + print(json.dumps(final_items)) diff --git a/convertNumber.py b/convertNumber.py index b431518..cac33e7 100644 --- a/convertNumber.py +++ b/convertNumber.py @@ -10,7 +10,8 @@ import sys import alp import string -ALPHA = string.digits + string.uppercase + string.lowercase + '+' + '/' +ALPHA = string.digits + string.ascii_letters + '+' + '/' + def base64_encode(num, base, alphabet=ALPHA): """Encode a number in Base X @@ -29,76 +30,84 @@ def base64_encode(num, base, alphabet=ALPHA): arr.reverse() return ''.join(arr) + def base64_decode(string, base, alphabet=ALPHA): - """Decode a Base X encoded string into the number + """Decode a Base X encoded string into the number - Arguments: - - `string`: The encoded string - - `base`: base of number - - `alphabet`: The alphabet to use for encoding - """ - strlen = len(string) - num = 0 + Arguments: + - `string`: The encoded string + - `base`: base of number + - `alphabet`: The alphabet to use for encoding + """ + strlen = len(string) + num = 0 - idx = 0 - for char in string: - power = (strlen - (idx + 1)) - num += alphabet.index(char) * (base ** power) - idx += 1 + idx = 0 + for char in string: + power = (strlen - (idx + 1)) + num += alphabet.index(char) * (base ** power) + idx += 1 - return num + return num if (len(sys.argv) == 4 and sys.argv[3] != "1"): - # calculate integer first - if (int(sys.argv[2]) <= 36): - # use built in python conversion if possible - decimal = int(sys.argv[1], int(sys.argv[2])) - elif (int(sys.argv[2]) > 36 and int(sys.argv[2]) <= 64): - # otherwise, use base64_decode - decimal = base64_decode(sys.argv[1], int(sys.argv[2])) - else: - # create dictionary to create xml from it - errorDic = dict(title="Ohoh, your number couldn't be converted", subtitle="make sure your base is between 2 and 64", uid="error", valid=False) - e = alp.Item(**errorDic) - alp.feedback(e) - sys.exit() - - # create dictionary to create xml from it - decimalDic = dict(title=str(decimal), subtitle="Decimal", uid="decimal", valid=True, arg=str(decimal), icon="icons/decimal.png") - d = alp.Item(**decimalDic) - - # calculate new number - if (int(sys.argv[3]) >= 2 and int(sys.argv[3]) <= 64): - conv = base64_encode(decimal, int(sys.argv[3])) - else: - # create dictionary to create xml from it - errorDic = dict(title="Ohoh, your number couldn't be converted", subtitle="make sure your base is between 2 and 64", uid="error", valid=False) - e = alp.Item(**errorDic) - itemsList = [d, e] - alp.feedback(itemsList) - sys.exit() - - # create dictionary to create xml from it - convertDic = dict(title=conv, subtitle="Number to base " + str(sys.argv[3]), uid="conv", valid=True, arg=conv) - c = alp.Item(**convertDic) - - if (int(sys.argv[2]) >= 36 or int(sys.argv[3]) >= 36): - # create dictionary to create xml from it - infoDic = dict(title="Case-Sensitive", subtitle="Be aware, if base is >= 36 letters are case-sensitive", uid="conv", valid=True, arg=conv) - i = alp.Item(**infoDic) - itemsList = [d, c, i] - else: - itemsList = [d, c] - - alp.feedback(itemsList) + # calculate integer first + if (int(sys.argv[2]) <= 36): + # use built in python conversion if possible + decimal = int(sys.argv[1], int(sys.argv[2])) + elif (int(sys.argv[2]) > 36 and int(sys.argv[2]) <= 64): + # otherwise, use base64_decode + decimal = base64_decode(sys.argv[1], int(sys.argv[2])) + else: + # create dictionary to create xml from it + errorDic = dict(title="Ohoh, your number couldn't be converted", + subtitle="make sure your base is between 2 and 64", uid="error", valid=False) + e = alp.Item(**errorDic) + alp.feedback(e) + sys.exit() + + # create dictionary to create xml from it + decimalDic = dict(title=str(decimal), subtitle="Decimal", uid="decimal", + valid=True, arg=str(decimal), icon="icons/decimal.png") + d = alp.Item(**decimalDic) + + # calculate new number + if (int(sys.argv[3]) >= 2 and int(sys.argv[3]) <= 64): + conv = base64_encode(decimal, int(sys.argv[3])) + else: + # create dictionary to create xml from it + errorDic = dict(title="Ohoh, your number couldn't be converted", + subtitle="make sure your base is between 2 and 64", uid="error", valid=False) + e = alp.Item(**errorDic) + itemsList = [d, e] + alp.feedback(itemsList) + sys.exit() + + # create dictionary to create xml from it + convertDic = dict(title=conv, subtitle="Number to base " + str(sys.argv[3]), uid="conv", valid=True, arg=conv) + c = alp.Item(**convertDic) + + if (int(sys.argv[2]) >= 36 or int(sys.argv[3]) >= 36): + # create dictionary to create xml from it + infoDic = dict( + title="Case-Sensitive", subtitle="Be aware, if base is >= 36 letters are case-sensitive", uid="conv", + valid=True, arg=conv) + i = alp.Item(**infoDic) + itemsList = [d, c, i] + else: + itemsList = [d, c] + + alp.feedback(itemsList) else: - if (len(sys.argv) != 4): - errorDic = dict(title="Make sure to pass 3 numbers", subtitle="convert `number` `base of original` `base to convert to`", uid="error", valid=False, arg="error") - error = alp.Item(**errorDic) - alp.feedback(error) - elif (int(sys.argv[3]) == 1): - errorDic = dict(title="Base 1 makes no sense", subtitle="", uid="error", valid=False, arg="error") - error = alp.Item(**errorDic) - alp.feedback(error) + if (len(sys.argv) != 4): + errorDic = dict( + title="Make sure to pass 3 numbers", subtitle="convert `number` `base of original` `base to convert to`", + uid="error", valid=False, arg="error") + error = alp.Item(**errorDic) + alp.feedback(error) + elif (int(sys.argv[3]) == 1): + errorDic = dict(title="Base 1 makes no sense", subtitle="", uid="error", valid=False, arg="error") + error = alp.Item(**errorDic) + alp.feedback(error) diff --git a/info.plist b/info.plist index 3ee66bc..961dfff 100644 --- a/info.plist +++ b/info.plist @@ -120,11 +120,11 @@ escaping 0 keyword - octal + oct runningsubtext converting ... script - python convertOctal.py {query} 2>&1 | tee nsc.log + python3 convertOctal.py {query} 2>&1 | tee nsc.log subtext Type any octal number title @@ -151,7 +151,7 @@ runningsubtext converting ... script - python convertNumber.py {query} 2>&1 | tee nsc.log + python3 convertNumber.py {query} 2>&1 | tee nsc.log subtext convert number to other system title @@ -174,11 +174,11 @@ escaping 0 keyword - binary + bin runningsubtext converting ... script - python convertBinary.py {query} 2>&1 | tee nsc.log + python3 convertBinary.py {query} 2>&1 | tee nsc.log subtext Type any binary number title @@ -205,7 +205,7 @@ runningsubtext converting ... script - python convertHex.py {query} 2>&1 | tee nsc.log + python3 convertHex.py {query} 2>&1 | tee nsc.log subtext Type any hexadecimal number title @@ -251,11 +251,11 @@ escaping 0 keyword - decimal + dec runningsubtext converting ... script - python convertDecimal.py {query} 2>&1 | tee nsc.log + python3 convertDecimal.py {query} 2>&1 | tee nsc.log subtext Type any decimal number title