Example #1
0
 def testDeprecationWarnings(self):
     ep = EntryPoint(
         "foo", "pkg_resources.tests.test_resources", ["TestEntryPoints"],
         ["x"]
     )
     with pytest.warns(pkg_resources.PkgResourcesDeprecationWarning):
         ep.load(require=False)
Example #2
0
def make_errorlog(app, global_conf, **local_conf):
    """Paste filterapp factory.
    """
    channel = local_conf.get('channel', None)
    keep = int(local_conf.get('keep', 20))
    path = local_conf.get('path', '/__error_log__')
    ignore = local_conf.get('ignore', None)
    # e.g. Paste.httpexceptions.HTTPFound,
    # Paste.httpexceptions.HTTPUnauthorized, Paste.httpexceptions.HTTPNotFound
    ignored_exceptions = []
    if ignore:
        from pkg_resources import EntryPoint
        ignore_names = [ name.strip() for name in ignore.split() ]
        bdict = __builtins__
        if not isinstance(bdict, dict): #pragma NO COVER pypy
            bdict = bdict.__dict__
        for name in ignore_names:
            if name in bdict:
                ignored_exc = bdict[name]
            else:
                ep = EntryPoint.parse('x=%s' % name)
                ignored_exc = EntryPoint.parse('x=%s' % name).resolve()

            ignored_exceptions.append(ignored_exc)
    ignored_exceptions = tuple(ignored_exceptions)
    return ErrorLog(app, channel, keep, path, ignored_exceptions)
Example #3
0
    def test_normcase_registration(self):
        # create an empty working set for a clean-slate test.
        cwd = utils.mkdtemp(self)
        mock_ws = WorkingSet([])
        dist_ = Distribution(cwd, project_name='pkg', version='1.0')
        dist_.egg_info = cwd  # just lazy
        registry = ArtifactRegistry('calmjs.artifacts', _working_set=mock_ws)
        # case sensitive test; have to patch the normcase at artifact
        # module with the nt version
        from ntpath import normcase as nt_normcase
        utils.stub_item_attr_value(self, artifact, 'normcase', nt_normcase)
        # using named case for case sensitivity test.
        c1 = EntryPoint.parse('case.js = dummy_builder:builder1')
        c1.dist = dist_
        c2 = EntryPoint.parse('Case.js = dummy_builder:builder2')
        c2.dist = dist_
        # use the error one
        ct = join(cwd, 'calmjs_artifacts', 'Case.js')
        with pretty_logging(stream=mocks.StringIO()) as stream:
            registry.register_entry_point(c1)
            registry.register_entry_point(c2)

        log = stream.getvalue()
        self.assertIn(
            "entry point 'Case.js = dummy_builder:builder2' from package "
            "'pkg 1.0' resolves to the path '%s' which was already "
            "registered to entry point 'case.js = dummy_builder:builder1'; "
            "conflicting entry point registration will be ignored." % ct,
            log
        )
        self.assertIn(
            "the file mapping error is caused by this platform's case-"
            "insensitive filename", log
        )
Example #4
0
 def setUp(self):
     self.mgr = cmds.CommandManager()
     self.mgr.load_namespace('contrail_api_cli.shell_command')
     ep = EntryPoint('cmd', 'contrail_api_cli.tests.test_command', attrs=('Cmd',))
     cls = ep.load(require=False)
     ext = Extension('cmd', ep, cls, cls('cmd'))
     self.mgr.mgrs[0].extensions.append(ext)
 def testRejects(self):
     for ep in [
         "foo", "x=1=2", "x=a:b:c", "q=x/na", "fez=pish:tush-z", "x=f[a]>2",
     ]:
         try: EntryPoint.parse(ep)
         except ValueError: pass
         else: raise AssertionError("Should've been bad", ep)
Example #6
0
    def test_conflict_registration(self):
        # create an empty working set for a clean-slate test.
        cwd = utils.mkdtemp(self)
        mock_ws = WorkingSet([])
        registry = ArtifactRegistry('calmjs.artifacts', _working_set=mock_ws)
        # using named case for case sensitivity test.
        st = join(cwd, 'calmjs_artifacts', 'Simple.js')
        dist_ = Distribution(cwd, project_name='pkg', version='1.0')
        dist_.egg_info = cwd  # just lazy
        s1 = EntryPoint.parse('Simple.js = dummy_builder:builder1')
        s1.dist = dist_
        s2 = EntryPoint.parse('Simple.js = dummy_builder:builder2')
        s2.dist = dist_

        with pretty_logging(stream=mocks.StringIO()) as stream:
            registry.register_entry_point(s1)
            # normal registry usage shouldn't be able to do this.
            registry.register_entry_point(s2)

        log = stream.getvalue()
        self.assertIn(
            "entry point 'Simple.js = dummy_builder:builder2' from package "
            "'pkg 1.0' resolves to the path '%s' which was already "
            "registered to entry point 'Simple.js = dummy_builder:builder1'; "
            "conflicting entry point registration will be ignored." % st,
            log
        )
