-
Notifications
You must be signed in to change notification settings - Fork 0
/
laravel_locale_finder.py
93 lines (73 loc) · 2.04 KB
/
laravel_locale_finder.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
import os
import re
import sys
import json
def replace_chars(string):
data = string
chars_to_relace = [
'{{ __', ' }}', "('", '("',
"')", '")',
]
for char in chars_to_relace:
data = data.replace(char, '')
return data
def get_files_paths(folder):
paths = []
for root, dirs, files in os.walk(folder):
for file in files:
paths.append(os.path.join(root, file))
return paths
def remove_duplicates(data):
result = {}
for key, value in data.items():
if value not in result.values():
data[key] = value
return data
def main():
# Getting arguments
args = []
params = {}
try:
del sys.argv[0]
args = sys.argv
except IndexError as e:
pass
if args:
params['folder'] = args[0]
try:
params['input'] = args[1]
params['output'] = args[1] + '.output'
except IndexError as e:
pass
# Loading input file
if 'input' in params:
try:
with open(params['input']) as input_file:
input_data = json.load(input_file)
except ValueError as e:
print('Input file format error')
exit()
# Finding matched pattern in files
pattern = re.compile("{{ __\\(.*\\) }}")
files = get_files_paths(params['folder'])
founds = []
result = {}
for file in files:
for i, line in enumerate(open(file)):
for match in re.finditer(pattern, line):
founds.append(replace_chars(match.group()))
for found in founds:
result[found] = ''
# Removing duplicates in data
result = remove_duplicates(result)
if 'input' in params:
for data in result:
if data not in input_data:
input_data[data] = ''
with open(params['output'], 'w') as outfile:
json.dump(input_data, outfile)
print('Output file: ' + params['output'])
else:
print(result)
if __name__ == "__main__":
main()