"""Adds call-paren spacing, GNU return-type-on-own-line, function-name casing, bool-flag prefix and CRLF columns to metrics.csv.""" exec(open('style_metrics.py').read().split("if __name__")[0]) import os, re, csv, collections KW = set('if for while switch return sizeof catch defined'.split()) SKIP = {'if','for','while','switch','catch','CRITICAL_BLOCK','foreach','BOOST_FOREACH','IMPLEMENT_SERIALIZE','sizeof','return'} rows = list(csv.DictReader(open('metrics.csv'))); by = {r['corpus']: r for r in rows} for name in by: sp = ns = ra = rs = 0; cas = collections.Counter(); fb = collections.Counter(); crlf = tot = 0 for p in corpus_files(os.path.join(ROOT, name)): rawb = open(p, 'rb').read(); crlf += rawb.count(b'\r\n'); tot += rawb.count(b'\n') raw = read(p); code, _ = strip_comments_strings(raw); L = code.split('\n') for t in re.findall(r'\b(?:bool|BOOL)\s+([fb])[A-Z]\w*', code): fb[t] += 1 for i, l in enumerate(L): s = l.strip() sp += sum(1 for w in re.findall(r'\b([A-Za-z_]\w*) \(', s) if w not in KW and w not in KEYWORDS) ns += sum(1 for w in re.findall(r'\b([A-Za-z_]\w*)\(', s) if w not in KW and w not in KEYWORDS) if l.startswith('{') and i > 1: prev = L[i-1].rstrip(); pp = L[i-2] if re.match(r'^[A-Za-z_][\w:~]*\s*\(.*\)\s*(const)?$', prev) and pp.strip() and re.match(r'^[A-Za-z_][\w\s\*&<>:,]*$', pp.strip()) and not pp.strip().endswith(';') and not pp[:1] in ' \t': ra += 1 elif re.match(r'^[A-Za-z_][\w\s\*&<>:,]*\s[\*&]?[A-Za-z_][\w:~]*\s*\(.*\)\s*(const)?$', prev): rs += 1 if s == '{' or (s.endswith('{') and s != '{'): hdr = l if s != '{' else (L[i-1] if i else '') m = re.search(r'(?:::|^|[\s\*&])([A-Za-z_]\w*)\s*\([^;]*\)\s*(const)?\s*\{?\s*$', hdr) if m and m.group(1) not in KEYWORDS and m.group(1) not in SKIP: n = m.group(1) cas['ALLCAPS' if re.match(r'^[A-Z][A-Z0-9_]+$', n) else 'Pascal' if n[0].isupper() else 'camel' if re.match(r'^[a-z]+[A-Z]', n) else 'lower'] += 1 r = by[name] r['call_space_before_paren_ratio'] = round(sp/(sp+ns), 3) if sp+ns else '' r['gnu_return_type_own_line_ratio'] = round(ra/(ra+rs), 3) if ra+rs else '' t = sum(cas.values()) r['func_name_pascal_pct'] = round(100*cas['Pascal']/t, 1) if t else '' r['func_name_camel_pct'] = round(100*cas['camel']/t, 1) if t else '' r['func_name_lower_pct'] = round(100*cas['lower']/t, 1) if t else '' r['bool_f_prefix'] = fb['f']; r['bool_b_prefix'] = fb['b'] r['crlf_ratio'] = round(crlf/tot, 3) if tot else '' keys = list(rows[0].keys()) w = csv.DictWriter(open('metrics.csv', 'w', newline=''), fieldnames=keys); w.writeheader(); [w.writerow(r) for r in rows] for r in rows: print(r['corpus'], {k: r[k] for k in keys[-9:]})