Example #7
0
def event_entrypoints():
    """Declare some events by mocking the invenio_stats.events entrypoint.

    It yields a list like [{event_type: <event_type_name>}, ...].
    """
    data = []
    result = []
    for idx in range(5):
        event_type_name = 'event_{}'.format(idx)
        from pkg_resources import EntryPoint
        entrypoint = EntryPoint(event_type_name, event_type_name)
        conf = dict(event_type=event_type_name, templates='/',
                    processor_class=EventsIndexer)
        entrypoint.load = lambda conf=conf: (lambda: [conf])
        data.append(entrypoint)
        result.append(conf)

    # including file_download
    event_type_name = 'file-download'
    from pkg_resources import EntryPoint
    entrypoint = EntryPoint('invenio_files_rest', 'test_dir')
    conf = dict(event_type=event_type_name, templates='contrib/file-download',
                processor_class=EventsIndexer)
    entrypoint.load = lambda conf=conf: (lambda: [conf])
    data.append(entrypoint)

    entrypoints = mock_iter_entry_points_factory(data, 'invenio_stats.events')

    with patch('invenio_stats.ext.iter_entry_points',
               entrypoints):
        yield result
 def testParseMap(self):
     m = EntryPoint.parse_map({'xyz':self.submap_str})
     self.checkSubMap(m['xyz'])
     self.assertEqual(list(m.keys()),['xyz'])
     m = EntryPoint.parse_map("[xyz]\n"+self.submap_str)
     self.checkSubMap(m['xyz'])
     self.assertEqual(list(m.keys()),['xyz'])
     self.assertRaises(ValueError, EntryPoint.parse_map, ["[xyz]", "[xyz]"])
     self.assertRaises(ValueError, EntryPoint.parse_map, self.submap_str)
 def setUp(self):
     from pkg_resources import EntryPoint
     
     spec = """
         ep1=crushinator.framework.tests.test_aggregator:dummy_entry_point
         ep2=crushinator.framework.tests.test_aggregator:dummy_entry_point
     """
     
     self.runner_entry_points = EntryPoint.parse_group('crushinator.runner', spec)
     self.collector_entry_points = EntryPoint.parse_group('crushinator.collector', spec)
Example #10
0
def test_retrieve_all():
    ret = pymonkey.get_entry_callables(
        True, (),
        [
            EntryPoint.parse('mod1 = patchingmod'),
            EntryPoint.parse('mod2 = patchingmod'),
        ],
        'pymonkey_patch',
    )

    assert ret == {
        'mod1': patchingmod.pymonkey_patch,
        'mod2': patchingmod.pymonkey_patch,
    }
Example #11
0
 def test_dummy_implemented_manual_entrypoint_double_regisetr(self):
     from calmjs.testing import module1
     registry = DummyModuleRegistry(__name__)
     with pretty_logging(stream=mocks.StringIO()) as s:
         registry.register_entry_point(
             EntryPoint.parse(
                 'calmjs.testing.module1 = calmjs.testing.module1'))
         registry.register_entry_point(
             EntryPoint.parse(
                 'calmjs.testing.module1 = calmjs.testing.module1'))
         # no dist.
         self.assertIn('manually registering entry_point', s.getvalue())
     result = registry.get_record('calmjs.testing.module1')
     # just merged together.
     self.assertEqual(result, {'calmjs.testing.module1': module1})
Example #12
0
    def run_tests(self):
        import unittest

        # Purge modules under test from sys.modules. The test loader will
        # re-import them from the build location. Required when 2to3 is used
        # with namespace packages.
        if sys.version_info >= (3,) and getattr(self.distribution, 'use_2to3', False):
            module = self.test_args[-1].split('.')[0]
            if module in _namespace_packages:
                del_modules = []
                if module in sys.modules:
                    del_modules.append(module)
                module += '.'
                for name in sys.modules:
                    if name.startswith(module):
                        del_modules.append(name)
                list(map(sys.modules.__delitem__, del_modules))

        loader_ep = EntryPoint.parse("x="+self.test_loader)
        loader_class = loader_ep.load(require=False)
        cks = loader_class()
        unittest.main(
            None, None, [unittest.__file__]+self.test_args,
            testLoader = cks
        )
Example #13
0
def load_model(dotted_name):
    """Import module and use module-level variable".

    :param dotted_name: path to model in form of string: ``some.python.module:Class``

    .. versionchanged:: 0.5.4

    """
    if isinstance(dotted_name, six.string_types):
        if ':' not in dotted_name:
            # backwards compatibility
            warnings.warn('model should be in form of module.model:User '
                'and not module.model.User', exceptions.MigrateDeprecationWarning)
            dotted_name = ':'.join(dotted_name.rsplit('.', 1))

        ep = EntryPoint.parse('x=%s' % dotted_name)
        if hasattr(ep, 'resolve'):
            # this is available on setuptools >= 10.2
            return ep.resolve()
        else:
            # this causes a DeprecationWarning on setuptools >= 11.3
            return ep.load(False)
    else:
        # Assume it's already loaded.
        return dotted_name
    def test_core_and_other_plugin(self, app, pr):

        core = EntryPoint.parse('core = girder_worker:GirderWorkerPlugin')
        core.load = mock.Mock(return_value=girder_worker.GirderWorkerPlugin)

        plugin = EntryPoint.parse('mock = mockplugin:MockPlugin')
        plugin.load = mock.Mock(return_value=mock_plugin(['mock.plugin.tasks']))

        pr.iter_entry_points.return_value = [core, plugin]

        main()

        app.conf.update.assert_any_call({'CELERY_IMPORTS':
                                         ['girder_worker.tasks']})
        app.conf.update.assert_any_call({'CELERY_INCLUDE':
                                         ['mock.plugin.tasks']})
