Commit b6c178927ea5fc74f5bddc820baef58ddd840319

Authored by quillotm
1 parent 91758e85fb
Exists in master

Solve issue: trying to get pointer[way] when the way does not exists.

Showing 1 changed file with 3 additions and 3 deletions Inline Diff

1 import json 1 import json
2 import argparse 2 import argparse
3 from utils import SubCommandRunner 3 from utils import SubCommandRunner
4 import json 4 import json
5 from os import path 5 from os import path
6 import sys 6 import sys
7 7
8 def write_run(file, jsonpath, content, force): 8 def write_run(file, jsonpath, content, force):
9 9
10 if content is None: 10 if content is None:
11 content = "" 11 content = ""
12 for line in sys.stdin: 12 for line in sys.stdin:
13 content += line 13 content += line
14 14
15 # Data variables 15 # Data variables
16 content_data = json.loads(content) 16 content_data = json.loads(content)
17 17
18 data = {} 18 data = {}
19 19
20 # Load file if necessary 20 # Load file if necessary
21 if path.isfile(file): 21 if path.isfile(file):
22 with open(file, "r") as f: 22 with open(file, "r") as f:
23 data = json.load(f) 23 data = json.load(f)
24 24
25 # Walk through the json path 25 # Walk through the json path
26 pointer = data 26 pointer = data
27 for i, way in enumerate(jsonpath): 27 for i, way in enumerate(jsonpath):
28 # End of the json path 28 # End of the json path
29
29 if i == len(jsonpath) - 1: 30 if i == len(jsonpath) - 1:
30 31
31 if not force and way in pointer: 32 if not force and way in pointer:
32 raise Exception(f"In your json file, {way} already exists in the path {jsonpath}. Add --force option.") 33 raise Exception(f"In your json file, {way} already exists in the path {jsonpath}. Add --force option.")
33 else: 34 else:
34 # If dictionary in both cases, copy from content to pointer 35 # If dictionary in both cases, copy from content to pointer
35 if type(pointer[way]) is dict and type(content_data) is dict: 36 if way in pointer and type(pointer[way]) is dict and type(content_data) is dict:
36 for key in content_data: 37 for key in content_data:
37 pointer[way][key] = content_data[key] 38 pointer[way][key] = content_data[key]
38 else: 39 else:
39 pointer[way] = content_data 40 pointer[way] = content_data
40 else: 41 else:
41 if way not in pointer: 42 if way not in pointer:
42 pointer[way] = {} 43 pointer[way] = {}
43 pointer = pointer[way] 44 pointer = pointer[way]
44
45 # Write the file 45 # Write the file
46 with open(file, "w") as f: 46 with open(file, "w") as f:
47 json.dump(data, f) 47 json.dump(data, f)
48 48
49 49
50 if __name__ == "__main__": 50 if __name__ == "__main__":
51 # Main parser 51 # Main parser
52 parser = argparse.ArgumentParser(description="Edite a json file using this command") 52 parser = argparse.ArgumentParser(description="Edite a json file using this command")
53 subparsers = parser.add_subparsers(title="action") 53 subparsers = parser.add_subparsers(title="action")
54 54
55 # kmeans 55 # kmeans
56 parser_write = subparsers.add_parser( 56 parser_write = subparsers.add_parser(
57 "write", 57 "write",
58 help="""Allow you to add content to a json file. 58 help="""Allow you to add content to a json file.
59 If the json file does not exists, it will create the json file with a dictionary as root. 59 If the json file does not exists, it will create the json file with a dictionary as root.
60 60
61 Example of command: 61 Example of command:
62 python -m volia.jsoneditor write file.json --jsonpath 6 "entropy" --content "[6, 6, 8] --force 62 python -m volia.jsoneditor write file.json --jsonpath 6 "entropy" --content "[6, 6, 8] --force
63 63
64 You can also use pipeline functionality to use the output of a command as the added content in the json. 64 You can also use pipeline functionality to use the output of a command as the added content in the json.
65 65
66 Example of pipeline command: 66 Example of pipeline command:
67 echo "2.06" | python -m volia.jsoneditor write file.json --jsonpath 6 "entropy" --force 67 echo "2.06" | python -m volia.jsoneditor write file.json --jsonpath 6 "entropy" --force
68 68
69 """) 69 """)
70 70
71 parser_write.add_argument("file", type=str, help="json file") 71 parser_write.add_argument("file", type=str, help="json file")
72 parser_write.add_argument("--jsonpath", 72 parser_write.add_argument("--jsonpath",
73 required=True, 73 required=True,
74 nargs="+", 74 nargs="+",
75 help="json path, example: a b => root[a]['b'] if a int and b str") 75 help="json path, example: a b => root[a]['b'] if a int and b str")
76 parser_write.add_argument("--content", default=None, type=str, help="json content to add") 76 parser_write.add_argument("--content", default=None, type=str, help="json content to add")
77 parser_write.add_argument("--force", action="store_true", help="force to rewrite if the key already exists") 77 parser_write.add_argument("--force", action="store_true", help="force to rewrite if the key already exists")
78 parser_write.set_defaults(which="write") 78 parser_write.set_defaults(which="write")
79 79
80 # Parse 80 # Parse
81 args = parser.parse_args() 81 args = parser.parse_args()
82 82
83 # Run commands 83 # Run commands
84 runner = SubCommandRunner({ 84 runner = SubCommandRunner({
85 "write": write_run, 85 "write": write_run,
86 }) 86 })
87 87
88 runner.run(args.which, args.__dict__, remove="which") 88 runner.run(args.which, args.__dict__, remove="which")
89 89