<commit_before>"""Functions and helpers related to guessing configuration attributes."""


import os
import re
from collections import namedtuple
from collections import OrderedDict

from .constants import INPUT


def python_modules():
    """Determine if there are python modules in the cwd.

    Returns:
        list of python modules as strings
    """

    ignored = ["setup.py", "conftest.py"]

    py_modules = []
    for file_ in os.listdir(os.path.abspath(os.curdir)):
        if file_ in ignored or not os.path.isfile(file_):
            continue

        file_name, file_ext = os.path.splitext(file_)
        if file_ext == ".py":
            py_modules.append(file_name)

    return sorted(py_modules)


def potential_data_files(scripts):
    """Determine if there are any potential data files down from cwd.

    Tries to ignore normal things that would pop up you'd like not to include.

    Args:
        scripts: list of known script exectuables

    Returns:
        list of files as relative paths down from cwd
    """

    potential_files = []

    for root, directories, files in os.walk(os.curdir):
        if _ignored(root, is_file=False):
            continue

        for file_ in files:
            if not _ignored(file_):
                relative_path = os.path.join(root, file_)
                while relative_path[0] in (os.curdir, os.path.sep):
                    relative_path = relative_path[1:]
                if relative_path not in scripts:
                    potential_files.append(relative_path)

    return potential_files


def latest_git_tag():
    """Returns the latest tag from git, if .git exists."""

    def git_tag_refs(file_=""):
        return os.path.join(".git", "refs", "tags", file_)

    try:
        git_tags = os.listdir(git_tag_refs())
    except OSError:
        return None

    latest_tag = {0: None}
    for tag in git_tags:
        latest_tag[os.stat(git_tag_refs(tag)).st_ctime] = tag

    return latest_tag[max(latest_tag)]


def find_in_files():
    """Look through most files to try to determine the version and author.

    Returns:
        OrderedDict with the some/all of the following keys if found::

            version, author, author_email, maintainer, maintainer_email
    """

    Guess = namedtuple("Guess", ("source", "weight", "guess"))
    Pattern = namedtuple("Pattern", ("name", "weight", "pattern"))

    git_tag = latest_git_tag()
    versions = [Guess("git tag", 99 * (git_tag is not None), git_tag)]

    authors = []
    emails = []
    maintainers = []
    maintainer_emails = []

    to_find = {
        "version": versions,
        "author": authors,
        "email": emails,
        "author_email": emails,
        "maintainer": maintainers,
        "maintainer_email": maintainer_emails,
    }

    file_weights = {  # additional to the pattern weights
        "__init__.py": 100,
        "__version__.py": 125,
        "version.py": 75,
    }

    for root, directories, files in os.walk(os.curdir):

        if _ignored(root, is_file=False):
            continue

        for file_ in files:<commit_msg>consistent order of guessed attributes from files<commit_after>"""Functions and helpers related to guessing configuration attributes."""


import os
import re
from collections import namedtuple
from collections import OrderedDict

from .constants import INPUT


def python_modules():
    """Determine if there are python modules in the cwd.

    Returns:
        list of python modules as strings
    """

    ignored = ["setup.py", "conftest.py"]

    py_modules = []
    for file_ in os.listdir(os.path.abspath(os.curdir)):
        if file_ in ignored or not os.path.isfile(file_):
            continue

        file_name, file_ext = os.path.splitext(file_)
        if file_ext == ".py":
            py_modules.append(file_name)

    return sorted(py_modules)


def potential_data_files(scripts):
    """Determine if there are any potential data files down from cwd.

    Tries to ignore normal things that would pop up you'd like not to include.

    Args:
        scripts: list of known script exectuables

    Returns:
        list of files as relative paths down from cwd
    """

    potential_files = []

    for root, directories, files in os.walk(os.curdir):
        if _ignored(root, is_file=False):
            continue

        for file_ in files:
            if not _ignored(file_):
                relative_path = os.path.join(root, file_)
                while relative_path[0] in (os.curdir, os.path.sep):
                    relative_path = relative_path[1:]
                if relative_path not in scripts:
                    potential_files.append(relative_path)

    return potential_files


def latest_git_tag():
    """Returns the latest tag from git, if .git exists."""

    def git_tag_refs(file_=""):
        return os.path.join(".git", "refs", "tags", file_)

    try:
        git_tags = os.listdir(git_tag_refs())
    except OSError:
        return None

    latest_tag = {0: None}
    for tag in git_tags:
        latest_tag[os.stat(git_tag_refs(tag)).st_ctime] = tag

    return latest_tag[max(latest_tag)]


def find_in_files():
    """Look through most files to try to determine the version and author.

    Returns:
        OrderedDict with the some/all of the following keys if found::

            version, author, author_email, maintainer, maintainer_email
    """

    Guess = namedtuple("Guess", ("source", "weight", "guess"))
    Pattern = namedtuple("Pattern", ("name", "weight", "pattern"))

    git_tag = latest_git_tag()
    versions = [Guess("git tag", 99 * (git_tag is not None), git_tag)]

    authors = []
    emails = []
    maintainers = []
    maintainer_emails = []

    to_find = OrderedDict([
        ("version", versions),
        ("author", authors),
        ("email", emails),
        ("author_email", emails),
        ("maintainer", maintainers),
        ("maintainer_email", maintainer_emails),
    ])

    file_weights = {  # additional to the pattern weights
        "__init__.py": 100,
        "__version__.py": 125,
        "version.py": 75,
    }

    for root, directories, files in os.walk(os.curdir):

        if _ignored(root, is_file=False):
            continue

        for file_ in files:
{'fraction_non_alphanumeric': 0.10353091548170634, 'fraction_numerical': 0.004154018213772168, 'mean_word_length': 2.4913552704963746, 'pattern_counts': {'":': 14, '<': 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': '3671423', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1795, 'n_words': 560}