Example #15
0
 def evolve_to(self, version):
     scriptname = '%s.evolve%s' % (self.package_name, version)
     evmodule = EntryPoint.parse('x=%s' % scriptname).load(False)
     self.step(version, evmodule)
     self.information(
         'Documentation',
         evmodule.__doc__ if evmodule.__doc__ is not None else 'UNKNOWN'
     )
     with transaction.manager:
         has_load_model = hasattr(evmodule.evolve, 'load_model')
         if has_load_model:
             root = self._get_root(version, evmodule.evolve.load_model)
             self.last_evolve_model = evmodule.evolve.load_model
             self.information('Load model', self.last_evolve_model.__name__)
         else:
             root = self._get_root(version)
         self.task('Executing script')
         try:
             evmodule.evolve(root, config=self.config)
             if has_load_model:
                 cleanup()
                 self.task('Cleanup model snapshot')
             self.set_db_version(root, version)
         except EvolveError as exc:
             error(exc)
Example #16
0
    def _find_entry_points(self, namespace):
        entry_points = {}
        # Internal entry points
        if namespace == 'guessit.transformer':
            internal_entry_points_str = ['split_path_components = guessit.transfo.split_path_components:SplitPathComponents',
                                    'guess_filetype = guessit.transfo.guess_filetype:GuessFiletype',
                                    'split_explicit_groups = guessit.transfo.split_explicit_groups:SplitExplicitGroups',
                                    'guess_date = guessit.transfo.guess_date:GuessDate',
                                    'guess_website = guessit.transfo.guess_website:GuessWebsite',
                                    'guess_release_group = guessit.transfo.guess_release_group:GuessReleaseGroup',
                                    'guess_properties = guessit.transfo.guess_properties:GuessProperties',
                                    'guess_language = guessit.transfo.guess_language:GuessLanguage',
                                    'guess_video_rexps = guessit.transfo.guess_video_rexps:GuessVideoRexps',
                                    'guess_episodes_rexps = guessit.transfo.guess_episodes_rexps:GuessEpisodesRexps',
                                    'guess_weak_episodes_rexps = guessit.transfo.guess_weak_episodes_rexps:GuessWeakEpisodesRexps',
                                    'guess_bonus_features = guessit.transfo.guess_bonus_features:GuessBonusFeatures',
                                    'guess_year = guessit.transfo.guess_year:GuessYear',
                                    'guess_country = guessit.transfo.guess_country:GuessCountry',
                                    'guess_idnumber = guessit.transfo.guess_idnumber:GuessIdnumber',
                                    'split_on_dash = guessit.transfo.split_on_dash:SplitOnDash',
                                    'guess_episode_info_from_position = guessit.transfo.guess_episode_info_from_position:GuessEpisodeInfoFromPosition',
                                    'guess_movie_title_from_position = guessit.transfo.guess_movie_title_from_position:GuessMovieTitleFromPosition',
                                    'post_process = guessit.transfo.post_process:PostProcess']
            for internal_entry_point_str in internal_entry_points_str:
                internal_entry_point = EntryPoint.parse(internal_entry_point_str)
                entry_points[internal_entry_point.name] = internal_entry_point

        # Package entry points
        setuptools_entrypoints = super(EnabledExtensionManager, self)._find_entry_points(namespace)
        for setuptools_entrypoint in setuptools_entrypoints:
            entry_points[setuptools_entrypoint.name] = setuptools_entrypoint

        return list(entry_points.values())
Example #17
0
 def iter_entry_points(self, name):
     items = self.items.get(name, [])
     for item in items:
         entry_point = item if isinstance(
             item, EntryPoint) else EntryPoint.parse(item)
         entry_point.dist = self.dist
         yield entry_point
    def testParse(self):
        s = "foo = setuptools.tests.test_resources:EntryPointTests [x]"
        ep = EntryPoint.parse(s, self.dist)
        self.assertfields(ep)

        ep = EntryPoint.parse("bar baz=  spammity[PING]")
        self.assertEqual(ep.name,"bar baz")
        self.assertEqual(ep.module_name,"spammity")
        self.assertEqual(ep.attrs, ())
        self.assertEqual(ep.extras, ("ping",))

        ep = EntryPoint.parse(" fizzly =  wocka:foo")
        self.assertEqual(ep.name,"fizzly")
        self.assertEqual(ep.module_name,"wocka")
        self.assertEqual(ep.attrs, ("foo",))
        self.assertEqual(ep.extras, ())
Example #19
0
    def run_tests(self):
        import sys, os

        rootdir =  os.path.dirname(os.path.abspath(__file__))
        if rootdir in sys.path:
            sys.path.remove(rootdir)

        # Ensure that any installed versions of this package aren't on sys.path
        ei_cmd = self.get_finalized_command('egg_info')
        egg_name = ei_cmd.egg_name.replace('-', '_')

        to_remove = []
        for dirname in sys.path:
            bn = os.path.basename(dirname)
            if bn.startswith(egg_name + "-"):
                to_remove.append(dirname)

        for dirname in to_remove:
            log.info("removing installed %r from sys.path before testing"%(dirname,))
            sys.path.remove(dirname)

        # Actually run the tests
        if sys.version_info[0] == 2:
            sys.path.insert(0, rootdir)
    
        import PyObjCTest
        import unittest
        from pkg_resources import EntryPoint
        loader_ep = EntryPoint.parse("x="+self.test_loader)
        loader_class = loader_ep.load(require=False)

        unittest.main(None, None, [unittest.__file__]+self.test_args, testLoader=loader_class())
