-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtagging.py
52 lines (42 loc) · 1.32 KB
/
tagging.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
import os
import json
import argparse
def parse_config(fname):
try:
config = open(fname, 'r').read()
config_dict = json.loads(config)
return config_dict
except:
return None
def write_config(fname, config_dict):
try:
config_file = open(fname, 'w')
json_str = json.dumps(config_dict)
config_file.write(json_str)
config_file.flush()
config_file.close()
except:
return
def main():
aparser = argparse.ArgumentParser()
aparser.add_argument("-d", "--directory",
default="/opt/uhp/configs/",
help="UHP JSON config directory")
aparser.add_argument("-t", "--tags",
help="Comma separated tag string")
args = aparser.parse_args()
try:
tags = [tag.strip() for tag in args.tags.split(',')]
except Exception as e:
tags = []
for fname in os.listdir(args.directory):
if fname.endswith(".json"):
config_dict = parse_config(args.directory + "/" + fname)
try:
config_dict["tags"] = list(set(config_dict["tags"] + tags))
except:
config_dict["tags"] = tags
write_config(args.directory + "/" + fname, config_dict)
return 0
if __name__ == "__main__":
main()