Exemple #1
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
        )
Exemple #2
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
        )
Exemple #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)
 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)
 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)
Exemple #6
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)
Exemple #7
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)
Exemple #8
0
    def test_load_entry_points_pkg(self):
        """Load modules from entry points"""

        # Entry point is package
        epoint1 = EntryPoint.parse('hooks = tests.testdata.lib.hooks', dist=DIST)
        # Entry point is module
        epoint2 = EntryPoint.parse('parsers = tests.testdata.lib.parsers.xml', dist=DIST)

        DIST._ep_map = {'pluginlib.test.plugins': {'hooks': epoint1, 'parsers': epoint2}}

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

        self.assertEqual(len(plugins), 3)
        self.assertTrue('parser' in plugins)
        self.assertTrue('engine' in plugins)
        self.assertTrue('hook' in plugins)

        self.assertEqual(len(plugins.engine), 0)

        self.assertEqual(len(plugins.hook), 2)
        self.assertTrue('right' in plugins.hook)
        self.assertTrue('left' in plugins.hook)

        self.assertEqual(len(plugins.parser), 1)
        self.assertTrue('xml' in plugins.parser)
Exemple #9
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,
    }
Exemple #10
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})
Exemple #11
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,
    }
Exemple #12
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})
Exemple #13
0
def check_standalone_help(cmdname):
    ref = ENTRY_POINTS[cmdname]

    ep = EntryPoint.parse("=".join([cmdname, ref]))
    name = ep.name

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

    # launch the loaded command with the --help option, and collect
    # its output. Also ensures that it causes the expected SystemExit
    with patch('sys.stdout', new=StringIO()) as out:
        with patch('sys.argv', new=[name, "--help"]):
            assert_raises(SystemExit, command)
        outhelp = out.getvalue()

    # now we'll load the expected help output from the first literal
    # block from the reST doc sources
    usage = find_usage("docs/standalone/%s.rst" % name)
    assert_true(usage is not None)

    # Then we can compare what we got from the command with what was
    # in the docs
    expected = space_normalize(usage)
    found = space_normalize(outhelp)
    assert_equal(expected, found)
    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']})
Exemple #15
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())
Exemple #16
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
Exemple #17
0
class FigmentatorSettings(BaseModel):
    """ Defines the settings for an individual Figmentator """

    cls: EntryPoint = Field(
        EntryPoint.parse(
            "model=figmentator.examples.simple:SimpleFigmentator"),
        description="""
The package path to a model class in the form of an EntryPoint as specified by:
https://setuptools.readthedocs.io/en/latest/pkg_resources.html#creating-and-parsing
        """,
    )
    requires: List[Requirement] = Field(
        [],
        description="""
A list of required packaged as specified by:
https://setuptools.readthedocs.io/en/latest/pkg_resources.html#requirements-parsing
""",
    )
    properties: Optional[Dict[str, Any]] = Field(
        ...,
        description="""
This is a dict of properties that will be passed to your model's startup method. It can
include anything you need to initalize your model, e.g. the path to your model
parameters.
""",
    )

    @classmethod
    def _parse_cls(cls: Type["FigmentatorSettings"], value: Any) -> EntryPoint:
        """ Parse and validate the model class in EntryPoint format """
        if isinstance(value, str):
            value = EntryPoint.parse(value)
        elif not isinstance(value, EntryPoint):
            raise ValueError(f"Must be str or EntryPoint, not {type(value)}!")

        return value

    @classmethod
    def _parse_requires(cls: Type["FigmentatorSettings"],
                        value: Any) -> List[Requirement]:
        """ Parse and validate a list of Requirements """
        if not isinstance(value, list):
            raise ValueError(f"Must be a list, not {type(value)}!")

        return list(parse_requirements([str(v) for v in value]))

    # In order to appease mypy, I cannot simply use validator as a decorator. Otherwise
    # it thinks the method is a normal method that expects self, because validator
    # internally wraps it in a classmethod (which mypy apparently doesn't handle well).
    parse_cls = validator("cls", pre=True)(getattr(_parse_cls, "__func__"))
    parse_requires = validator("requires", pre=True,
                               whole=True)(getattr(_parse_requires,
                                                   "__func__"))

    class Config:
        """
        Need to set allow_arbitrary_types so pydantic doesn't complain about EntryPoint
        """

        arbitrary_types_allowed = True