Example #20
0
def includeme(config): # pragma: no cover
    config.add_directive('add_evolution_step', add_evolution_step)
    config.add_evolution_step(legacy_to_new)
    config.scan('.subscribers')
    for i in range(1, VERSION+1):
        scriptname = 'substanced.evolution.evolve%s' % i
        evmodule = EntryPoint.parse('x=%s' % scriptname).load(False)
        config.add_evolution_step(evmodule.evolve)
Example #21
0
    def _find_entry_points(self, namespace):
        # default entry points
        eps = super(ProviderManager, self)._find_entry_points(namespace)

        # internal entry points
        for iep in self.internal_providers:
            ep = EntryPoint.parse(iep)
            if ep.name not in [e.name for e in eps]:
                eps.append(ep)

        # registered entry points
        for rep in self.registered_providers:
            ep = EntryPoint.parse(rep)
            if ep.name not in [e.name for e in eps]:
                eps.append(ep)

        return eps
Example #22
0
    def load_all(cls):
        if cls.__plugins__ is not None:
            return cls.__plugins__

        entry_points = []

        # builtin plugins
        for _, m, _ in iter_modules(['squad/plugins']):
            e = EntryPoint(m, 'squad.plugins.' + m, attrs=('Plugin',))
            entry_points.append(e)

        # external plugins
        plugins = iter_entry_points('squad_plugins')
        entry_points += list(plugins)

        cls.__plugins__ = {e.name: e.resolve() for e in entry_points}
        return cls.__plugins__
Example #23
0
def setup_hasher(hasher_name):
    hasher_class = EntryPoint.parse('hasher=%s' % hasher_name).load(False)
    if not IHasher.implementedBy(hasher_class):
        raise BrokenImplementation
    _hasher = hasher_class()

    global hasher
    hasher = _hasher
Example #24
0
    def _find_entry_points(self, namespace):
        # copy of default extensions
        eps = list(super(RegistrableExtensionManager, self)._find_entry_points(namespace))

        # internal extensions
        for iep in self.internal_extensions:
            ep = EntryPoint.parse(iep)
            if ep.name not in [e.name for e in eps]:
                eps.append(ep)

        # registered extensions
        for rep in self.registered_extensions:
            ep = EntryPoint.parse(rep)
            if ep.name not in [e.name for e in eps]:
                eps.append(ep)

        return eps
Example #25
0
def plimc(args=None, stdout=None):
    """This is the `plimc` command line utility

    :param args: list of command-line arguments. If None, then ``sys.argv[1:]`` will be used.
    :type args: list or None
    :param stdout: file-like object representing stdout. If None, then ``sys.stdout`` will be used.
                   Custom stdout is used for testing purposes.
    :type stdout: None or a file-like object
    """
    # Parse arguments
    # ------------------------------------
    cli_parser = argparse.ArgumentParser(description='Compile plim source files into mako files.')
    cli_parser.add_argument('source', help="path to source plim template")
    cli_parser.add_argument('-o', '--output', help="write result to FILE.")
    cli_parser.add_argument('-e', '--encoding', default='utf-8', help="content encoding")
    cli_parser.add_argument('-p', '--preprocessor', default='plim:preprocessor',
                            help="Preprocessor instance that will be used for parsing the template")
    cli_parser.add_argument('-H', '--html', action='store_true', help="Render HTML output instead of Mako template")
    cli_parser.add_argument('-V', '--version', action='version',
                            version='Plim {}'.format(get_distribution("Plim").version))

    if args is None:
        args = sys.argv[1:]
    args = cli_parser.parse_args(args)

    # Get custom preprocessor, if specified
    # -------------------------------------
    preprocessor_path = args.preprocessor
    preprocessor = EntryPoint.parse('x={}'.format(preprocessor_path)).load(False)

    # Render to html, if requested
    # ----------------------------
    if args.html:
        root_dir = os.path.dirname(os.path.abspath(args.source))
        template_file = os.path.basename(args.source)
        lookup = TemplateLookup(directories=[root_dir],
                                input_encoding=args.encoding,
                                output_encoding=args.encoding,
                                preprocessor=preprocessor)
        content = lookup.get_template(template_file).render_unicode()
    else:
        with codecs.open(args.source, 'rb', args.encoding) as fd:
            content = preprocessor(fd.read())

    # Output
    # ------------------------------------
    if args.output is None:
        if stdout is None:
            stdout = PY3K and sys.stdout.buffer or sys.stdout
        fd = stdout
        content = codecs.encode(content, 'utf-8')
    else:
        fd = codecs.open(args.output, 'wb', args.encoding)
    try:
        fd.write(content)
    finally:
        fd.close()
