<commit_before>import inspect
import sys

from fields import Fields

from hunter.actions import Action
from hunter.actions import CodePrinter
from hunter.actions import Debugger
from hunter.actions import VarsDumper


__version__ = "0.1.0"


class Tracer(Fields.predicate):
    """
    Trace object.

    """

    def __init__(self):
        self.predicate = self.not_started

    def not_started(self, _):
        raise RuntimeError("Tracer is not started.")

    def __call__(self, frame, kind, arg):
        """
        The settrace function.

        .. note::

            This always returns self (drills down) - as opposed to only drilling down when predicate(event) is True because it might
            match further inside.
        """
        self.predicate(Event(frame, kind, arg))
        return self

    def trace(self, *predicates, **options):
        """
        Starts tracing. Can be used as a context manager (with slightly incorrect semantics - it starts tracing before ``__enter__`` is
        called).
        """
        if "action" not in options:
            options["action"] = CodePrinter()
        self.predicate = F(*predicates, **options)

        sys.settrace(self)
        return self

    def stop(self):
        sys.settrace(None)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.stop()

_tracer = Tracer()
trace = _tracer.trace
stop = _tracer.stop


class CachedProperty(object):
    def __init__(self, func):
        self.func = func

    def __get__(self, obj, cls):
        value = obj.__dict__[self.func.__name__] = self.func(obj)
        return value


class Event(object):
    """
    Event wrapper for ``frame, kind, arg`` (the arguments the settrace function gets).

    Provides few convenience properties.
    """
<commit_msg>Allow a pre-existin tracer to also run.<commit_after>import inspect
import sys

from fields import Fields

from hunter.actions import Action
from hunter.actions import CodePrinter
from hunter.actions import Debugger
from hunter.actions import VarsDumper


__version__ = "0.1.0"


class Tracer(Fields.predicate):
    """
    Trace object.

    """

    def __init__(self):
        self._current_predicate = self.not_started
        self._previous_tracer = None

    def not_started(self, _):
        raise RuntimeError("Tracer is not started.")

    def __call__(self, frame, kind, arg):
        """
        The settrace function.

        .. note::

            This always returns self (drills down) - as opposed to only drilling down when predicate(event) is True because it might
            match further inside.
        """
        self.predicate(Event(frame, kind, arg))
        if self._previous_tracer:
            self._previous_tracer(frame, kind, arg)
        return self

    def trace(self, *predicates, **options):
        """
        Starts tracing. Can be used as a context manager (with slightly incorrect semantics - it starts tracing before ``__enter__`` is
        called).
        """
        if "action" not in options:
            options["action"] = CodePrinter()
        predicate = F(*predicates, **options)

        previous_tracer = sys.gettrace()
        if previous_tracer is self:
            self._current_predicate |= predicate
        else:
            self._previous_tracer = previous_tracer
            sys.settrace(self)
        return self

    def stop(self):
        sys.settrace(self._previous_tracer)
        self._previous_tracer = None

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.stop()

_tracer = Tracer()
trace = _tracer.trace
stop = _tracer.stop


class CachedProperty(object):
    def __init__(self, func):
        self.func = func

    def __get__(self, obj, cls):
        value = obj.__dict__[self.func.__name__] = self.func(obj)
        return value


class Event(object):
    """
    Event wrapper for ``frame, kind, arg`` (the arguments the settrace function gets).

    Provides few convenience properties.
    """

{'fraction_non_alphanumeric': 0.09437208159252888, 'fraction_numerical': 0.0014745637748832638, 'mean_word_length': 2.4697357203751067, 'pattern_counts': {'":': 0, '<': 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': '80047', 'n_tokens_mistral': 1179, 'n_tokens_neox': 1122, 'n_words': 360}