<commit_before>import _pytest
import pytest
import math
import errno
import re
import os
from os.path import expanduser, join
from operator import attrgetter, itemgetter
from collections import OrderedDict, namedtuple


def pytest_addoption(parser):
    parser.addoption("--interactive", "--ia", action="store_true",
                     dest='interactive',
                     help="enable iteractive selection of tests after"
                     " collection")


@pytest.hookimpl(trylast=True)
def pytest_collection_modifyitems(session, config, items):
    """called after collection has been performed, may filter or re-order
    the items in-place.
    """
    if not (config.option.interactive and items):
        return

    capman = config.pluginmanager.getplugin("capturemanager")
    if capman:
        capman.suspendcapture(in_=True)

    tr = config.pluginmanager.getplugin('terminalreporter')

    from .shell import PytestShellEmbed, SelectionMagics
    # prep a separate ipython history file
    fname = 'shell_history.sqlite'
    confdir = join(expanduser('~'), '.config', 'pytest_interactive')
    try:
        os.makedirs(confdir)
    except OSError as e:  # py2 compat
        if e.errno == errno.EEXIST:
            pass
        else:
            raise

    selection = FuncCollection()

    PytestShellEmbed.pytest_hist_file = join(confdir, fname)
    ipshell = PytestShellEmbed(banner1='Entering IPython shell...')
    ipshell.register_magics(SelectionMagics)
    # shell needs ref to curr selection
    ipshell.selection = selection

    # build a tree of test items
    tr.write_line("Building test tree...")
    # test tree needs ref to shell
    tt = TestTree(items, tr, ipshell, selection, config)

    intro = """Welcome to pytest-interactive, the pytest + IPython sensation!\n
Please explore the collected test tree using tt.<TAB>
HINT: when finished tabbing to a test node, simply __call__() it to have
pytest invoke all tests collected under that node."""

    user_ns = {
        'tt': tt,
        'shell': ipshell,
        'config': config,
        'session': session,
        '_selection': selection,
        'lastfailed': tt.get_cache_items(path='cache/lastfailed'),
    }

    # preload cached test sets
    for name, testnames in tt.get_cache_dict().items():
        user_ns[name] = tt.get_cache_items(key=name)

    # embed and block until user exits
    ipshell(intro, local_ns=user_ns)

    # submit final selection
    if selection:
        items[:] = list(selection.values())[:]
    else:
        items[:] = []


_root_ids = ('.', '')
_root_name = 'session'
Package = namedtuple('Package', 'name path node parent')


def gen_nodes(item, cache):
    '''generate all parent objs of this node up to the root/session
    '''
    path = ()
    # pytest node api - lists path items in order
    chain = item.listchain()
    for node in chain:
        try:
            name = node._obj.__name__
        except AttributeError as ae:
            # when either Instance or non-packaged module
            if isinstance(node, _pytest.python.Instance):
                # leave out Instances, later versions are going to drop them
                # anyway
                continue
            elif node.nodeid in _root_ids:
                name = _root_name
            else:  # XXX should never get here
                raise ae
        # packaged module
        if '.' in name and isinstance(node, _pytest.python.Module):
            # FIXME: this should be cwd dependent!!!
            # (i.e. don't add package objects we're below in the fs)
            prefix = tuple(name.split('.'))
            lpath = node.fspath
            fspath = str(lpath)
            # don't include the mod name in path
            for level in prefix[:-1]:
                name = '{}{}'.format(fspath[:fspath.index(level)], level)
                path += (level,)
                yield path, Package(name, lpath, node, node.parent)
            name = prefix[-1]  # this mod's name
        # func item
        elif isinstance(node, _pytest.python.Function):
            name = node.name
            if '[' in name:
                funcname = name.split('[')[0]
                try:
                    # TODO: look up the pf based on the vanilla func obj
                    # (should be an attr on the _pyfuncitem...)
                    pf = cache[path + (funcname,)]
                except KeyError:<commit_msg>Look for pytest.Item, not pytest.Function<commit_after>import pytest