Example #26
0
 def evolve_to(self, version):
     scriptname = '%s.evolve%s' % (self.package_name, version)
     evmodule = EntryPoint.parse('x=%s' % scriptname).load(False)
     if self.transaction is not None:
         self.transaction.begin()
     evmodule.evolve(self.context)
     self.set_db_version(version, commit=False)
     if self.transaction is not None:
         self.transaction.commit()
 def run_tests(self):
     import unittest
     loader_ep = EntryPoint.parse("x="+self.test_loader)
     loader_class = loader_ep.load(require=False)
     unittest.main(
         None, None, [unittest.__file__]+self.test_args,
         testLoader = loader_class(),
         testRunner = ExtendedTeamcityTestRunner()
     )
Example #28
0
 def test_printable_name(self):
     """
     Allow any printable character in the name.
     """
     # Create a name with all printable characters; strip the whitespace.
     name = string.printable.strip()
     spec = "{name} = module:attr".format(**locals())
     ep = EntryPoint.parse(spec)
     assert ep.name == name
Example #29
0
 def _resolve_as_ep(val):
     """
     Load the indicated attribute value, called, as a as if it were
     specified as an entry point.
     """
     if val is None:
         return
     parsed = EntryPoint.parse("x=" + val)
     return parsed.load(require=False)()
Example #30
0
    def test_add_component(self, use_entrypoint):
        """
        Tests that add_component works with an without an entry point and that external
        configuration overriddes local (hardcoded) configuration values.
        """

        app = CustomApp({'shutdown': {'method': 'stop'}})
        if not use_entrypoint:
            app.add_component('shutdown', ShutdownComponent, method='exception')
        else:
            entrypoint = EntryPoint('shutdown', __spec__.name)
            entrypoint.load = Mock(return_value=ShutdownComponent)
            app.component_types['shutdown'] = entrypoint
            app.add_component('shutdown', method='exception')

        assert len(app.components) == 1
        assert isinstance(app.components[0], ShutdownComponent)
        assert app.components[0].method == 'stop'
Example #31
0
 def find_dependencies_inner(pkgname):
     stack.append(pkgname)
     log.debug("%r %r", pkgname, stack)
     if pkgname in stack[:-1]:
         raise CircularDependencyError(stack)
     if not pkgname in visited:
         mod = EntryPoint.parse("x=%s.evolve" % pkgname).resolve()
         visited.add(pkgname)
         for p in mod.DEPENDS:
             find_dependencies_inner(p)
         dependencies.append(mod)
     stack.pop()
    def testParse(self):
        s = "foo = pkg_resources.tests.test_resources:TestEntryPoints [x]"
        ep = EntryPoint.parse(s, self.dist)
        self.assertfields(ep)

        ep = EntryPoint.parse("bar baz=  spammity[PING]")
        assert ep.name == "bar baz"
        assert ep.module_name == "spammity"
        assert ep.attrs == ()
        assert ep.extras == ("ping", )

        ep = EntryPoint.parse(" fizzly =  wocka:foo")
        assert ep.name == "fizzly"
        assert ep.module_name == "wocka"
        assert ep.attrs == ("foo", )
        assert ep.extras == ()

        # plus in the name
        spec = "html+mako = mako.ext.pygmentplugin:MakoHtmlLexer"
        ep = EntryPoint.parse(spec)
        assert ep.name == 'html+mako'
Example #33
0
def parse_entry_points(fp):
    return {
        gr: {
            k: {
                "module": e.module_name,
                "attr": '.'.join(e.attrs) if e.attrs else None,
                "extras": list(e.extras),
            }
            for k, e in eps.items()
        }
        for gr, eps in EntryPoint.parse_map(fp).items()
    }
Example #34
0
    def test_load_entry_points_bad2(self):
        """Raise warning and continue when entry point fails - bad module"""

        epoint1 = EntryPoint.parse('bad = tests.testdata.lib.parsers.bad', dist=DIST)
        epoint2 = EntryPoint.parse('parsers = tests.testdata.lib.parsers.xml', dist=DIST)
        DIST._ep_map = {'pluginlib.test.plugins': {'bad': epoint1, 'parsers': epoint2}}

        ploader = loader.PluginLoader(group='testdata', entry_point='pluginlib.test.plugins')

        with warnings.catch_warnings(record=True) as e:
            warnings.simplefilter("always")
            plugins = ploader.plugins

            self.assertEqual(len(e), 1)
            self.assertTrue(issubclass(e[-1].category, EntryPointWarning))
            self.assertRegex(str(e[-1].message), 'can not be loaded for entry point bad')

        self.assertEqual(len(plugins.parser), 1)
        self.assertTrue('xml' in plugins.parser)
        self.assertEqual(len(plugins.engine), 0)
        self.assertEqual(len(plugins.hook), 0)
Example #35
0
    def test_indexer_generate_modname_nunja_script(self):
        entry_point = EntryPoint.parse(
            'example.package.mold = example.package:mold')
        module = ModuleType('example.package')

        nunja_template, nunja_script, nunja_modpath = generate_modname_nunja(
            entry_point, module, fext='.tmpl')
        # the names provided will be the fragments based on actual file
        # name, with the final filename extension stripped.
        name = nunja_script(['example', 'package', 'table', 'index'])

        self.assertEqual(name, 'example.package.mold/table/index')
