annotate combineJSON.py @ 1:661bc10b0612 draft default tip

planemo upload commit 67dd8ca7284a045d8b96fa2bcef60dd101693cca-dirty
author nml
date Fri, 13 Apr 2018 13:46:46 -0400
parents 116510205617
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
1 #!/usr/bin/env python
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
2 import argparse
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
3 import json
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
4 import sys
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
5
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
6
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
7 def init_parser():
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
8 parser = argparse.ArgumentParser(
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
9 prog="combineJSON",
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
10 formatter_class=argparse.RawDescriptionHelpFormatter,
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
11 description="Combine JSON data arrays into a single array")
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
12 parser.add_argument('-i',
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
13 nargs='*',
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
14 help="Input JSON files to be combined")
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
15 parser.add_argument('-o',
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
16 help='Output file name')
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
17 return parser
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
18
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
19
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
20 parser = init_parser()
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
21 args = parser.parse_args()
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
22 input_files = args.i
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
23 json_file = []
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
24
1
661bc10b0612 planemo upload commit 67dd8ca7284a045d8b96fa2bcef60dd101693cca-dirty
nml
parents: 0
diff changeset
25 if input_files is None or len(input_files) < 1:
0
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
26 print('Not enough input files. '
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
27 'Please use -i filename.txt filename1.txt '
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
28 'to combine JSON data files')
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
29 sys.exit(0)
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
30
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
31
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
32 for file_path in input_files:
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
33 try:
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
34 # Attempt to open each input file, parse as JSON
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
35 with open(file_path, 'r') as curr_file:
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
36 file_data = curr_file.read()
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
37 parsed_json_file = json.loads(file_data)
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
38 # Append each valid JSON data array
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
39 for entry in parsed_json_file:
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
40 json_file.append(entry)
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
41 except Exception as e:
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
42 print("Help! I can't parse this file {}. "
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
43 "Are you sure this is a valid JSON file?"
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
44 .format(file_path))
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
45 raise(e)
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
46
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
47 if args.o:
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
48 with open(args.o, 'w') as out_json:
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
49 json.dump(json_file, out_json)
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
50 else:
116510205617 planemo upload commit dd7f4dc22bbe2f26aafd07a345997db79d9e5ad1
nml
parents:
diff changeset
51 print(json.dumps(json_file))