<commit_before># -*- coding: utf-8 -*-
"""
Helpful decorators for module writing
"""

# Import python libs
from __future__ import absolute_import, print_function, unicode_literals

import errno
import inspect
import logging
import subprocess
import sys
import time
from collections import defaultdict
from functools import wraps

# Import salt libs
import salt.utils.args
import salt.utils.data
import salt.utils.versions
from salt.exceptions import CommandExecutionError, SaltConfigurationError

# Import 3rd-party libs
from salt.ext import six
from salt.log import LOG_LEVELS

IS_WINDOWS = False
if getattr(sys, "getwindowsversion", False):
    IS_WINDOWS = True

log = logging.getLogger(__name__)


class Depends(object):
    """
    This decorator will check the module when it is loaded and check that the
    dependencies passed in are in the globals of the module. If not, it will
    cause the function to be unloaded (or replaced).
    """

    # kind -> Dependency -> list of things that depend on it
    dependency_dict = defaultdict(lambda: defaultdict(dict))

    def __init__(self, *dependencies, **kwargs):
        """
        The decorator is instantiated with a list of dependencies (string of
        global name)

        An example use of this would be:

        .. code-block:: python

            @depends('modulename')
            def test():
                return 'foo'

            OR

            @depends('modulename', fallback_function=function)
            def test():
                return 'foo'

        .. code-block:: python

        This can also be done with the retcode of a command, using the
        ``retcode`` argument:

            @depends('/opt/bin/check_cmd', retcode=0)
            def test():
                return 'foo'

        It is also possible to check for any nonzero retcode using the
        ``nonzero_retcode`` argument:

            @depends('/opt/bin/check_cmd', nonzero_retcode=True)
            def test():
                return 'foo'

        .. note::
            The command must be formatted as a string, not a list of args.
            Additionally, I/O redirection and other shell-specific syntax are
            not supported since this uses shell=False when calling
            subprocess.Popen().

        """
        log.trace(
            "Depends decorator instantiated with dep list of %s and kwargs %s",
            dependencies,
            kwargs,
        )
        self.dependencies = dependencies
        self.params = kwargs

    def __call__(self, function):
        """
        The decorator is "__call__"d with the function, we take that function
        and determine which module and function name it is to store in the
        class wide dependency_dict
        """
        try:
            # This inspect call may fail under certain conditions in the loader.
            # Possibly related to a Python bug here:
            # http://bugs.python.org/issue17735
            frame = inspect.stack()[1][0]
            # due to missing *.py files under esky we cannot use inspect.getmodule
            # module name is something like salt.loaded.int.modules.test
            _, kind, mod_name = frame.f_globals["__name__"].rsplit(".", 2)
            fun_name = function.__name__
            for dep in self.dependencies:
                self.dependency_dict[kind][dep][(mod_name, fun_name)] = (
                    frame,
                    self.params,
                )
        except Exception as exc:  # pylint: disable=broad-except
            log.exception(
                "Exception encountered when attempting to inspect frame in "
                "dependency decorator"
            )
        return function

    @staticmethod
    def run_command(dependency, mod_name, func_name):
        full_name = "{0}.{1}".format(mod_name, func_name)
        log.trace("Running '%s' for '%s'", dependency, full_name)<commit_msg>Only inspect one frame in depends decorator<commit_after># -*- coding: utf-8 -*-
"""
Helpful decorators for module writing
"""

# Import python libs
from __future__ import absolute_import, print_function, unicode_literals

import errno
import inspect
import logging
import subprocess
import sys
import time
from collections import defaultdict
from functools import wraps

# Import salt libs
import salt.utils.args
import salt.utils.data
import salt.utils.versions
from salt.exceptions import CommandExecutionError, SaltConfigurationError

# Import 3rd-party libs
from salt.ext import six
from salt.log import LOG_LEVELS

IS_WINDOWS = False
if getattr(sys, "getwindowsversion", False):
    IS_WINDOWS = True

log = logging.getLogger(__name__)


class Depends(object):
    """
    This decorator will check the module when it is loaded and check that the
    dependencies passed in are in the globals of the module. If not, it will
    cause the function to be unloaded (or replaced).
    """

    # kind -> Dependency -> list of things that depend on it
    dependency_dict = defaultdict(lambda: defaultdict(dict))

    def __init__(self, *dependencies, **kwargs):
        """
        The decorator is instantiated with a list of dependencies (string of
        global name)

        An example use of this would be:

        .. code-block:: python

            @depends('modulename')
            def test():
                return 'foo'

            OR

            @depends('modulename', fallback_function=function)
            def test():
                return 'foo'

        .. code-block:: python

        This can also be done with the retcode of a command, using the
        ``retcode`` argument:

            @depends('/opt/bin/check_cmd', retcode=0)
            def test():
                return 'foo'

        It is also possible to check for any nonzero retcode using the
        ``nonzero_retcode`` argument:

            @depends('/opt/bin/check_cmd', nonzero_retcode=True)
            def test():
                return 'foo'

        .. note::
            The command must be formatted as a string, not a list of args.
            Additionally, I/O redirection and other shell-specific syntax are
            not supported since this uses shell=False when calling
            subprocess.Popen().

        """
        log.trace(
            "Depends decorator instantiated with dep list of %s and kwargs %s",
            dependencies,
            kwargs,
        )
        self.dependencies = dependencies
        self.params = kwargs

    def __call__(self, function):
        """
        The decorator is "__call__"d with the function, we take that function
        and determine which module and function name it is to store in the
        class wide dependency_dict
        """
        try:
            # This inspect call may fail under certain conditions in the loader.
            # Possibly related to a Python bug here:
            # http://bugs.python.org/issue17735
            frame = inspect.currentframe().f_back
            # due to missing *.py files under esky we cannot use inspect.getmodule
            # module name is something like salt.loaded.int.modules.test
            _, kind, mod_name = frame.f_globals["__name__"].rsplit(".", 2)
            fun_name = function.__name__
            for dep in self.dependencies:
                self.dependency_dict[kind][dep][(mod_name, fun_name)] = (
                    frame,
                    self.params,
                )
        except Exception as exc:  # pylint: disable=broad-except
            log.exception(
                "Exception encountered when attempting to inspect frame in "
                "dependency decorator"
            )
        return function

    @staticmethod
    def run_command(dependency, mod_name, func_name):
        full_name = "{0}.{1}".format(mod_name, func_name)
        log.trace("Running '%s' for '%s'", dependency, full_name)
{'fraction_non_alphanumeric': 0.07847875858560163, 'fraction_numerical': 0.0030526583566522513, 'mean_word_length': 2.1376695929768554, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '226566', 'n_tokens_mistral': 2060, 'n_tokens_neox': 1966, 'n_words': 743}