Example #36
0
    def test_indexer_generate_modname_nunja_modpath_pkg_resources(self):
        entry_point = EntryPoint.parse('nunja.testing = nunja.testing:mold')

        nunja_template, nunja_script, nunja_modpath = generate_modname_nunja(
            entry_point, testing, fext='.tmpl')
        # the names provided will be the fragments based on actual file
        # name, with the final filename extension stripped.
        name = nunja_modpath(testing, entry_point)
        self.assertEqual(len(name), 1)
        # make it easier to see wrong than just not True
        self.assertIn(join('nunja', 'testing', 'mold'), name[0])
        self.assertTrue(name[0].endswith(join('nunja', 'testing', 'mold')))
Example #37
0
 def test_dummy_implemented_manual_entrypoint(self):
     from calmjs.testing import module1
     registry = DummyModuleRegistry(__name__)
     with pretty_logging(stream=mocks.StringIO()) as s:
         registry.register_entry_point(
             EntryPoint.parse(
                 'calmjs.testing.module1 = calmjs.testing.module1')
         )
         # no dist.
         self.assertIn('manually registering entry_point', s.getvalue())
     result = registry.get_record('calmjs.testing.module1')
     self.assertEqual(result, {'calmjs.testing.module1': module1})
Example #38
0
    def test_indexer_modpath_pkg_resources_entry_point_not_import(self):
        entry_point = EntryPoint.parse('nunjatest = nunja.testing:mold')

        nunja_template, nunja_script, nunja_modpath = generate_modname_nunja(
            entry_point, testing, fext='.tmpl')

        with pretty_logging(logger='nunja', stream=StringIO()) as stream:
            path = nunja_modpath(testing, entry_point)

        msg = stream.getvalue()
        self.assertEqual(msg, '')
        self.assertTrue(path[0].endswith(join('nunja', 'testing', 'mold')))
Example #39
0
def event_entrypoints():
    """Declare some events by mocking the invenio_stats.events entrypoint.

    It yields a list like [{event_type: <event_type_name>}, ...].
    """
    data = []
    result = []
    for idx in range(5):
        event_type_name = 'event_{}'.format(idx)
        from pkg_resources import EntryPoint
        entrypoint = EntryPoint(event_type_name, event_type_name)
        conf = dict(event_type=event_type_name,
                    templates='/',
                    processor_class=EventsIndexer)
        entrypoint.load = lambda conf=conf: (lambda: [conf])
        data.append(entrypoint)
        result.append(conf)

    # including file_download
    event_type_name = 'file-download'
    from pkg_resources import EntryPoint
    entrypoint = EntryPoint('invenio_files_rest', 'test_dir')
    conf = dict(event_type=event_type_name,
                templates='contrib/file-download',
                processor_class=EventsIndexer)
    entrypoint.load = lambda conf=conf: (lambda: [conf])
    data.append(entrypoint)

    entrypoints = mock_iter_entry_points_factory(data, 'invenio_stats.events')

    with patch('invenio_stats.ext.iter_entry_points', entrypoints):
        yield result
Example #40
0
    def run(self):
        import unittest
        import time

        # Ensure that build directory is on sys.path (py3k)
        import sys

        self.cleanup_environment()
        self.add_project_to_sys_path()

        import PyObjCTools.TestSupport as modo

        from pkg_resources import EntryPoint

        loader_ep = EntryPoint.parse("x=" + self.test_loader)
        loader_class = loader_ep.resolve()

        import warnings

        warnings.simplefilter("error")

        try:
            meta = self.distribution.metadata
            name = meta.get_name()
            test_pkg = name + "_tests"

            time_before = time.time()
            suite = loader_class().loadTestsFromName(
                self.distribution.test_suite)

            runner = unittest.TextTestRunner(verbosity=self.verbosity)
            result = runner.run(suite)

            time_after = time.time()

            # Print out summary. This is a structured format that
            # should make it easy to use this information in scripts.
            summary = dict(
                count=result.testsRun,
                fails=len(result.failures),
                errors=len(result.errors),
                xfails=len(getattr(result, "expectedFailures", [])),
                xpass=len(getattr(result, "unexpectedSuccesses", [])),
                skip=len(getattr(result, "skipped", [])),
                testSeconds=(time_after - time_before),
            )
            print("SUMMARY: %s" % (summary, ))
            if not result.wasSuccessful():
                raise DistutilsError("some tests failed")

        finally:
            self.remove_from_sys_path()
Example #41
0
def test_compute_match_types(mocker):
    iep = mocker.patch("raincoat.match.iter_entry_points")

    class MatchFactory(object):
        def __init__(self, name):
            self.name = name

        def __repr__(self):
            return self.name

    MatchA, MatchB = MatchFactory("A"), MatchFactory("B")

    entry_a, entry_b = iep.return_value = [
        EntryPoint("a", "aaa"),
        EntryPoint("b", "bbb"),
    ]
    entry_a.load = lambda: MatchA
    entry_b.load = lambda: MatchB

    assert match_module.compute_match_types() == {"a": MatchA, "b": MatchB}
    assert MatchA.match_type == "a"
    assert MatchB.match_type == "b"
    def load_instrumentor(  # pylint: disable=no-self-use
            self, entry_point: EntryPoint):
        """Takes a collection of instrumentation entry points
        and activates them by instantiating and calling instrument()
        on each one.

        Distros can override this method to customize the behavior by
        inspecting each entry point and configuring them in special ways,
        passing additional arguments, load a replacement/fork instead,
        skip loading entirely, etc.
        """
        instrumentor: BaseInstrumentor = entry_point.load()
        instrumentor().instrument()
