-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace-keys.py
More file actions
executable file
·60 lines (47 loc) · 1.55 KB
/
replace-keys.py
File metadata and controls
executable file
·60 lines (47 loc) · 1.55 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
from os import listdir
from os.path import join, isfile, isdir, expanduser
from json import loads
import string
import sys
def find(path, file_op, arg, ignore=[]):
for item in listdir(path):
f = join(path, item)
if not item in ignore:
if isfile(f):
file_op(f, arg)
elif isdir(f):
find(f, file_op, arg)
else:
print('Ignoring {}'.format(f))
def insert_data(filename, data):
try:
with open(filename, 'r') as f:
file_str = f.read()
except UnicodeDecodeError:
print('Ignoring {}. (UnicodeDecodeError)'.format(filename))
return
for key, value in data.items():
file_str = file_str.replace('{'+key+'}', value)
with open(filename, 'w') as f:
f.write(file_str)
def extract_data(data_filename):
try:
with open(data_filename, 'r') as data_file:
data_str = data_file.read()
return loads(data_str)
except:
print('Error in processing JSON file: ', sys.exc_info()[0])
raise
def replace(directory, filename, ignore=[]):
data = extract_data(filename)
find(directory, insert_data, data, ignore)
print('All keys in {} replaced'.format(directory))
def main():
try:
directory = sys.argv[1]
data_filename = sys.argv[2]
except IndexError:
sys.exit('Usage: python3 replace-keys.py [directory] [data.json]')
replace(directory, data_filename)
if __name__ == '__main__':
main()