-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproperties2js.py
More file actions
93 lines (73 loc) · 2.94 KB
/
properties2js.py
File metadata and controls
93 lines (73 loc) · 2.94 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
import os
import json
import shutil
import re
import sys
import codecs
# given a valid <your file>.properties, produces <your file>.js in the same folder
def prop2js(inputPath):
if (".properties" != inputPath[-11:]):
raise NameError("invalid filename")
inputText = open(inputPath).read()
lines = inputText.split("\n")
objects = {} # we store every entry as key-value pair within objects
for line in lines:
if (len(line) == 0 or line[0] == '#' or line[0] == '!'):
continue # skipping comments
equalIndex = line.find("=")
if (equalIndex == -1):
raise NameError(line)
# Converting [] syntax to . for ease of parsing
line = line.strip("]").replace("[", ".")
match = re.split(r'\.', line[0: equalIndex+1], 1)
level = objects
oName = line[0:equalIndex]
subName = line[0:equalIndex]
# reads nested objects, split on " [ ] . = " symbols
while(len(match) == 2 and match[1] != ""):
oName = match[0]
subName = match[1]
if (oName not in level):
level[oName] = {}
match = re.split(r'\.', subName, 1)
level = level[oName]
level[subName.strip("[].=")] = line[equalIndex+1:]
# preserves utf8 input
with codecs.open(inputPath.replace(".properties", ".js"), 'w+', encoding='utf8') as output:
output.write("var obj = {")
data = json.dumps(objects, ensure_ascii=False,
indent=4, encoding='utf8')[1:-1]
output.write(unicode(data))
output.write("}")
output.close()
return
# recursively produces clone of .properties directories as .js
def convert(folderPath, outputPath):
shutil.rmtree(outputPath, True)
shutil.copytree(folderPath, outputPath)
for (dirName, subdirList, fileList) in os.walk(outputPath):
for fname in fileList:
if (".properties" == fname[-11:]):
prop2js(dirName+"/"+fname)
os.remove(dirName+"/"+fname)
return
def main():
usage = "\nTo convert a file: python properties2js.py <FILE NAME>.properties \nTo convert folder: python properties2js.py <SRC> <DEST>\n"
if (len(sys.argv) < 2 or len(sys.argv) > 3):
print(usage)
raise EnvironmentError("Incorrect number of arguments")
if (len(sys.argv) == 2 and os.path.isdir(sys.argv[1])):
print(usage)
raise EnvironmentError("Expected a file where folder was provided")
if (len(sys.argv) == 3 and os.path.isfile(sys.argv[1])):
print(usage)
raise EnvironmentError("Expected a folder where file was provided")
if (len(sys.argv) == 3 and os.path.isfile(sys.argv[2])):
print(usage)
raise EnvironmentError("Destination must be a folder")
if (os.path.isdir(sys.argv[1])):
convert(sys.argv[1], sys.argv[2])
if (os.path.isfile(sys.argv[1])):
prop2js(sys.argv[1])
return
main()