From f2af093b7db9312bb7988e3529dcbddebd163963 Mon Sep 17 00:00:00 2001 From: tevenfeng Date: Wed, 30 Nov 2022 09:41:59 +0000 Subject: [PATCH] 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))