Skip to content

Commit

Permalink
feat: combine multiple json file (#66)
Browse files Browse the repository at this point in the history
* feat: combine multiple json file

* remove the exception within function
  • Loading branch information
xhdix authored Jun 16, 2022
1 parent ac97ba1 commit 6f26700
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions tracevis.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ def show_conf_route():
print(conf.route)


def combine_json_files(json_list_files):
print("saving combined json file...")
all_measurements = []
for json_list_file in json_list_files:
for json_file in json_list_file:
print("· - · · · adding: " + json_file)
with open(json_file) as json_file:
for measurement in json.load(json_file):
all_measurements.append(measurement)
print("· · · - · · · · - · · · · - · · · · - · ")
combined_data_path = json_list_files[0][0].replace(
".json", "_combined.json")
with open(combined_data_path, "w") as combined_jsonfile:
combined_jsonfile.write(json.dumps(all_measurements,
default=lambda o: o.__dict__))
print("saved: " + combined_data_path)
print("· · · - · - · · · - · - · · · - · - · · · - · -")
return combined_data_path


def dump_args_to_file(file, args, packet_info):
print("saving measurement config...")
args_without_config_arg = args.copy()
Expand Down Expand Up @@ -99,7 +119,7 @@ def get_args():
help="download the latest traceroute measuremets of a RIPE Atlas probe via ID and visualize")
parser.add_argument('-I', '--ripemids', type=str,
help="add comma-separated RIPE Atlas measurement IDs (up to 12)")
parser.add_argument('-f', '--file', type=str,
parser.add_argument('-f', '--file', type=str, action='append', nargs='+',
help="open a measurement file and visualize")
parser.add_argument('--csv', action='store_true',
help="create a sorted csv file instead of visualization")
Expand Down Expand Up @@ -288,7 +308,20 @@ def main(args):
probe_id=args["ripe"], output_dir=output_dir, name_prefix=name_prefix,
measurement_ids=measurement_ids)
if args.get("file"):
measurement_path = args["file"]
try:
# -f filename*.json
# [['filename1.json','filename2.json','filename3.json',]]
#
# -f filename1.json -f filename2.json
# [['filename1.json'],['filename2.json']]
#
if len(args["file"]) > 1 or len(args["file"][0]) > 1:
measurement_path = combine_json_files(args["file"])
else:
measurement_path = args["file"][0][0]
except Exception as e:
print(f"Error!\n{e!s}")
exit(1)
if args.get("csv"):
utils.csv.json2csv(measurement_path)
elif args.get("csvraw"):
Expand Down

0 comments on commit 6f26700

Please sign in to comment.