<commit_before>"""Python package version query tools."""

import logging
import json
import pathlib

from .version import Version

_LOG = logging.getLogger(__name__)


def query_metadata_json(path: pathlib.Path) -> Version:
    """Get version from metadata.json file."""
    with open(str(path), 'r') as metadata_file:
        metadata = json.load(metadata_file)
    version_str = metadata['version']
    return Version.from_str(version_str)


def query_pkg_info(path: pathlib.Path) -> Version:
    """Get version from PKG-INFO file."""
    with open(str(path), 'r') as pkginfo_file:
        for line in pkginfo_file:
            if line.startswith('Version:'):
                version_str = line.replace('Version:', '').strip()
                return Version.from_str(version_str)
    raise ValueError(path)


def query_package_folder(path: pathlib.Path, search_parent_directories: bool = False) -> Version:
    """Get version from Python package folder."""
    paths = [path] + (list(path.parents) if search_parent_directories else [])
    metadata_json_paths, pkg_info_paths = None, None
    for pth in paths:
        metadata_json_paths = list(pth.parent.glob('{}*.dist-info/metadata.json'.format(pth.name)))
        pkg_info_paths = list(pth.parent.glob('{}*.egg-info/PKG-INFO'.format(pth.name)))
        if len(metadata_json_paths) == 1 and not pkg_info_paths:
            return query_metadata_json(metadata_json_paths[0])
        if not metadata_json_paths and len(pkg_info_paths) == 1:
            return query_pkg_info(pkg_info_paths[0])
    raise ValueError(paths, metadata_json_paths, pkg_info_paths)
<commit_msg>also check for METADATA file from dist-info folder<commit_after>"""Python package version query tools."""

import logging
import json
import pathlib

from .version import Version

_LOG = logging.getLogger(__name__)


def query_metadata_json(path: pathlib.Path) -> Version:
    """Get version from metadata.json file."""
    with open(str(path), 'r') as metadata_file:
        metadata = json.load(metadata_file)
    version_str = metadata['version']
    return Version.from_str(version_str)


def query_pkg_info(path: pathlib.Path) -> Version:
    """Get version from PKG-INFO file."""
    with open(str(path), 'r') as pkginfo_file:
        for line in pkginfo_file:
            if line.startswith('Version:'):
                version_str = line.replace('Version:', '').strip()
                return Version.from_str(version_str)
    raise ValueError(path)


def query_package_folder(path: pathlib.Path, search_parent_directories: bool = False) -> Version:
    """Get version from Python package folder."""
    paths = [path] + (list(path.parents) if search_parent_directories else [])
    metadata_json_paths, pkg_info_paths = None, None
    for pth in paths:
        metadata_json_paths = list(pth.parent.glob('{}*.dist-info/metadata.json'.format(pth.name)))
        pkg_info_paths = list(pth.parent.glob('{}*.egg-info/PKG-INFO'.format(pth.name)))
        pkg_info_paths += list(pth.parent.glob('{}*.dist-info/METADATA'.format(pth.name)))
        if len(metadata_json_paths) == 1 and not pkg_info_paths:
            return query_metadata_json(metadata_json_paths[0])
        if not metadata_json_paths and len(pkg_info_paths) == 1:
            return query_pkg_info(pkg_info_paths[0])
    raise ValueError(paths, metadata_json_paths, pkg_info_paths)

{'fraction_non_alphanumeric': 0.12048549437537004, 'fraction_numerical': 0.002368265245707519, 'mean_word_length': 3.765867418899859, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1043729', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1029, 'n_words': 239}