-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_jsons.py
More file actions
144 lines (114 loc) Β· 5.13 KB
/
Copy pathmerge_jsons.py
File metadata and controls
144 lines (114 loc) Β· 5.13 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from __future__ import annotations
import json
import os
import argparse
class JSONSMerger():
def __init__(self, username: str, directory: str = './data/repositories/'):
self.directory = f"{directory}/{username}"
self.username = username
self.json = {}
def merge_all(self):
for file_path in os.listdir(self.directory):
if file_path.endswith(".json") and file_path.startswith(self.username):
self.merge(file_path)
def merge(self, json_path: str):
merging_json = self.open_json(json_path)
for info in merging_json:
if "linter" in info:
self.merge_linter(info)
else:
self.merge_total(info)
# * -=-=-=-=-=-=-=-=-=-=-=-
# Merging related to linter parsed results
def merge_linter(self, info: dict):
if info["language"] in self.json and info["linter"] in self.json[info["language"]]:
self.linter_blend(info)
return
self.linter_adjoin(info)
def linter_adjoin(self, info: dict):
if not info["language"] in self.json:
self.json[info["language"]] = {}
self.json[info["language"]][info["linter"]] = {}
new_entry = self.json[info["language"]][info["linter"]]
new_entry["files"] = info["files"]
new_entry["fixed"] = info["fixed"] if info["fixed"] else 0
new_entry["errors"] = info["errors"]
def linter_blend(self, info: dict):
new_entry = self.json[info["language"]][info["linter"]]
new_entry["files"] += info["files"]
new_entry["fixed"] += info["fixed"] if info["fixed"] else 0
new_entry["errors"] += info["errors"]
# * -=-=-=-=-=-=-=-=-=-=-=-
# Merging related to total parsed results
def merge_total(self, info: dict):
if info["language"] in self.json and "total" in self.json[info["language"]]:
self.total_blend(info)
return
self.total_adjoin(info)
def total_adjoin(self, info: dict):
if not info["language"] in self.json:
self.json[info["language"]] = {}
self.json[info["language"]]["total"] = {}
new_entry = self.json[info["language"]]["total"]
new_entry["files"] = info["files"]
new_entry["lines"] = info["lines"]
new_entry["tokens"] = info["tokens"]
new_entry["clones"] = info["clones"]
new_entry["duplicate_lines_num"] = info["duplicate_lines_num"]
new_entry["duplicate_tokens_num"] = info["duplicate_tokens_num"]
# Obviously stacking duplicate percentages doesn't make any sense.
# new_entry["duplicate_lines_percent"] = info["duplicate_lines_percent"]
# new_entry["duplicate_tokens_percent"] = info["duplicate_tokens_percent"]
def total_blend(self, info: dict):
new_entry = self.json[info["language"]]["total"]
new_entry["files"] += info["files"]
new_entry["lines"] += info["lines"]
new_entry["tokens"] += info["tokens"]
new_entry["clones"] += info["clones"]
new_entry["duplicate_lines_num"] += info["duplicate_lines_num"]
new_entry["duplicate_tokens_num"] += info["duplicate_tokens_num"]
# Obviously stacking duplicate percentages doesn't make any sense.
# new_entry["duplicate_lines_percent"] += info["duplicate_lines_percent"]
# new_entry["duplicate_tokens_percent"] += info["duplicate_tokens_percent"]
# * -=-=-=-=-=-=-=-=-=-=-=-
# Utility Functions
def open_json(self, filename: str) -> dict | None:
"""Opens up JSON file in the same directory.
Args:
file_path (str): json file name
Returns:
dict: loaded json as dictionary
"""
json_path = os.path.join(self.directory, filename)
try:
with open(json_path, "r") as json_file:
return json.load(json_file)
except FileNotFoundError:
print(f"Couldn't find {filename}. (path: {json_path}) file.")
return None
def save_json(self, output_filename: str, indent: int = 4):
""" Saves currently stored json in the class to file
Args:
output_filename (str): the name of output json file
"""
save_dir = './data/scanned_user'
with open(f'{save_dir}/{output_filename}.json', 'w') as json_file:
json.dump(self.json, json_file, indent=indent, sort_keys=True)
print(f"Succesfully parsed and saved as {json_file.name}.")
def print(self, indent: int = 2):
""" Printsout currently stored json in the class. """
print(json.dumps(self.json, indent=indent, sort_keys=True))
def arguments_parser():
parser = argparse.ArgumentParser(description="Mega Linter Data Scraper")
parser.add_argument("-u", "--username", default="Luzkan",
help="Username (which is name of the directory containing parsed linter .json's). \
(default: %(default)s)")
return parser.parse_args()
def main():
args = arguments_parser()
jm = JSONSMerger(args.username)
jm.merge_all()
jm.print()
jm.save_json(f'{args.username}')
if __name__ == "__main__":
main()