<commit_before>import collections

from git_code_debt.diff_parser_base import DiffParserBase
from git_code_debt.metric import Metric
from git_code_debt.metrics.common import FILE_TYPE_MAP
from git_code_debt_util.path import split_file_path


def is_python_import(line):
    line = line.lstrip()
    if line.startswith('import'):
        return True
    if line.startswith('from') and 'import' in line:
        return True
    return False

def is_template_import(line):
    line = line.lstrip()
    return (
        line.startswith('#') and
        is_python_import(line[1:])
    )

# Maps a set of file extensions to a function that determines if the line is an import.
IMPORT_CHECK_MAP = {
    '.py': is_python_import,
    '.tmpl': is_template_import,
}


class ImportsParser(DiffParserBase):
    """Counts number of imports in a repository by file type."""

    def get_metrics_from_stat(self, file_diff_stats):
        imports_by_extension = collections.defaultdict(int)

        for file_diff_stat in file_diff_stats:
            import_count = 0

            _, _, extension = split_file_path(file_diff_stat.filename)
            is_import_line = IMPORT_CHECK_MAP.get(extension, None)
            if is_import_line:
                for line in file_diff_stat.lines_added:
                    import_count += 1 if is_import_line(line) else 0
                for line in file_diff_stat.lines_removed:
                    import_count -= 1 if is_import_line(line) else 0

            imports_by_extension[extension] += import_count

        for extension in IMPORT_CHECK_MAP.keys():
            imports_changed = imports_by_extension.get(extension, 0)
            file_type = FILE_TYPE_MAP.get(extension, 'unknown')
            yield Metric('ImportCount_{0}'.format(file_type), imports_changed)

    def get_possible_metric_ids(self):
        return [
            'ImportCount_{0}'.format(file_type)
            for file_type in set(FILE_TYPE_MAP.values()) | set(['unknown'])
        ]<commit_msg>Import metric only reports for python and templates.<commit_after>import collections

from git_code_debt.diff_parser_base import DiffParserBase
from git_code_debt.metric import Metric
from git_code_debt.metrics.common import FILE_TYPE_MAP
from git_code_debt_util.path import split_file_path


def is_python_import(line):
    line = line.lstrip()
    if line.startswith('import'):
        return True
    if line.startswith('from') and 'import' in line:
        return True
    return False

def is_template_import(line):
    line = line.lstrip()
    return (
        line.startswith('#') and
        is_python_import(line[1:])
    )

# Maps a set of file extensions to a function that determines if the line is
# an import.
IMPORT_CHECK_MAP = {
    '.py': is_python_import,
    '.tmpl': is_template_import,
}


class ImportsParser(DiffParserBase):
    """Counts number of imports in a repository by file type."""

    def get_metrics_from_stat(self, file_diff_stats):
        imports_by_extension = collections.defaultdict(int)

        for file_diff_stat in file_diff_stats:
            import_count = 0

            _, _, extension = split_file_path(file_diff_stat.filename)
            is_import_line = IMPORT_CHECK_MAP.get(extension, None)
            if is_import_line:
                for line in file_diff_stat.lines_added:
                    import_count += 1 if is_import_line(line) else 0
                for line in file_diff_stat.lines_removed:
                    import_count -= 1 if is_import_line(line) else 0

            imports_by_extension[extension] += import_count

        for extension in IMPORT_CHECK_MAP.keys():
            imports_changed = imports_by_extension.get(extension, 0)
            file_type = FILE_TYPE_MAP.get(extension, 'unknown')
            yield Metric('ImportCount_{0}'.format(file_type), imports_changed)

    def get_possible_metric_ids(self):
        return [
            'ImportCount_{0}'.format(file_type)
            for file_type in set(IMPORT_CHECK_MAP.values()) | set(['unknown'])
        ]
{'fraction_non_alphanumeric': 0.08061325420375866, 'fraction_numerical': 0.004451038575667656, 'mean_word_length': 2.911992263056093, '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': '3375669', 'n_tokens_mistral': 1218, 'n_tokens_neox': 1181, 'n_words': 287}