summaryrefslogtreecommitdiff
path: root/backend/endpoints/get_plist.py
blob: ac7366ec186b4a9a2d5cd7fd6be68cc84453710b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import json
import logging
import os
import plistlib
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from config import Config

class Get_Plist:
    output = dict()


    def find_index(path):
        if os.path.isfile(os.path.join(path, "index.html")):
            return os.path.join(path, "index.html")

        with os.scandir(path) as it:
            for entry in it:
                if entry.name == "." or entry.name == "..":
                    continue
                if(os.DirEntry.is_dir(entry)):
                    return Get_Plist.find_index(os.path.join(path, entry.name))

        return False


    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]["docset_root"] = docset_root
                        Get_Plist.output["docsets"][-1]["docset_index"] = os.path.join(docset_root, Get_Plist.output["docsets"][-1]["docset_index"])
                        if not os.path.isfile(Get_Plist.output["docsets"][-1]["docset_index"]):
                            index_loc = Get_Plist.find_index(Get_Plist.output["docsets"][-1]["docset_index"])
                            if not index_loc:
                                del Get_Plist.output["docsets"][-1]["docset_index"]
                            else:
                                Get_Plist.output["docsets"][-1]["docset_index"] = index_loc


                        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]

        # Find the index page
        Get_Plist.output["docsets"][-1]["docset_index"] = os.path.join("Contents", "Resources", "Documents")
        if "dashIndexFilePath" in pl:
            Get_Plist.output["docsets"][-1]["docset_index"] = os.path.join(Get_Plist.output["docsets"][-1]["docset_index"], pl["dashIndexFilePath"])

        return True


    def main():
        Get_Plist.output["docsets"] = []

        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()