-
Notifications
You must be signed in to change notification settings - Fork 72
/
update_tree.py
executable file
·67 lines (55 loc) · 2.18 KB
/
update_tree.py
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
import json
LEGEND = "📦🌍💵📒🪙🧅🧩❗️"
class Conversion:
def __init__(self, Dict:dict) -> None:
self.dict = Dict
self.elements = 0
def sorter(self, dico:dict) -> dict:
output = {}
for k in sorted(dico.keys(), key=lambda x:(x.split(" ",1)[-1] if x[0] in LEGEND else x).lower()):
if type(dico[k]) is dict:
output[k] = self.sorter(dico=dico[k])
else:
output[k] = dico[k]
return output
def to_arf(self) -> dict:
"""Converts the userfriendly dict to the arf.json dict"""
self.dict = self.sorter(self.dict) # Pour que l'arbre et toutes ses branches soient classé par ordre alphabétique
return self._to_arf(self.dict)
def from_arf(self) -> dict:
"""Converts the arf.json dict to the userfriendly dict"""
return self._from_arf(self.dict)
def _to_arf(self, dictio:dict) -> dict:
output = {}
for k, v in dictio.items():
output["name"] = k
if type(v) is dict:
output["children"] = [self._to_arf({x:y}) for x, y in v.items()]
output["type"] = "folder"
elif type(v) is str:
output["url"] = v
output["type"] = "url"
self.elements += 1
else:
raise TypeError(f"{v} must be a dict or a string.")
return output
def _from_arf(self, dictio:dict) -> dict:
output = {}
if dictio["type"] == "url":
self.elements += 1
output[dictio["name"]] = dictio["url"]
elif dictio["type"] == "folder":
output[dictio["name"]] = {}
for item in dictio["children"]:
output[dictio["name"]].update(self._from_arf(item))
return output
def main() -> None:
print("Updating ...", end="\r")
with open("database.json", "r", encoding="utf-8") as f:
json_tree = json.loads(f.read())
c = Conversion(json_tree)
with open("js/tree.json","w", encoding="utf-8") as f:
json.dump(c.to_arf(), f, indent=4)
print(f"Updated ! {c.elements} urls in the list.")
if __name__ == "__main__":
main()