Exemple #1
0
def importhook_setup(monkeypatch, request):
    # we clear this out for various reasons.  The most important one is
    # that a real flaskext could be in there which would disable our
    # fake package.  Secondly we want to make sure that the flaskext
    # import hook does not break on reloading.
    for entry, value in list(sys.modules.items()):
        if (
            entry.startswith('flaskr.ext.') or
            entry.startswith('flask_') or
            entry.startswith('flaskext.') or
            entry == 'flaskext'
        ) and value is not None:
            monkeypatch.delitem(sys.modules, entry)
    from flask import ext
    reload_module(ext)

    # reloading must not add more hooks
    import_hooks = 0
    for item in sys.meta_path:
        cls = type(item)
        if cls.__module__ == 'flaskr.exthook' and \
           cls.__name__ == 'ExtensionImporter':
            import_hooks += 1
    assert import_hooks == 1

    def teardown():
        from flask import ext
        for key in ext.__dict__:
            assert '.' not in key

    request.addfinalizer(teardown)
Exemple #2
0
def importhook_setup(monkeypatch, request):
    # we clear this out for various reasons.  The most important one is
    # that a real flaskext could be in there which would disable our
    # fake package.  Secondly we want to make sure that the flaskext
    # import hook does not break on reloading.
    for entry, value in list(sys.modules.items()):
        if (entry.startswith('flask.ext.') or entry.startswith('flask_')
                or entry.startswith('flaskext.')
                or entry == 'flaskext') and value is not None:
            monkeypatch.delitem(sys.modules, entry)
    from flask import ext
    reload_module(ext)

    # reloading must not add more hooks
    import_hooks = 0
    for item in sys.meta_path:
        cls = type(item)
        if cls.__module__ == 'flask.exthook' and \
           cls.__name__ == 'ExtensionImporter':
            import_hooks += 1
    assert import_hooks == 1

    def teardown():
        from flask import ext
        for key in ext.__dict__:
            assert '.' not in key

    request.addfinalizer(teardown)
Exemple #3
0
    def _reload(self, name):
        if len(name) == 0: return

        try:
            module = importlib.import_module(name)
            logging.debug("reloading %s" % module)
            reload_module(module)
            self._reload('.'.join(module.__name__.split('.')[:-1]))
        except:
            logging.warning(sys.exc_info()[0])
Exemple #4
0
    def junk(self):
        logging.debug("module name: %s" % self.module_name)
        module = importlib.import_module(self.module_name)
        logging.debug("reloading %s" % module)
        reload_module(module)
        if module.__package__ is None: return

        package = importlib.import_module(module.__package__)
        #package = __import__(module.__package__)
        logging.debug("reloading %s" % package)
        reload_module(package)
Exemple #5
0
    def _yield_movie_sub_stage_from_all_providers(self, query, full_path):
        """ Will yield MovieSubStages one provider at a time (like a generator).
            The function will proceed to the next provider if the current one 
            failed to return a result.

            The return value is a two-dim array (each row contains result from
            one provider).
        """
        reload_module(SubProviders)
        while setNextSubProvider():
            _stages = QuerySubStage(getSubProvider().PROVIDER_NAME, 
                                    query, full_path).getMovieSubStages()
            # Yield only if we got something.
            if _stages:
                yield _stages
Exemple #6
0
def resolve_name(import_name, silent=False, reload=False):
    """Imports an object based on a string.  This is useful if you want to
    use import paths as endpoints or something similar.  An import path can
    be specified either in dotted notation (``xml.sax.saxutils.escape``)
    or with a colon as object delimiter (``xml.sax.saxutils:escape``).

    If `silent` is True the return value will be `None` if the import fails.

    :param import_name: the dotted name for the object to import.
    :param silent: if set to `True` import errors are ignored and
                   `None` is returned instead.
    :param reload: if set to `True` modules that are already loaded will be
                   reloaded
    :return: imported object
    """
    # force the import name to automatically convert to strings
    import_name = to_string(import_name)
    try:
        if ':' in import_name:
            module, obj = import_name.split(':', 1)
        elif '.' in import_name and import_name not in sys.modules:
            module, obj = import_name.rsplit('.', 1)
        else:
            module, obj = import_name, None
            # __import__ is not able to handle basestring strings in the
            # fromlist

        mod = None
        # if the module is a package
        if reload and module in sys.modules:
            try:
                importlib.invalidate_caches()
            except Exception:
                pass
            try:
                mod = reload_module(sys.modules[module])
            except Exception:
                pass
        if not mod:
            if not obj:
                return __import__(module)
            try:
                mod = __import__(module, None, None, [obj])
            except ImportError:
                if ':' in import_name:
                    raise
                return __import__(import_name)
        if not obj:
            return mod
        try:
            return getattr(mod, obj)
        except AttributeError:
            # support importing modules not yet set up by the parent module
            # (or package for that matter)
            if ':' in import_name:
                raise
            return __import__(import_name)
    except ImportError as e:
        if not silent:
            raise ImportStringError(import_name, e), None, sys.exc_info()[2]
