<commit_before>"""

This module provides an interface to the mechanisms
used to monitor Python module file modifications.

    :copyright: 2008 by <NAME> <<EMAIL>>
    :license: MIT (See ``LICENSE`` file for more details)

"""


import os
from errno import ENOENT
import logging
import time
from os.path import splitext

from modipyd import LOGGER
from modipyd import utils
from modipyd.module import read_module_code, \
                           collect_python_module_file
from modipyd.resolve import ModuleNameResolver
from modipyd.descriptor import ModuleDescriptor
from modipyd.utils.decorators import require


class Monitor(object):
    """
    This class provides an interface to the mechanisms
    used to monitor Python module file modifications.
    """

    def __init__(self, filepath_or_list, search_path=None):
        from modipyd.resolve import normalize_path

        super(Monitor, self).__init__()
        self.search_path = search_path

        # paths will be used as dictionary key,
        # so make it normalized.
        paths = utils.wrap_sequence(filepath_or_list)
        self.paths = [normalize_path(i) for i in paths]
        assert not isinstance(self.paths, basestring)

        self.monitoring = False
        self.__descriptors = None
        self.__filenames = None
        self.__failures = None

    @property
    def descriptors(self):
        if self.__descriptors is None:
            self.__descriptors = {}
            self.__filenames = {}
            self.__failures = set()
            entries = list(self.refresh())
            LOGGER.debug("%d descriptoes" % len(entries))
        return self.__descriptors

    @require(descriptor=ModuleDescriptor)
    def remove(self, descriptor):
        filename = splitext(descriptor.filename)[0]
        descriptors, filenames = self.__descriptors, self.__filenames

        if (descriptor.name not in descriptors or 
                filename not in filenames):
            raise KeyError(
                "No monitoring descriptor '%s'" % \
                descriptor.name)

        LOGGER.debug("Removed: %s" % descriptor.describe())
        descriptor.clear_dependencies()
        del descriptors[descriptor.name]
        del filenames[filename]

    def refresh(self):
        assert isinstance(self.paths, (tuple, list))
        assert isinstance(self.__descriptors, dict)
        assert isinstance(self.__filenames, dict)
        assert isinstance(self.__failures, set)

        # short variable names
        descriptors = self.__descriptors
        filenames = self.__filenames
        failures = self.__failures<commit_msg>comment<commit_after>"""

This module provides an interface to the mechanisms
used to monitor Python module file modifications.

    :copyright: 2008 by <NAME> <<EMAIL>>
    :license: MIT (See ``LICENSE`` file for more details)

"""


import os
from errno import ENOENT
import logging
import time
from os.path import splitext

from modipyd import LOGGER
from modipyd import utils
from modipyd.module import read_module_code, \
                           collect_python_module_file
from modipyd.resolve import ModuleNameResolver
from modipyd.descriptor import ModuleDescriptor
from modipyd.utils.decorators import require


class Monitor(object):
    """
    This class provides an interface to the mechanisms
    used to monitor Python module file modifications.
    """

    def __init__(self, filepath_or_list, search_path=None):
        from modipyd.resolve import normalize_path

        super(Monitor, self).__init__()
        self.search_path = search_path

        # paths will be used as dictionary key,
        # so make it normalized.
        paths = utils.wrap_sequence(filepath_or_list)
        self.paths = [normalize_path(i) for i in paths]
        assert not isinstance(self.paths, basestring)

        self.monitoring = False
        self.__descriptors = None
        self.__filenames = None
        self.__failures = None

    @property
    def descriptors(self):
        if self.__descriptors is None:
            self.__descriptors = {}
            self.__filenames = {}
            self.__failures = set()
            entries = list(self.refresh())
            LOGGER.debug("%d descriptoes" % len(entries))
        return self.__descriptors

    @require(descriptor=ModuleDescriptor)
    def remove(self, descriptor):
        filename = splitext(descriptor.filename)[0]
        descriptors, filenames = self.__descriptors, self.__filenames

        if (descriptor.name not in descriptors or 
                filename not in filenames):
            raise KeyError(
                "No monitoring descriptor '%s'" % \
                descriptor.name)

        LOGGER.debug("Removed: %s" % descriptor.describe())
        descriptor.clear_dependencies()
        del descriptors[descriptor.name]
        del filenames[filename]

    def refresh(self):
        assert isinstance(self.paths, (tuple, list))
        assert isinstance(self.__descriptors, dict)
        assert isinstance(self.__filenames, dict)
        assert isinstance(self.__failures, set)

        # localize variable access to minimize overhead
        # and to reduce the visual noise.
        descriptors = self.__descriptors
        filenames = self.__filenames
        failures = self.__failures
{'fraction_non_alphanumeric': 0.07583474816072439, 'fraction_numerical': 0.0018864365214110545, 'mean_word_length': 2.616643929058663, 'pattern_counts': {'":': 0, '<': 9, '<?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': '1955717', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1370, 'n_words': 418}