forked from code-charity/youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocale.py
71 lines (50 loc) · 1.69 KB
/
locale.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
# TODO: reduce script length
import json
import os
import pathlib
import re
def lowerCamelCase(string):
string = re.sub(r"(-|_)+", " ", string).title().replace(" ", "")
return string[0].lower() + string[1:]
def getListOfFiles(dirName):
allFiles = list()
for entry in os.listdir(dirName):
fullPath = os.path.join(dirName, entry)
if not os.path.isdir(fullPath):
allFiles.append(fullPath)
for entry in os.listdir(dirName):
fullPath = os.path.join(dirName, entry)
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
return allFiles
def addItem(allFiles):
message = input("Enter your message: ")
for keyFile in allFiles:
with open(keyFile, "r+") as json_file:
data = json.load(json_file)
data[lowerCamelCase(message)] = {
"message": message
}
json_file.seek(0)
json.dump(data, json_file, indent=4)
json_file.truncate()
def removeItem(allFiles):
key = input("Enter your key (lowerCamelCase): ")
for keyFile in allFiles:
with open(keyFile, "r+") as json_file:
data = json.load(json_file)
if data[key]:
del data[key]
json_file.seek(0)
json.dump(data, json_file, indent=4)
json_file.truncate()
allFiles = getListOfFiles("_locales/")
operation = input("""--------------------------------
Add item: 1
Remove item: 2
--------------------------------
Enter number: """)
if operation == "1":
addItem(allFiles)
elif operation == "2" :
removeItem(allFiles)