Exemple #7
0
def resolve_name(import_name, silent=False, reload=False):
    """Imports an object based on a string.  This is useful if you want to
    use import paths as endpoints or something similar.  An import path can
    be specified either in dotted notation (``xml.sax.saxutils.escape``)
    or with a colon as object delimiter (``xml.sax.saxutils:escape``).

    If `silent` is True the return value will be `None` if the import fails.

    :param import_name: the dotted name for the object to import.
    :param silent: if set to `True` import errors are ignored and
                   `None` is returned instead.
    :param reload: if set to `True` modules that are already loaded will be
                   reloaded
    :return: imported object
    """
    # force the import name to automatically convert to strings
    import_name = bytestring(import_name)
    try:
        if ':' in import_name:
            module, obj = import_name.split(':', 1)
        elif '.' in import_name and import_name not in sys.modules:
            module, obj = import_name.rsplit('.', 1)
        else:
            module, obj = import_name, None
            # __import__ is not able to handle unicode strings in the fromlist

        mod = None
        # if the module is a package
        if reload and module in sys.modules:
            try:
                importlib.invalidate_caches()
            except Exception:
                pass
            try:
                mod = reload_module(sys.modules[module])
            except Exception:
                pass
        if not mod:
            if not obj:
                return __import__(module)
            try:
                mod = __import__(module, None, None, [obj])
            except ImportError:
                if ':' in import_name:
                    raise
                return __import__(import_name)
        if not obj:
            return mod
        try:
            return getattr(mod, obj)
        except AttributeError:
            # support importing modules not yet set up by the parent module
            # (or package for that matter)
            if ':' in import_name:
                raise
            return __import__(import_name)
    except ImportError as e:
        if not silent:
            raise_with_tb(ImportStringError(import_name, e))
Exemple #8
0
    def _get_movie_sub_stage_from_all_providers(self, query, full_path):
        """ Will return MovieSubStages all together (like a list). 
        
            The return value is a two-dim array with a single row in it, a row
            that contains the results from all the providers.
        """
        reload_module(SubProviders)
        # Two-dim array with single row.
        movie_sub_stages = [[]]
        while setNextSubProvider():
            _stages = QuerySubStage(
                getSubProvider().PROVIDER_NAME,
                query, 
                full_path).getMovieSubStages()
            # We might be getting empty list, so append is performed if it's
            # not empty.
            if _stages:
                movie_sub_stages[0].extend(_stages)

        return movie_sub_stages
Exemple #9
0
def get ( name ):
    if name in plugins:
        return plugins [ name ]

    requester = extract_calling_plugin_name ()
    if requester and requester not in registered_plugins:
        raise Exception ( "Plugin %s didn't register!" % requester )

    print ( "Loading plugin %s..." % name )
    plugin = importlib.import_module ( "plugins.%s" % name )
    if name not in registered_plugins:
        reload_module ( plugin )
    plugins [ name ] = plugin

    if name not in registered_plugins:
        raise Exception ( "Plugin %s didn't register!" % name )

    if requester:
        add_dependency ( requester, name )

    return plugins [ name ]
Exemple #10
0
    def setup(self):
        # we clear this out for various reasons.  The most important one is
        # that a real flaskext could be in there which would disable our
        # fake package.  Secondly we want to make sure that the flaskext
        # import hook does not break on reloading.
        for entry, value in list(sys.modules.items()):
            if (entry.startswith('flask.ext.') or entry.startswith('flask_')
                    or entry.startswith('flaskext.')
                    or entry == 'flaskext') and value is not None:
                sys.modules.pop(entry, None)
        from flask import ext
        reload_module(ext)

        # reloading must not add more hooks
        import_hooks = 0
        for item in sys.meta_path:
            cls = type(item)
            if cls.__module__ == 'flask.exthook' and \
               cls.__name__ == 'ExtensionImporter':
                import_hooks += 1
        self.assert_equal(import_hooks, 1)
Exemple #11
0
Fichier : ext.py Projet : 005/flask
    def setup(self):
        # we clear this out for various reasons.  The most important one is
        # that a real flaskext could be in there which would disable our
        # fake package.  Secondly we want to make sure that the flaskext
        # import hook does not break on reloading.
        for entry, value in list(sys.modules.items()):
            if (entry.startswith('flask.ext.') or
                entry.startswith('flask_') or
                entry.startswith('flaskext.') or
                entry == 'flaskext') and value is not None:
                sys.modules.pop(entry, None)
        from flask import ext
        reload_module(ext)

        # reloading must not add more hooks
        import_hooks = 0
        for item in sys.meta_path:
            cls = type(item)
            if cls.__module__ == 'flask.exthook' and \
               cls.__name__ == 'ExtensionImporter':
                import_hooks += 1
        self.assert_equal(import_hooks, 1)