From d209ff852ff153b067e975deda28cbdfa7d0dd41 Mon Sep 17 00:00:00 2001 From: Louie S Date: Sun, 2 Apr 2023 21:06:52 -0700 Subject: backend returns json to frontend --- .../endpoints/__pycache__/get_plist.cpython-39.pyc | Bin 0 -> 1814 bytes backend/endpoints/get_plist.py | 86 +++++++++++++-------- backend/server.py | 3 +- 3 files changed, 55 insertions(+), 34 deletions(-) create mode 100644 backend/endpoints/__pycache__/get_plist.cpython-39.pyc diff --git a/backend/endpoints/__pycache__/get_plist.cpython-39.pyc b/backend/endpoints/__pycache__/get_plist.cpython-39.pyc new file mode 100644 index 0000000..d70e425 Binary files /dev/null and b/backend/endpoints/__pycache__/get_plist.cpython-39.pyc differ diff --git a/backend/endpoints/get_plist.py b/backend/endpoints/get_plist.py index cfa56bf..6d2ca4c 100644 --- a/backend/endpoints/get_plist.py +++ b/backend/endpoints/get_plist.py @@ -7,36 +7,56 @@ import sys sys.path.append(os.path.join(os.path.dirname(__file__), "..")) from config import Config -output = dict() -output["paths"] = [] - -def find_plist(path): - with os.scandir(path) as it: - for entry in it: - if entry.name == "." or entry.name == ".." or entry.name == "Resources": - continue - if(os.DirEntry.is_dir(entry)): - find_plist(path + "/" + entry.name) - elif(entry.name == "Info.plist"): - #parse_plist(path + "/" + entry.name) - output["paths"].append(path + "/" + entry.name) - -def parse_plist(path): - with open(path, "rb") as fp: - pl = plistlib.load(fp) - print(pl["CFBundleIdentifier"]) - print(pl["CFBundleName"]) - - - -if not os.path.isdir(Config.docset_base): - e = "Invalid DOCSET_BASE \"" + Config.docset_base + "\"" - logging.error(e); - output["success"] = False - output["message"] = e - -else: - find_plist(Config.docset_base); - output["success"] = True - -print(json.dumps(output)) +class Get_Plist: + output = dict() + output["docsets"] = [] + + + def find_plist(path): + with os.scandir(path) as it: + for entry in it: + docset_root = os.path.join(path, entry.name) + plist_path = os.path.join(docset_root, "Contents", "Info.plist") + if(os.path.isfile(plist_path)): + if(Get_Plist.parse_plist(plist_path)): + Get_Plist.output["docsets"][-1]["plist_path"] = plist_path + for f in ["icon.png", "icon@2x.png"]: + if os.path.isfile(os.path.join(docset_root, f)): + Get_Plist.output["docsets"][-1][f] = os.path.join(docset_root, f) + + + def parse_plist(path): + # TODO figure out all possible fields + fields = [ + "CFBundleIdentifier", + "CFBundleName", + "DocSetPlatformFamily"] + + with open(path, "rb") as fp: + pl = plistlib.load(fp) + + if "isDashDocset" in pl and not pl["isDashDocset"]: + return False + + Get_Plist.output["docsets"].append({}) + for f in fields: + Get_Plist.output["docsets"][-1][f] = pl[f] + + return True + + + def main(): + if not os.path.isdir(Config.docset_base): + e = "Invalid DOCSET_BASE \"" + Config.docset_base + "\"" + logging.error(e); + Get_Plist.output["success"] = False + Get_Plist.output["message"] = e + + else: + Get_Plist.find_plist(Config.docset_base); + Get_Plist.output["success"] = True + + return json.dumps(Get_Plist.output) + +if __name__ == "__main__": + Get_Plist.main() diff --git a/backend/server.py b/backend/server.py index b8e1091..477ad15 100644 --- a/backend/server.py +++ b/backend/server.py @@ -1,6 +1,7 @@ import http.server from config import * +from endpoints.get_plist import * class MyServer(http.server.BaseHTTPRequestHandler): def do_GET(self): @@ -9,7 +10,7 @@ class MyServer(http.server.BaseHTTPRequestHandler): self.send_header("Access-Control-Allow-Origin", "*") self.send_header("Access-Control-Allow-Headers", "content-type") self.end_headers() - output = exec(open("." + self.path).read()) + output = Get_Plist.main() self.wfile.write(output.encode()) -- cgit