<commit_before>"""
Distribution
============

Module responsible for building the pywincffi distribution
in ``setup.py``.  This module is meant to serve two
purposes.  The first is to serve as the main means of loading
the pywincffi library:

>>> from pywincffi.core import dist
>>> ffi, library = dist.load()

The second is to facilitate a means of building a static
library.  This is used by the setup.py during the install
process to build and install pywincffi as well as a wheel
for distribution.

The reason for this setup is so that pywincffi can handle
the underlying load process inline. This makes it easier to
test as well as perform some additional testing/development
on non-windows platforms.
"""

import os
from collections import namedtuple
from os.path import join, isfile
from pkg_resources import resource_filename

from cffi import FFI

from pywincffi.core.logger import get_logger
from pywincffi.exceptions import ResourceNotFoundError


logger = get_logger("core.dist")
InlineModule = namedtuple("InlineModule", ("ffi", "library"))


def get_filepath(root, filename):
    """
    Returns the filepath to the requested header or source
    file.

    :param str root:
        The root directory under ``pywincffi/core/cdefs``. 'headers'
        for example would be a valid entry here.

    :param str filename:
        The name of the file you want to retrieve under ``root``.

    :raises ResourceNotFoundError:
        Raised if the function could not find the requested file.
    """
    logger.debug("get_filepath(%r, %r)", root, filename)
    path = resource_filename(
        "pywincffi", join("core", "cdefs", root, filename))

    if not isfile(path):
        raise ResourceNotFoundError(
            "Failed to locate %s/%s" % (root, filename))

    return path


class Distribution(object):
    """
    A class responsible for building, caching and returning
    the built artifacts for :mod:`pywincffi`

    :ivar str MODULE_NAME:
        The name of the module to pass to :meth:`FFI.set_source`. This
        will eventually end up becoming the name of the underlying C-module
        that is built.

    :ivar tuple HEADERS:
        A tuple containing all the headers used to build pywincffi.

    :ivar tuple SOURCES:
        A tuple containing all the C source files to build pywincffi.

    :ivar tuple LIBRARIES:
        A tuple of Windows library names that :meth:`FFI.verify` should
        use.
    """
    MODULE_NAME = "_pywincffi"
    HEADERS = (
        get_filepath("headers", "constants.h"),
        get_filepath("headers", "structs.h"),
        get_filepath("headers", "functions.h")
    )
    SOURCES = (get_filepath("sources", "main.c"), )
    LIBRARIES = ("kernel32", )

    # Attributes used internally by this class for caching.
    _pywincffi = None

    @classmethod
    def load_definitions(cls, cached=True):
        """
        Reads in the headers and source files and produces
        a tuple of strings with the results.

        :param bool cached:
            If True, return precached versions of the headers
            and source.

        :rtype: tuple
        :return:
            Returns a tuple of strings containing the headers
            and sources.
        """
        header = ""
        source = ""

        for path in cls.HEADERS:
            logger.debug("Reading %s", path)
            with open(path, "r") as file_:
                header += file_.read()

        for path in cls.SOURCES:
            logger.debug("Reading %s", path)
            with open(path, "r") as file_:
                source += file_.read()

        return header, source
<commit_msg>removing now unused cached keyword<commit_after>"""
Distribution
============

Module responsible for building the pywincffi distribution
in ``setup.py``.  This module is meant to serve two
purposes.  The first is to serve as the main means of loading
the pywincffi library:

>>> from pywincffi.core import dist
>>> ffi, library = dist.load()

The second is to facilitate a means of building a static
library.  This is used by the setup.py during the install
process to build and install pywincffi as well as a wheel
for distribution.

The reason for this setup is so that pywincffi can handle
the underlying load process inline. This makes it easier to
test as well as perform some additional testing/development
on non-windows platforms.
"""

import os
from collections import namedtuple
from os.path import join, isfile
from pkg_resources import resource_filename

from cffi import FFI

from pywincffi.core.logger import get_logger
from pywincffi.exceptions import ResourceNotFoundError


logger = get_logger("core.dist")
InlineModule = namedtuple("InlineModule", ("ffi", "library"))


def get_filepath(root, filename):
    """
    Returns the filepath to the requested header or source
    file.

    :param str root:
        The root directory under ``pywincffi/core/cdefs``. 'headers'
        for example would be a valid entry here.

    :param str filename:
        The name of the file you want to retrieve under ``root``.

    :raises ResourceNotFoundError:
        Raised if the function could not find the requested file.
    """
    logger.debug("get_filepath(%r, %r)", root, filename)
    path = resource_filename(
        "pywincffi", join("core", "cdefs", root, filename))

    if not isfile(path):
        raise ResourceNotFoundError(
            "Failed to locate %s/%s" % (root, filename))

    return path


class Distribution(object):
    """
    A class responsible for building, caching and returning
    the built artifacts for :mod:`pywincffi`

    :ivar str MODULE_NAME:
        The name of the module to pass to :meth:`FFI.set_source`. This
        will eventually end up becoming the name of the underlying C-module
        that is built.

    :ivar tuple HEADERS:
        A tuple containing all the headers used to build pywincffi.

    :ivar tuple SOURCES:
        A tuple containing all the C source files to build pywincffi.

    :ivar tuple LIBRARIES:
        A tuple of Windows library names that :meth:`FFI.verify` should
        use.
    """
    MODULE_NAME = "_pywincffi"
    HEADERS = (
        get_filepath("headers", "constants.h"),
        get_filepath("headers", "structs.h"),
        get_filepath("headers", "functions.h")
    )
    SOURCES = (get_filepath("sources", "main.c"), )
    LIBRARIES = ("kernel32", )

    # Attributes used internally by this class for caching.
    _pywincffi = None

    @classmethod
    def load_definitions(cls):
        """
        Reads in the headers and source files and produces
        a tuple of strings with the results.

        :rtype: tuple
        :return:
            Returns a tuple of strings containing the headers
            and sources.
        """
        header = ""
        source = ""

        for path in cls.HEADERS:
            logger.debug("Reading %s", path)
            with open(path, "r") as file_:
                header += file_.read()

        for path in cls.SOURCES:
            logger.debug("Reading %s", path)
            with open(path, "r") as file_:
                source += file_.read()

        return header, source

{'fraction_non_alphanumeric': 0.0844363940365055, 'fraction_numerical': 0.0005573359342343597, 'mean_word_length': 2.701908200103146, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '2812543', 'n_tokens_mistral': 2049, 'n_tokens_neox': 1967, 'n_words': 798}