-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmerge_comp_freqs.py
107 lines (78 loc) · 2.64 KB
/
merge_comp_freqs.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
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
#!/usr/bin/env python3
# Merges two CSV documents.
##
import csv
import sys
import os
class MergeError(Exception):
def __init__(self, message):
Exception.__init__(self, message)
class Merged(object):
def __init__(self):
self._all_modules_with_complexity = {}
self._merged = {}
def sorted_result(self):
# Sort on descending order:
ordered = sorted(
list(
self._merged.items()), key=lambda item: int(
item[1][0]), reverse=True)
return ordered
def extend_with(self, name, freqs):
if name in self._all_modules_with_complexity:
complexity = self._all_modules_with_complexity[name]
self._merged[name] = freqs, complexity
def record_detected(self, name, complexity):
self._all_modules_with_complexity[name] = complexity
def skip_heading(f):
next(f)
def read_heading_from(r):
p = next(r)
while p == []:
p = next(r)
return p
def validate_content_by(heading, expected):
comparison = expected.split(',')
stripped = heading[0:len(comparison)] # allow extra fields
if stripped != comparison:
raise MergeError(
'Erroneous content. Expected = ' +
expected +
', got = ' +
','.join(heading))
def parse_csv(merged, filename, parse_action, expected_format):
with open(filename, 'rt') as csvfile:
r = csv.reader(csvfile, delimiter=',')
heading = read_heading_from(r)
validate_content_by(heading, expected_format)
for row in r:
parse_action(merged, row)
def write_csv(stats):
print('module,revisions,code')
for s in stats:
name, (f, c) = s
print(name + ',' + f + ',' + c)
def as_os_aware_path(name):
return os.path.normpath(name)
def parse_complexity(merged, row):
name = as_os_aware_path(row[1])
complexity = row[4]
merged.record_detected(name, complexity)
def parse_freqs(merged, row):
name = as_os_aware_path(row[0])
freqs = row[1]
merged.extend_with(name, freqs)
def merge(revs_file, comp_file):
merged = Merged()
parse_csv(merged, comp_file, parse_complexity,
expected_format='language,filename,blank,comment,code')
parse_csv(merged, revs_file, parse_freqs, expected_format='entity,n-revs')
write_csv(merged.sorted_result())
if __name__ == '__main__':
if len(sys.argv) != 3:
msg = ('Wrong arguments. Require one CSV file with frequencies and one'
' with the complexity')
raise MergeError(msg)
revs_file = sys.argv[1]
comp_file = sys.argv[2]
merge(revs_file, comp_file)