import math
import errno
import re
import os
from os.path import expanduser, join
from operator import attrgetter, itemgetter
from collections import OrderedDict, namedtuple


def pytest_addoption(parser):
    parser.addoption("--interactive", "--ia", action="store_true",
                     dest='interactive',
                     help="enable iteractive selection of tests after"
                     " collection")


@pytest.hookimpl(trylast=True)
def pytest_collection_modifyitems(session, config, items):
    """called after collection has been performed, may filter or re-order
    the items in-place.
    """
    if not (config.option.interactive and items):
        return

    capman = config.pluginmanager.getplugin("capturemanager")
    if capman:
        capman.suspendcapture(in_=True)

    tr = config.pluginmanager.getplugin('terminalreporter')

    from .shell import PytestShellEmbed, SelectionMagics
    # prep a separate ipython history file
    fname = 'shell_history.sqlite'
    confdir = join(expanduser('~'), '.config', 'pytest_interactive')
    try:
        os.makedirs(confdir)
    except OSError as e:  # py2 compat
        if e.errno == errno.EEXIST:
            pass
        else:
            raise

    selection = FuncCollection()

    PytestShellEmbed.pytest_hist_file = join(confdir, fname)
    ipshell = PytestShellEmbed(banner1='Entering IPython shell...')
    ipshell.register_magics(SelectionMagics)
    # shell needs ref to curr selection
    ipshell.selection = selection

    # build a tree of test items
    tr.write_line("Building test tree...")
    # test tree needs ref to shell
    tt = TestTree(items, tr, ipshell, selection, config)

    intro = """Welcome to pytest-interactive, the pytest + IPython sensation!\n
Please explore the collected test tree using tt.<TAB>
HINT: when finished tabbing to a test node, simply __call__() it to have
pytest invoke all tests collected under that node."""

    user_ns = {
        'tt': tt,
        'shell': ipshell,
        'config': config,
        'session': session,
        '_selection': selection,
        'lastfailed': tt.get_cache_items(path='cache/lastfailed'),
    }

    # preload cached test sets
    for name, testnames in tt.get_cache_dict().items():
        user_ns[name] = tt.get_cache_items(key=name)

    # embed and block until user exits
    ipshell(intro, local_ns=user_ns)

    # submit final selection
    if selection:
        items[:] = list(selection.values())[:]
    else:
        items[:] = []


_root_ids = ('.', '')
_root_name = 'session'
Package = namedtuple('Package', 'name path node parent')


def gen_nodes(item, cache):
    '''generate all parent objs of this node up to the root/session
    '''
    path = ()
    # pytest node api - lists path items in order
    chain = item.listchain()
    for node in chain:
        try:
            name = node._obj.__name__
        except AttributeError as ae:
            # when either Instance or non-packaged module
            if isinstance(node, pytest.Instance):
                # leave out Instances, later versions are going to drop them
                # anyway
                continue
            elif node.nodeid in _root_ids:
                name = _root_name
            else:  # XXX should never get here
                raise ae
        # packaged module
        if '.' in name and isinstance(node, pytest.Module):
            # FIXME: this should be cwd dependent!!!
            # (i.e. don't add package objects we're below in the fs)
            prefix = tuple(name.split('.'))
            lpath = node.fspath
            fspath = str(lpath)
            # don't include the mod name in path
            for level in prefix[:-1]:
                name = '{}{}'.format(fspath[:fspath.index(level)], level)
                path += (level,)
                yield path, Package(name, lpath, node, node.parent)
            name = prefix[-1]  # this mod's name
        # func item
        elif isinstance(node, pytest.Item):
            name = node.name
            if '[' in name:
                funcname = name.split('[')[0]
                try:
                    # TODO: look up the pf based on the vanilla func obj
                    # (should be an attr on the _pyfuncitem...)
                    pf = cache[path + (funcname,)]
                except KeyError:
{'fraction_non_alphanumeric': 0.09706980427650187, 'fraction_numerical': 0.0011313497001923295, 'mean_word_length': 2.2122093023255816, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '620400', 'n_tokens_mistral': 2526, 'n_tokens_neox': 2460, 'n_words': 785}