Example #43
0
 def _load_plugin(self, ep: pkg_resources.EntryPoint):
     try:
         self.logger.debug(" Loading plugin %s" % ep)
         plugin = ep.load(require=True)
         self.logger.debug(" Initializing plugin %s" % ep)
         plugin_context = copy.copy(self.app_context)
         plugin_context.logger = self.logger.getChild(ep.name)
         obj = plugin(plugin_context)
         return Plugin(ep.name, ep, obj)
     except ImportError as ie:
         self.logger.warning("Plugin %r import failed: %s" % (ep, ie))
     except pkg_resources.UnknownExtra as ue:
         self.logger.warning("Plugin %r dependencies resolution failed: %s" % (ep, ue))
Example #44
0
def test_provider_manager():
    setup_names = {
        ep.name
        for ep in iter_entry_points(provider_manager.namespace)
    }
    internal_names = {
        EntryPoint.parse(iep).name
        for iep in provider_manager.internal_extensions
    }
    enabled_names = set(default_providers)
    disabled_names = set(disabled_providers)
    assert enabled_names == setup_names - disabled_names
    assert internal_names == enabled_names | disabled_names
Example #45
0
    def execute_pkg_resources(cls, spec):
        cls.demote_bootstrap()

        entry = EntryPoint.parse("run = {0}".format(spec))

        # See https://pythonhosted.org/setuptools/history.html#id25 for rationale here.
        if hasattr(entry, 'resolve'):
            # setuptools >= 11.3
            runner = entry.resolve()
        else:
            # setuptools < 11.3
            runner = entry.load(require=False)
        return runner()
Example #46
0
    def test_indexer_modpath_pkg_resources_entry_point_fail_import(self):
        entry_point = EntryPoint.parse('example.mold = example.package:mold')
        module = ModuleType('example.package')

        nunja_template, nunja_script, nunja_modpath = generate_modname_nunja(
            entry_point, module, fext='.tmpl')

        with pretty_logging(logger='nunja', stream=StringIO()) as stream:
            nunja_modpath(None, None)

        msg = stream.getvalue()
        self.assertIn("does not appear to be a valid module", msg)
        self.assertIn("got unexpected entrypoint", msg)
Example #47
0
 def __getitem__(self, name):
     """Get a provider, lazy loading it if necessary"""
     if name in self.providers:
         return self.providers[name]
     for ep in iter_entry_points(self.entry_point):
         if ep.name == name:
             self.providers[ep.name] = ep.load()
             return self.providers[ep.name]
     for ep in (EntryPoint.parse(c) for c in self.registered_providers):
         if ep.name == name:
             self.providers[ep.name] = ep.load(require=False)
             return self.providers[ep.name]
     raise KeyError(name)
Example #48
0
 def prepare_from_item(self, key, value):
     # TODO if value is of a BaseResourceDefinition...
     kwargs = dict(value)
     if '__init__' not in kwargs:
         raise ValueError(
             "provided object mapping missing the '__init__' key")
     entry = EntryPoint.parse('target=' + kwargs.pop('__init__'))
     target = entry.resolve()
     kwargs = {
         key: map_vars_value(value, vars_=self.__vars)
         for key, value in kwargs.items()
     }
     return target(**kwargs)
Example #49
0
def get_wex_entry_points_from_cwd():
    try:
        with open(os.path.join(os.getcwd(), 'entry_points.txt')) as txt:
            entry_point_map = EntryPoint.parse_map(txt.read())
        entry_points = dict(
            (str(ep), ep) for ep in entry_point_map.get(GROUP, {}).values())
        if os.getcwd() not in sys.path:
            sys.path.insert(0, os.getcwd())
    except IOError as exc:
        if exc.errno != errno.ENOENT:
            raise
        entry_points = {}

    return entry_points
Example #50
0
    def test_module_registry_standard(self):
        with pretty_logging(stream=mocks.StringIO()):
            self.registry.register_entry_points([
                EntryPoint.parse(
                    'calmjs.testing.module1 = calmjs.testing.module1')
            ])
        self.assertEqual(
            sorted(key for key, value in self.registry.iter_records()), [
                'calmjs.testing.module1',
            ])

        module1 = self.registry.get_record('calmjs.testing.module1')
        key = 'calmjs/testing/module1/hello'
        self.assertEqual(sorted(module1.keys()), [key])
Example #51
0
def iter_entry_points(group, name):
    """Mock pkg_resources.iter_entry_points to yield EntryPoint from
    packages found in test/data even though these are not
    installed.
    """
    libpython = CubicWebConfigurationTC.datapath()
    prefix = 'cubicweb_'
    for pkgname in os.listdir(libpython):
        if not pkgname.startswith(prefix):
            continue
        location = join(libpython, pkgname)
        yield EntryPoint(pkgname[len(prefix):],
                         pkgname,
                         dist=Distribution(location))
