-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
195 lines (178 loc) · 6.76 KB
/
Copy pathmain.py
File metadata and controls
195 lines (178 loc) · 6.76 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import fileManager as fm
import re
from consolemenu import *
from consolemenu.items import *
from http.server import HTTPServer
from server import Server
import time
import json
import os
from tabulate import tabulate as tb
import math
from tqdm import tqdm
HOST_NAME = 'localhost'
PORT = 8000
def safeInput(text, reg='.*'):
result = None
while result is None or re.search(reg, result) is None:
if result is not None: print('Invalid Input')
result = input(text)
return result
def AddLabelsCommandLine():
oldCat = 'Misc'
while(True):
print('Input New Label:\n'+'='*30)
fn = safeInput('Api Name: ', '^[a-zA-Z]+$')
val = safeInput('Value: ', '.+')
desc = safeInput('Description: ', '.+')
cat = input(f'Category{"" if oldCat is None else f"(default: {oldCat})"}: ')
if cat == '': cat = oldCat
lang = input('Language(default: en_US): ')
manager.addLabel(fm.Label(fn,val,desc,cat,lang))
oldCat = cat
print('='*30+'\n')
def StartWebAppServer():
httpd = HTTPServer((HOST_NAME,PORT),Server)
httpd.RequestHandlerClass.manager = manager
print(f'[{time.asctime()}]\tStart Server - {HOST_NAME.lower() if "http" in HOST_NAME.lower() else f"http://{HOST_NAME.lower()}"}:{PORT}')
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print(f'[{time.asctime()}]\tStop Server - {HOST_NAME}:{PORT}')
def CheckFileConsistency():
labels = manager.getLabelsFromJS('AK')
labels = labels + manager.getLabelsFromJS('LZ')
inconsistent = [l for l in labels if l['name']!=l['object']]
if len(inconsistent) == 0:
print('No inconsistent labels found in JS files')
else:
print('Labels with different names from customLabel:')
for l in inconsistent:
print(f'{l["name"]} - {l["object"]}')
input('Press Enter to continue...')
def SortFileContents():
labelsObjs = manager.getLabelsFromXML()
JSlabelsAK = manager.getLabelsFromJS(file='AK')
JSlabelsLZ = manager.getLabelsFromJS(file='LZ')
labelsObjs.sort(key= lambda x: x['fullName'].lower())
manager.saveLabelsToXML(labelsObjs)
JSlabelsAK.sort(key=lambda x: x['name'].lower())
manager.saveLabelsToJS(JSlabelsAK,file='AK')
JSlabelsLZ.sort(key=lambda x: x['name'].lower())
manager.saveLabelsToJS(JSlabelsLZ,file='LZ')
print('DONE!')
input('Press Enter to continue...')
def CheckJSFiles():
labelsObjs = manager.getLabelsFromXML()
JSlabelsAK = manager.getLabelsFromJS(file='AK')
JSlabelsLZ = manager.getLabelsFromJS(file='LZ')
labelApis = set()
for l in JSlabelsAK:
labelApis.add(l['object'])
for l in JSlabelsLZ:
labelApis.add(l['object'])
notImportedLabels = []
for l in labelsObjs:
if not l['fullName'] in labelApis:
notImportedLabels.append(l['fullName'])
if len(notImportedLabels) > 0:
print(f'The Following label Api names are not imported in JS files: {len(notImportedLabels)}')
printSplitStrings(notImportedLabels,5)
input('Press Enter to continue...')
menu = ConsoleMenu(f"What do you want to do with those {len(notImportedLabels)} labels?", "")
def delete():
nil = set(notImportedLabels)
newObjs = []
for obj in labelsObjs:
if obj['fullName'] not in nil:
newObjs.append(obj)
manager.saveLabelsToXML(newObjs)
def imprt():
for l in notImportedLabels:
if l[0].lower()<='k':
JSlabelsAK.append({'name':l,'object':l})
else:
JSlabelsLZ.append({'name':l,'object':l})
JSlabelsAK.sort(key=lambda x: x['name'].lower())
manager.saveLabelsToJS(JSlabelsAK,file='AK')
JSlabelsLZ.sort(key=lambda x: x['name'].lower())
manager.saveLabelsToJS(JSlabelsLZ,file='LZ')
menu.append_item(FunctionItem("Delete labels from CustomLabels file",delete,[],should_exit=True))
menu.append_item(FunctionItem("Import labels to JS",imprt,[],should_exit=True))
menu.show()
else:
print('All Labels are imported')
input('Press Enter to continue...')
def stringConstantLength(s,length):
return "{:<{length}}".format(s,length=length) if len(s)<length else s[:length-3]+'...'
def LookForUnusedLabels():
labels = manager.getLabelsFromXML()
rootdir = manager.rootFolder
uses = {}
for l in labels:
uses[l['fullName']] = 0
apiNames = list(uses.keys())
t = tqdm(list(os.walk(rootdir+'\\src')))
for folder,dirs,file in t:
if len([folder for exc in ['contentassets','documents','siteDotComSites','staticresources','translations'] if exc in folder])>0: continue
for files in [f for f in file if f not in ['labelsAK.js','labelsLZ.js','CustomLabels.labels']]:
t.desc = stringConstantLength(files,40)
try:
with open(os.path.join(folder,files),'r',encoding='UTF-8') as fileObj:
fileData = fileObj.read()
for l in apiNames:
if l in fileData:
uses[l] += 1
except:
print(folder)
unusedLabels = [l for l in apiNames if uses[l]==0]
if len(unusedLabels) > 0:
print(f'{len(unusedLabels)} Unused Labels:')
printSplitStrings(unusedLabels,5)
else:
print('No unused labels found')
input('Press Enter to continue...')
def printSplitStrings(ls,columns=3):
num = len(ls)
itemsPerColumn = math.ceil(num/columns)
table = []
for i in range(itemsPerColumn):
row=[]
for c in range(columns):
idx = c * itemsPerColumn + i
row.append(ls[idx] if idx < num else '')
table.append(row)
print(tb(table))
def loadConfiguration():
try:
with open('project.config','r') as configFile:
data = configFile.read()
config = json.loads(data)
return config
except Exception as e:
pass
return { 'rootDirectory': None }
def saveConfiguration(config):
with open('project.config','w') as configFile:
data = configFile.write(json.dumps(config))
if __name__=="__main__":
config = loadConfiguration()
manager = fm.fileManager()
if manager.selectFolder(config['rootDirectory']) is None:
exit()
config['rootDirectory'] = manager.rootFolder
saveConfiguration(config)
menu = ConsoleMenu("Label Maker", "Please Choose an action to continue")
RepairSubMenu = ConsoleMenu("Repair Files","Choose an action:")
RepairSubMenu.append_item(FunctionItem("Check Javascript import files", CheckJSFiles, []))
RepairSubMenu.append_item(FunctionItem("Reformat label files", SortFileContents, []))
RepairSubMenu.append_item(FunctionItem("Check File Consistency", CheckFileConsistency, []))
RepairSubMenu.append_item(FunctionItem("Look for unused labels", LookForUnusedLabels, []))
r_submenu_item = SubmenuItem("Repair Files", RepairSubMenu, menu)
menu.append_item(FunctionItem("Change Project Directory", manager.selectFolder, []))
menu.append_item(FunctionItem("Start Web App", StartWebAppServer, []))
menu.append_item(FunctionItem("Add Labels", AddLabelsCommandLine, []))
menu.append_item(r_submenu_item)
menu.show()