Exemple #18
0
    def run(self):
        """This method could be simpler by using the subprocess module, but the
        "correct" way to do things is to use the pkg_resources module to load
        the main method dynamically using the specification given by the
        `entry_points`.  This means that if the `entry_points` is changed, then
        this method will still work.
        """

        # Get the entry points, with gui_scripts taking precedence
        gui_scripts = self.distribution.entry_points.get('gui_scripts', [])
        console_scripts = self.distribution.entry_points.get(
            'console_scripts', [])
        scripts = gui_scripts + console_scripts

        if len(scripts) == 0:
            self.warn('No scripts defined in gui_scripts or console_scripts.')
            return

        script = scripts[0]

        if len(scripts) > 1:
            self.warn(
                'More than one script specified, running the first one: {}'.
                format(script))

        # This is a bit advanced, but use the pkg_resources module to load the
        # entry point.  In order to actually load it, we have to provide a
        # Distribution object, and an empty Distribution will work.
        ep = EntryPoint.parse(script, dist=Distribution())
        # Load the entry point (this is the actual function to run)
        function_to_run = ep.load()
        # And run it
        function_to_run()
Exemple #19
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
        )
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
Exemple #21
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
        )
Exemple #22
0
    def load_entrypoint(cls, reference, onerror='raise'):

        # Resolve entrypoint
        expression = u'_ = {reference}'.format(reference=reference)
        try:
            entrypoint = EntryPoint.parse(expression)
        except:
            log.failure(
                'Error parsing entrypoint "{reference}" from expression "{expression}"',
                reference=reference,
                expression=expression)
            raise

        # Load entrypoint
        try:
            thing = entrypoint.resolve()
        except:
            log.failure('Error loading entrypoint "{reference}"',
                        reference=reference)
            if onerror == 'raise':
                raise
            elif onerror == 'ignore':
                return cls.noop_callable
            #raise

        return thing
Exemple #23
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)
Exemple #24
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
Exemple #25
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())
Exemple #26
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())
Exemple #27
0
def test_LazyModule():

    # create an entry point for taurus.core.util.test.dumm
    w = WorkingSet()
    d = w.find(Requirement.parse('taurus'))
    ep = EntryPoint.parse("dummy_mod = taurus.core.util.test.dummy", dist=d)
    modname = ep.name

    # lazy load the ep module as taurus.fbt
    LazyModule.import_ep(modname, "taurus", ep)

    # check that lazy-loading did not import the entry point modules
    assert modname not in sys.modules
    assert ep.module_name not in sys.modules

    # import the module and check that it is a LazyModule
    import taurus.dummy_mod as lzm
    assert isinstance(lzm, LazyModule)

    # same again
    import taurus.dummy_mod as lzm
    assert isinstance(lzm, LazyModule)

    # now access a member of the lazy module
    assert lzm.foo == 1

    # ...and check that any subsequent import will return a "real" module,
    # not a lazy one

    import taurus.dummy_mod as lzm
    assert not isinstance(lzm, LazyModule)
    assert isinstance(lzm, ModuleType)
    def testParse(self):
        s = "foo = setuptools.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 == ()
    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, ())
    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, ())
Exemple #31
0
def test_retrieve_name_doesnt_match_module_name():
    ret = pymonkey.get_entry_callables(
        False,
        ('mod-1', ),
        [EntryPoint.parse('mod-1 = patchingmod')],
        'pymonkey_patch',
    )
    assert ret == {'mod-1': patchingmod.pymonkey_patch}
Exemple #32
0
def test_get_entry_callables(entry_s):
    ret = pymonkey.get_entry_callables(
        False,
        ('patchingmod', ),
        [EntryPoint.parse(entry_s)],
        'pymonkey_patch',
    )
    assert ret == {'patchingmod': patchingmod.pymonkey_patch}
Exemple #33
0
 def available_providers(self):
     """Available providers"""
     available_providers = set(self.providers.keys())
     available_providers.update(
         [ep.name for ep in iter_entry_points(self.entry_point)])
     available_providers.update(
         [EntryPoint.parse(c).name for c in self.registered_providers])
     return available_providers
Exemple #34
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
Exemple #35
0
    def list_entry_points(self):
        # copy of default extensions
        eps = list(super(RegistrableExtensionManager, self).list_entry_points())

        # 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
