-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.py
executable file
·100 lines (80 loc) · 3.35 KB
/
script.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
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
93
94
95
96
97
98
99
100
#!/usr/bin/python3
import os
import json
from shutil import move
from natsort import natsorted, ns
# SCHOOL_ADVICE = "School Advice"
LANGUAGES = "Languages"
LOCAL_RESOURCES = "local_resources"
ADVICE_NON_COVID = "Advice for non-COVID patients"
# SHOP_ADVICE = "Shop Advice"
# PLACE_OF_WORSHIP = "Place of worship Advice"
rootDir = "graphics"
ALL_LANGUAGES = []
# Map directory name to a dict
forAutoGen = [LANGUAGES, LOCAL_RESOURCES, ADVICE_NON_COVID]
data = {
LANGUAGES: [],
LOCAL_RESOURCES: [],
# SCHOOL_ADVICE: [],
ADVICE_NON_COVID: []
}
def checkForPathMisMatch(lan):
if lan not in ALL_LANGUAGES:
raise Exception("Same name expected for " + lan + "in Language folder")
def renameToPng(path):
rt = path
if path.endswith("PNG"):
striped = os.path.splitext(path)[0]
move(striped + ".PNG", striped + ".png")
print("renamed file ", path)
rt = striped + ".png"
split = list(os.path.split(rt))
orgFile = split[-1]
striped = split[-1].replace(" ", "")
split.pop(-1)
move(os.path.sep.join(split + [orgFile]), os.path.sep.join(split + [striped]))
return os.path.sep.join(split + [striped])
def convertToSrcPathToLinux(path: str):
return path.replace(os.path.sep, "/")
def titleCasing(titleString):
return ' '.join([w.title() if w.islower() else w for w in titleString.split()])
def main():
for paths in forAutoGen:
toProcPath = os.path.join(rootDir, paths)
for lvl1Dir in os.listdir(toProcPath):
if paths == LANGUAGES:
ALL_LANGUAGES.append(lvl1Dir)
if paths != LANGUAGES and paths != LOCAL_RESOURCES:
checkForPathMisMatch(lvl1Dir)
entry = {"language": lvl1Dir, "graphics": []}
for lvl2Dir in os.listdir(os.path.join(toProcPath, lvl1Dir)):
dirOrFilePath = os.path.join(toProcPath, lvl1Dir, lvl2Dir)
if os.path.isdir(dirOrFilePath):
for file in os.listdir(dirOrFilePath):
if (len(os.listdir(dirOrFilePath))) > 1:
raise Exception("Too many files in dir " + dirOrFilePath)
if file.endswith("png"):
entry["graphics"].append(
{"info": {"name": titleCasing(lvl2Dir),
"src": convertToSrcPathToLinux(os.path.join(dirOrFilePath, file))}})
elif file.endswith("PNG"):
reNamed = renameToPng(os.path.join(dirOrFilePath, file))
entry["graphics"].append(
{"info": {"name": titleCasing(lvl2Dir), "src": convertToSrcPathToLinux(reNamed)}})
if len(entry["graphics"]) != 0:
data[paths].append(entry)
sortedLan = sorted(data[LANGUAGES], key=lambda k: k['language'])
data[LANGUAGES] = sortedLan
# TODO: make this into a function
count = 0
for lan in data[ADVICE_NON_COVID]:
data[ADVICE_NON_COVID][count]["graphics"] = sorted(lan["graphics"], key=lambda k: k['info']["name"])
count = count + 1
indx = 0
for lan in data[LANGUAGES]:
data[LANGUAGES][indx]["graphics"] = natsorted(lan["graphics"], key=lambda k: k['info']["name"])
indx = indx + 1
print(json.dumps(data))
if __name__ == "__main__":
main()