Example #52
0
def load_model(dotted_name):
    """Load module with dotted name syntax

    Example::

        >>> load_model('gpypi.utils:import_path') # doctest: +ELLIPSIS
        <function import_path at 0x...>

    """
    if isinstance(dotted_name, basestring):
        return EntryPoint.parse('x=%s' % dotted_name).load(False)
    else:
        # Assume it's already loaded.
        return dotted_name
 def register(self, entry_point):
     """Register a provider
     :param string entry_point: provider to register (entry point syntax)
     :raise: ValueError if already registered
     """
     if entry_point in self.registered_providers:
         raise ValueError('Entry point \'%s\' already registered' %
                          entry_point)
     entry_point_name = EntryPoint.parse(entry_point).name
     if entry_point_name in self.available_providers:
         raise ValueError(
             'An entry point with name \'%s\' already registered' %
             entry_point_name)
     self.registered_providers.insert(0, entry_point)
Example #54
0
    def _find_entry_points(self, namespace):
        entry_points = {}
        # Internal entry points
        if namespace == self.namespace:
            for internal_entry_point_str in self._internal_entry_points:
                internal_entry_point = EntryPoint.parse(internal_entry_point_str)
                entry_points[internal_entry_point.name] = internal_entry_point

        # Package entry points
        setuptools_entrypoints = super(DefaultTransformerExtensionManager, self)._find_entry_points(namespace)
        for setuptools_entrypoint in setuptools_entrypoints:
            entry_points[setuptools_entrypoint.name] = setuptools_entrypoint

        return list(entry_points.values())
Example #55
0
def write_entries(cmd, basename, filename):
    ep = cmd.distribution.entry_points

    if isinstance(ep, str) or ep is None:
        data = ep
    elif ep is not None:
        data = []
        for section, contents in sorted(ep.items()):
            if not isinstance(contents, str):
                contents = EntryPoint.parse_group(section, contents)
                contents = '\n'.join(sorted(map(str, contents.values())))
            data.append('[%s]\n%s\n\n' % (section, contents))
        data = ''.join(data)

    cmd.write_or_delete_file('entry points', filename, data, True)
 def get_theme_dir(self):
     ''' Validate theme option and return path to theme's root obtained from entry point. '''
     entry_points = EntryPoint.parse_map(self.distribution.entry_points,
                                         self.distribution)
     if 'mkdocs.themes' not in entry_points:
         raise DistutilsOptionError(
             "no mkdocs.themes are defined in entry_points")
     if self.theme is None and len(entry_points['mkdocs.themes']) == 1:
         # Default to the only theme defined in entry_points as none specified.
         self.theme = tuple(entry_points['mkdocs.themes'].keys())[0]
     if self.theme not in entry_points['mkdocs.themes']:
         raise DistutilsOptionError(
             "you must specify a valid theme name to work on")
     theme = entry_points['mkdocs.themes'][self.theme]
     return path.dirname(theme.resolve().__file__)
Example #57
0
    def test_module_registry_pythonic(self):
        registry = PythonicModuleRegistry(__name__)
        with pretty_logging(stream=mocks.StringIO()):
            registry.register_entry_points([
                EntryPoint.parse(
                    'calmjs.testing.module1 = calmjs.testing.module1')
            ])
        self.assertEqual(sorted(key for key, value in registry.iter_records()),
                         [
                             'calmjs.testing.module1',
                         ])

        module1 = registry.get_record('calmjs.testing.module1')
        key = 'calmjs.testing.module1.hello'
        self.assertEqual(sorted(module1.keys()), [key])
Example #58
0
    def test_entry_points(self):
        # verify the expected entry points resolve and can be
        # initialized
        for nameref in ENTRY_POINTS.items():
            cmd = "=".join(nameref)
            ep = EntryPoint.parse(cmd)

            if hasattr(ep, "resolve"):
                #new environments
                cmd_inst = ep.resolve()
            else:
                # old environments
                cmd_inst = ep.load(require=False)

            self.assertTrue(callable(cmd_inst))
Example #59
0
 def run(self):
     install_scripts.run(self)
     orig_install_dir = self.install_dir
     self.install_dir = self.install_agents
     eps = EntryPoint.parse_map(self.distribution.entry_points)
     header = get_script_header('')
     for ep in eps.get('resource_agents', {}).values():
         filename = os.path.join(*(ep.name.split('.')))
         contents = header + self.agent_template % {
             'module': ep.module_name,
             'class': ep.attrs[0],
             'method': '.'.join(ep.attrs),
         }
         self.write_script(filename, contents)
     self.install_dir = orig_install_dir
Example #60
0
def test_queues_entrypoints(app):
    """Declare some queues by mocking the invenio_queues.queues entrypoint.

    It yields a list like [{name: queue_name, exchange: conf}, ...].
    """
    data = []
    result = []
    for idx in range(5):
        queue_name = 'queue{}'.format(idx)
        from pkg_resources import EntryPoint
        entrypoint = EntryPoint(queue_name, queue_name)
        conf = dict(name=queue_name, exchange=MOCK_MQ_EXCHANGE)
        entrypoint.load = lambda conf=conf: (lambda: [conf])
        data.append(entrypoint)
        result.append(conf)

    entrypoints = mock_iter_entry_points_factory(data)

    with patch('pkg_resources.iter_entry_points',
               entrypoints):
        try:
            yield result
        finally:
            remove_queues(app)