From 1935b05b978293e0669e436c9a5b0b587cab55d0 Mon Sep 17 00:00:00 2001
From: JJAYCHEN <31304335+JJAYCHEN1e@users.noreply.github.com>
Date: Sun, 20 Mar 2022 12:09:30 +0800
Subject: [PATCH 1/8] feat: Upgrade to Python 3.
---
alp/item.py | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/alp/item.py b/alp/item.py
index 724d051..cca47e1 100755
--- a/alp/item.py
+++ b/alp/item.py
@@ -62,12 +62,12 @@ def processItem(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)
- 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":
@@ -87,4 +87,4 @@ def processItem(item):
else:
processItem(items)
- print ET.tostring(feedback, encoding="utf-8")
+ print(ET.tostring(feedback, encoding="utf-8"))
From c5996e0f70420268df422f41339ebed12ec35eb7 Mon Sep 17 00:00:00 2001
From: JJAYCHEN <31304335+JJAYCHEN1e@users.noreply.github.com>
Date: Sun, 20 Mar 2022 13:57:43 +0800
Subject: [PATCH 2/8] Update info.plist
---
info.plist | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
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
From 42683e2d3918db412c2b0e48f3222e5a83bedd96 Mon Sep 17 00:00:00 2001
From: JJAYCHEN <31304335+JJAYCHEN1e@users.noreply.github.com>
Date: Sun, 20 Mar 2022 14:08:45 +0800
Subject: [PATCH 3/8] Update README.md
---
README.md | 2 ++
1 file changed, 2 insertions(+)
diff --git a/README.md b/README.md
index a5196d4..057d3c7 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,5 @@
+I've upgrade scripts to use `Python 3` because macOS 12.3 dropped `Python 2`.
+
# NSC
*Number System Converter -- an [Alfred](http://www.alfredapp.com/) extension*
* * *
From aab6eb45af8b54ce056d290960ffab7b3466545e Mon Sep 17 00:00:00 2001
From: JJAYCHEN <31304335+JJAYCHEN1e@users.noreply.github.com>
Date: Sun, 20 Mar 2022 14:09:21 +0800
Subject: [PATCH 4/8] Update README.md
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 057d3c7..ca84d3b 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-I've upgrade scripts to use `Python 3` because macOS 12.3 dropped `Python 2`.
+I've upgraded scripts to use `Python 3` because macOS 12.3 dropped `Python 2`. And I prefer to use a short version of commands like `dec` and `bin`.
# NSC
*Number System Converter -- an [Alfred](http://www.alfredapp.com/) extension*
From 1c4cd23afdd208215731e063f7b36c275a9ff10a Mon Sep 17 00:00:00 2001
From: JJAYCHEN <31304335+JJAYCHEN1e@users.noreply.github.com>
Date: Sun, 20 Mar 2022 14:09:49 +0800
Subject: [PATCH 5/8] Update README.md
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index ca84d3b..3d2e824 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-I've upgraded scripts to use `Python 3` because macOS 12.3 dropped `Python 2`. And I prefer to use a short version of commands like `dec` and `bin`.
+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`.
# NSC
*Number System Converter -- an [Alfred](http://www.alfredapp.com/) extension*
From 484e059544f0ebc91b3445c7b240ab408dbd4574 Mon Sep 17 00:00:00 2001
From: JJAYCHEN <31304335+JJAYCHEN1e@users.noreply.github.com>
Date: Sun, 20 Mar 2022 14:10:28 +0800
Subject: [PATCH 6/8] Update README.md
---
README.md | 2 ++
1 file changed, 2 insertions(+)
diff --git a/README.md b/README.md
index 3d2e824..99789bc 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +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*
* * *
From b0b6ddb37ba7240cde6f211e2613d0b944c6b26d Mon Sep 17 00:00:00 2001
From: JJAYCHEN <31304335+JJAYCHEN1e@users.noreply.github.com>
Date: Sun, 20 Mar 2022 16:14:12 +0800
Subject: [PATCH 7/8] fix: Upgrade to Python 3.
---
convertNumber.py | 139 +++++++++++++++++++++++++----------------------
1 file changed, 74 insertions(+), 65 deletions(-)
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)
From f2af093b7db9312bb7988e3529dcbddebd163963 Mon Sep 17 00:00:00 2001
From: tevenfeng
Date: Wed, 30 Nov 2022 09:41:59 +0000
Subject: [PATCH 8/8] drop plistlib.readPlist && feedback using json instead of
xml
---
alp/core.py | 11 ++++++-----
alp/item.py | 28 +++++++++++++++++-----------
2 files changed, 23 insertions(+), 16 deletions(-)
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 cca47e1..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"].items():
if v is None:
continue
- itemToAdd.set(k, v)
+ json_item[k] = v
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))