Exemple #36
0
 def create(self):
     """Run create.py to create latest schema version. Record version."""
     scriptname = "%s.create" % (self.evolve_packagename)
     log.info("Run create script %s", scriptname)
     crmodule = EntryPoint.parse("x=%s" % scriptname).resolve()
     crmodule.create(self.connection)
     if self.get_db_version() is None:
         self.set_db_version(self.sw_version)
Exemple #37
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
Exemple #38
0
def make_predicate_restriction(app,
                               global_config,
                               predicate,
                               enabled=True,
                               **kw):
    if isinstance(predicate, STRING_TYPES):
        predicate = EntryPoint.parse('x=%s' % predicate).resolve()
    return PredicateRestriction(app, predicate, enabled, **kw)
Exemple #39
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
def make_predicate_restriction(app,
                               global_config,
                               predicate,
                               enabled=True,
                               **kw):
    if isinstance(predicate, basestring):
        predicate = EntryPoint.parse('x=%s' % predicate).load(False)
    return PredicateRestriction(app, predicate, enabled, **kw)
Exemple #41
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)
Exemple #42
0
    def _parse_cls(cls: Type["FigmentatorSettings"], value: Any) -> EntryPoint:
        """ Parse and validate the model class in EntryPoint format """
        if isinstance(value, str):
            value = EntryPoint.parse(value)
        elif not isinstance(value, EntryPoint):
            raise ValueError(f"Must be str or EntryPoint, not {type(value)}!")

        return value
Exemple #43
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)
Exemple #44
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
    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,
                      testRunner=TeamcityTestRunner,
                      testLoader=loader_class())
 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()
     )
Exemple #47
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()
Exemple #48
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.resolve()()
 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
Exemple #50
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)()
 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 test_plugin_throws_import_error(self, app, pr):
        core = EntryPoint.parse('core = girder_worker:GirderWorkerPlugin')
        core.load = mock.Mock(side_effect=ImportError(
            'Intentionally throw import error'))
        pr.iter_entry_points.return_value = [core]

        main()

        app.conf.update.assert_any_call({'CELERY_IMPORTS': []})
 def register_module(self, name=None, module_name=None, attrs=(), entry_point=None):
     if entry_point:
         ep = EntryPoint.parse(entry_point)
     else:
         ep = EntryPoint(name, module_name, attrs)
     loaded = self._load_one_plugin(ep, invoke_on_load=True, invoke_args=(), invoke_kwds={})
     if loaded:
         self.extensions.append(loaded)
         self.extensions = self.order_extensions(self.extensions)
         self._extensions_by_name = None
    def test_core_plugin(self, app, pr):
        ep = EntryPoint.parse('core = girder_worker:GirderWorkerPlugin')
        ep.load = mock.Mock(return_value=girder_worker.GirderWorkerPlugin)
        pr.iter_entry_points.return_value = [ep]

        main()

        app.conf.update.assert_any_call({'CELERY_IMPORTS':
                                         ['girder_worker.tasks']})
        app.conf.update.assert_any_call({'CELERY_INCLUDE': []})
Exemple #55
0
def make_retry(app, global_conf, **local_conf):
    from pkg_resources import EntryPoint
    tries = int(local_conf.get('tries', 3))
    retryable = local_conf.get('retryable')
    highwater = local_conf.get('highwater', 2<<20)
    tries_write_error = int(local_conf.get('tries_write_error', 1))
    if retryable is not None:
        retryable = [EntryPoint.parse('x=%s' % x).load(False)
                      for x in retryable.split(' ')]
    return Retry(app, tries, retryable=retryable, highwater=highwater,
        tries_write_error=tries_write_error)
Exemple #56
0
def make_retry(app, global_conf, **local_conf):
    from pkg_resources import EntryPoint
    tries = int(local_conf.get('tries', 3))
    retryable = local_conf.get('retryable')
    highwater = local_conf.get('highwater', 2<<20)
    log_after_try_count = int(local_conf.get('log_after_try_count', 1))
    if retryable is not None:
        retryable = [EntryPoint.parse('x=%s' % x).resolve()
                      for x in retryable.split(' ')]
    return Retry(app, tries, retryable=retryable, highwater=highwater,
                 log_after_try_count=log_after_try_count)
 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)
Exemple #58
0
  def execute_pkg_resources(spec):
    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)
    runner()