<commit_before>import re
import os


class Attribute:
    def __init__(self):
        pass


class MetrixPP:
    def __init__(self):
        self.magic = Attribute()
        self.size = Attribute()
        self.todo = Attribute()
        self.complexity = Attribute()
        self.lines = Attribute()

        self.lines.total = Attribute()
        self.lines.code = Attribute()
        self.lines.comments = Attribute()

    def parse(self, fn):
        with open(fn, 'r') as f:
            for l in f:
                if ':: ' and "'"in l:
                    param = l.split("'")[1]
                elif 'Average' in l:
                    avg = float(re.findall(r"[-+]?\d*\.\d+|\d+", l.split(':')[-1].strip())[0])
                elif 'Total' in l:
                    tot = float(re.findall(r"[-+]?\d*\.\d+|\d+", l.split(':')[-1].strip())[0])

                    if param == 'std.code.magic:numbers':
                        self.magic.average = avg
                        self.magic.total = tot
                    elif param == 'std.general:size':
                        self.size.average = avg
                        self.size.total = tot
                    elif param == 'std.code.todo:comments':
                        self.todo.average = avg
                        self.todo.total = tot
                    elif param == 'std.code.complexity:cyclomatic':
                        self.complexity.average = avg
                        self.complexity.total = tot
                    elif param == 'std.code.length:total':
                        self.lines.total.average = avg
                        self.lines.total.total = tot
                    elif param == 'std.code.lines:total':
                        self.lines.total.total = avg
                        self.lines.total.average = tot
                    elif param == 'std.code.lines:code':
                        self.lines.code.average = avg
                        self.lines.code.total = tot
                    elif param == 'std.code.lines:comments':
                        self.lines.comments.average = avg
                        self.lines.comments.total = tot


class CPPcheck():
    def __init__(self):
        pass

    def parse(self, fn):
        # CPP check outputs in HTML. What do we want out of CPP check?
        # Is there an option to output in something other than HTML?
        # Skipping this parser for now...
        pass


class PyLint():
    def __init__(self, srcDir):
        self.files = []
        for dirpath, dirnames, filenames in os.walk(srcDir):
            for filename in [f for f in filenames if f.endswith(".py")]:
                self.files.append(os.path.join(dirpath, filename))

    def parse(self):
        with open(fn, 'r') as f:
            for l in f:
                print l


class Radon():
    def __init__(self):
        pass

    def parse(self, fn):
        d_tmp = {}
        d = {}
        file_key = None
        with open(fn, 'r') as f:
            for l in f:
                if ':' in l:
                    key, val = l.split(':')
                    key = key.strip().lower()
                    val = val.strip().lower()

                    try:
                        d_tmp[key] = int(val)
                    except:
                        pass

                    if key == 'blank':
                        d[file_key] = d_tmp
                        d_tmp = {}

                elif '.py\n' in l:
                    file_key = l[:-1]

                elif '** Total **' in l:
                    file_key = 'total'

        return d


# test/debug
if __name__ == '__main__':
    pl = PyLint()
    pl.parse('/home/sean/Desktop/temp.txt')<commit_msg>converted metrix parser output to dictionary format<commit_after>import re
import os


class MetrixPP:
    def __init__(self):
        pass

    def parse(self, fn):
        d = {}
        with open(fn, 'r') as f:
            for l in f:
                if ':: ' and "'"in l:
                    param = l.split("'")[1]
                elif 'Average' in l:
                    avg = float(re.findall(r"[-+]?\d*\.\d+|\d+", l.split(':')[-1].strip())[0])
                elif 'Total' in l:
                    tot = float(re.findall(r"[-+]?\d*\.\d+|\d+", l.split(':')[-1].strip())[0])

                    if param == 'std.code.magic:numbers':
                        key = 'std.code.magic:numbers'
                    elif param == 'std.general:size':
                        key = 'std.general:size'
                    elif param == 'std.code.todo:comments':
                        key = 'std.code.todo:comments'
                    elif param == 'std.code.complexity:cyclomatic':
                        key = 'std.code.complexity:cyclomatic'
                    elif param == 'std.code.length:total':
                        key = 'std.code.length:total'
                    elif param == 'std.code.lines:total':
                        key = 'std.code.lines:total'
                    elif param == 'std.code.lines:code':
                        key = 'std.code.lines:code'
                    elif param == 'std.code.lines:comments':
                        key = 'std.code.lines:comments'

                    d[key] = {}
                    d[key]['average'] = avg
                    d[key]['total'] = tot
        return d


class CPPcheck():
    def __init__(self):
        pass

    def parse(self, fn):
        # CPP check outputs in HTML. What do we want out of CPP check?
        # Is there an option to output in something other than HTML?
        # Skipping this parser for now...
        pass


class PyLint():
    def __init__(self, srcDir):
        self.files = []
        for dirpath, dirnames, filenames in os.walk(srcDir):
            for filename in [f for f in filenames if f.endswith(".py")]:
                self.files.append(os.path.join(dirpath, filename))

    def parse(self):
        with open(fn, 'r') as f:
            for l in f:
                print l


class Radon():
    def __init__(self):
        pass

    def parse(self, fn):
        d_tmp = {}
        d = {}
        file_key = None
        with open(fn, 'r') as f:
            for l in f:
                if ':' in l:
                    key, val = l.split(':')
                    key = key.strip().lower()
                    val = val.strip().lower()

                    try:
                        d_tmp[key] = int(val)
                    except:
                        pass

                    if key == 'blank':
                        d[file_key] = d_tmp
                        d_tmp = {}

                elif '.py\n' in l:
                    file_key = l[:-1]

                elif '** Total **' in l:
                    file_key = 'total'

        return d


# test/debug
if __name__ == '__main__':
    pl = PyLint()
    pl.parse('/home/sean/Desktop/temp.txt')
{'fraction_non_alphanumeric': 0.12570929725010913, 'fraction_numerical': 0.001745962461807071, 'mean_word_length': 1.148796498905908, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2799910', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1876, 'n_words': 487}