Exemplo n.º 1
0
Arquivo: env.py Projeto: rs/webassets
def autoload():
    """Find assets by looking for an ``assets`` module within each
    installed application, similar to how, e.g., the admin autodiscover
    process works. This is were this code has been adapted from, too.

    Only runs once.

    TOOD: Not thread-safe!
    TODO: Bring back to status output via callbacks?
    """
    global _APPLICATIONS_LOADED
    if _APPLICATIONS_LOADED:
        return False

    # Import this locally, so that we don't have a global Django
    # dependency.
    from django.conf import settings

    for app in settings.INSTALLED_APPS:
        # For each app, we need to look for an assets.py inside that
        # app's package. We can't use os.path here -- recall that
        # modules may be imported different ways (think zip files) --
        # so we need to get the app's __path__ and look for
        # admin.py on that path.
        #if options.get('verbosity') > 1:
        #    print "\t%s..." % app,

        # Step 1: find out the app's __path__ Import errors here will
        # (and should) bubble up, but a missing __path__ (which is
        # legal, but weird) fails silently -- apps that do weird things
        # with __path__ might need to roll their own registration.
        try:
            app_path = import_module(app).__path__
        except AttributeError:
            #if options.get('verbosity') > 1:
            #    print "cannot inspect app"
            continue

        # Step 2: use imp.find_module to find the app's assets.py.
        # For some reason imp.find_module raises ImportError if the
        # app can't be found but doesn't actually try to import the
        # module. So skip this app if its assetse.py doesn't exist
        try:
            imp.find_module('assets', app_path)
        except ImportError:
            #if options.get('verbosity') > 1:
            #    print "no assets module"
            continue

        # Step 3: import the app's assets file. If this has errors we
        # want them to bubble up.
        import_module("%s.assets" % app)
        #if options.get('verbosity') > 1:
        #    print "assets module loaded"

    _APPLICATIONS_LOADED = True
Exemplo n.º 2
0
def load_builtin_filters():
    from os import path
    import warnings

    current_dir = path.dirname(__file__)
    for entry in os.listdir(current_dir):
        if entry.endswith('.py'):
            name = path.splitext(entry)[0]
        elif path.exists(path.join(current_dir, entry, '__init__.py')):
            name = entry
        else:
            continue

        module_name = 'webassets.filter.%s' % name
        try:
            module = import_module(module_name)
        except Exception, e:
            warnings.warn('Error while loading builtin filter '
                          'module \'%s\': %s' % (module_name, e))
        else:
            for attr_name in dir(module):
                attr = getattr(module, attr_name)
                if inspect.isclass(attr) and issubclass(attr, Filter):
                    if not attr.name:
                        # Skip if filter has no name; those are
                        # considered abstract base classes.
                        continue
                    register_filter(attr)
Exemplo n.º 3
0
def load_builtin_filters():
    from os import path
    import warnings

    current_dir = path.dirname(__file__)
    for entry in os.listdir(current_dir):
        if entry.endswith('.py'):
            name = path.splitext(entry)[0]
        elif path.exists(path.join(current_dir, entry, '__init__.py')):
            name = entry
        else:
            continue

        module_name = 'webassets.filter.%s' % name
        try:
            module = import_module(module_name)
        except Exception, e:
            warnings.warn('Error while loading builtin filter '
                          'module \'%s\': %s' % (module_name, e))
        else:
            for attr_name in dir(module):
                attr = getattr(module, attr_name)
                if inspect.isclass(attr) and issubclass(attr, Filter):
                    if not attr.name:
                        # Skip if filter has no name; those are
                        # considered abstract base classes.
                        continue
                    register_filter(attr)
Exemplo n.º 4
0
 def __init__(self, module_name):
     sys.path.insert(0, '')  # Ensure the current directory is on the path
     try:
         try:
             self.module = import_module(module_name)
         except ImportError, e:
             raise LoaderError(e)
     finally:
         sys.path.pop()
Exemplo n.º 5
0
 def __init__(self, module_name):
     sys.path.insert(0, '')  # Ensure the current directory is on the path
     try:
         try:
             self.module = import_module(module_name)
         except ImportError, e:
             raise LoaderError(e)
     finally:
         sys.path.pop()
Exemplo n.º 6
0
 def __init__(self, module_name):
     if isinstance(module_name, types.ModuleType):
         self.module = module_name
     else:
         sys.path.insert(0, "")  # Ensure the current directory is on the path
         try:
             try:
                 self.module = import_module(module_name)
             except ImportError, e:
                 raise LoaderError(e)
         finally:
             sys.path.pop(0)
Exemplo n.º 7
0
 def __init__(self, module_name):
     if isinstance(module_name, types.ModuleType):
         self.module = module_name
     else:
         sys.path.insert(0,
                         '')  # Ensure the current directory is on the path
         try:
             try:
                 self.module = import_module(module_name)
             except ImportError as e:
                 raise LoaderError(e)
         finally:
             sys.path.pop(0)
Exemplo n.º 8
0
 def __init__(self, module_name):
     if isinstance(module_name, types.ModuleType):
         self.module = module_name
     else:
         sys.path.insert(0, '')  # Ensure the current directory is on the path
         try:
             try:
                 if ":" in module_name:
                     module_name, env = module_name.split(":")
                     self.environment = env
                 self.module = import_module(module_name)
             except ImportError as e:
                 raise LoaderError(e)
         finally:
             sys.path.pop(0)
Exemplo n.º 9
0
def load_builtin_filters():
    from os import path
    import warnings

    current_dir = path.dirname(__file__)
    for name in unique_modules(current_dir):

        module_name = "webassets.filter.%s" % name
        try:
            module = import_module(module_name)
        except Exception as e:
            warnings.warn("Error while loading builtin filter " "module '%s': %s" % (module_name, e))
        else:
            for attr_name in dir(module):
                attr = getattr(module, attr_name)
                if inspect.isclass(attr) and issubclass(attr, Filter):
                    if not attr.name:
                        # Skip if filter has no name; those are
                        # considered abstract base classes.
                        continue
                    register_filter(attr)
Exemplo n.º 10
0
def load_builtin_filters():
    from os import path
    import warnings

    current_dir = path.dirname(__file__)
    for name in unique_modules(current_dir):

        module_name = 'webassets.filter.%s' % name
        try:
            module = import_module(module_name)
        except Exception as e:
            warnings.warn('Error while loading builtin filter '
                          'module \'%s\': %s' % (module_name, e))
        else:
            for attr_name in dir(module):
                attr = getattr(module, attr_name)
                if inspect.isclass(attr) and issubclass(attr, Filter):
                    if not attr.name:
                        # Skip if filter has no name; those are
                        # considered abstract base classes.
                        continue
                    register_filter(attr)
Exemplo n.º 11
0
def load_builtin_filters():
    from os import path
    import warnings

    # load modules to work based with and without pyinstaller
    # from: https://github.com/webcomics/dosage/blob/master/dosagelib/loader.py
    # see: https://github.com/pyinstaller/pyinstaller/issues/1905

    # load modules using iter_modules()
    # (should find all filters in normal build, but not pyinstaller)
    prefix = __name__ + '.'
    module_names = [m[1] for m in pkgutil.iter_modules(__path__, prefix)]

    # special handling for PyInstaller
    importers = map(pkgutil.get_importer, __path__)
    toc = set()
    for i in importers:
        if hasattr(i, 'toc'):
            toc |= i.toc
    for elm in toc:
        if elm.startswith(prefix):
            module_names.append(elm)

    for module_name in module_names:
        #module_name = 'webassets.filter.%s' % name
        try:
            module = import_module(module_name)
        except Exception as e:
            warnings.warn('Error while loading builtin filter '
                          'module \'%s\': %s' % (module_name, e))
        else:
            for attr_name in dir(module):
                attr = getattr(module, attr_name)
                if inspect.isclass(attr) and issubclass(attr, Filter):
                    if not attr.name:
                        # Skip if filter has no name; those are
                        # considered abstract base classes.
                        continue
                    register_filter(attr)
Exemplo n.º 12
0
def load_builtin_filters():
    from os import path
    import warnings

    # load modules to work based with and without pyinstaller
    # from: https://github.com/webcomics/dosage/blob/master/dosagelib/loader.py
    # see: https://github.com/pyinstaller/pyinstaller/issues/1905

    # load modules using iter_modules()
    # (should find all filters in normal build, but not pyinstaller)
    prefix = __name__ + '.'
    module_names = [m[1] for m in pkgutil.iter_modules(__path__, prefix)]

    # special handling for PyInstaller
    importers = map(pkgutil.get_importer, __path__)
    toc = set()
    for i in importers:
        if hasattr(i, 'toc'):
            toc |= i.toc
    for elm in toc:
        if elm.startswith(prefix):
            module_names.append(elm)

    for module_name in module_names:
        #module_name = 'webassets.filter.%s' % name
        try:
            module = import_module(module_name)
        except Exception as e:
            warnings.warn('Error while loading builtin filter '
                          'module \'%s\': %s' % (module_name, e))
        else:
            for attr_name in dir(module):
                attr = getattr(module, attr_name)
                if inspect.isclass(attr) and issubclass(attr, Filter):
                    if not attr.name:
                        # Skip if filter has no name; those are
                        # considered abstract base classes.
                        continue
                    register_filter(attr)
Exemplo n.º 13
0
def autoload():
    """Find assets by looking for an ``assets`` module within each
    installed application, similar to how, e.g., the admin autodiscover
    process works. This is were this code has been adapted from, too.

    Only runs once.

    TOOD: Not thread-safe!
    TODO: Bring back to status output via callbacks?
    """
    global _ASSETS_LOADED
    if _ASSETS_LOADED:
        return False

    # Import this locally, so that we don't have a global Django
    # dependency.
    from django.conf import settings

    for app in settings.INSTALLED_APPS:
        # For each app, we need to look for an assets.py inside that
        # app's package. We can't use os.path here -- recall that
        # modules may be imported different ways (think zip files) --
        # so we need to get the app's __path__ and look for
        # admin.py on that path.
        #if options.get('verbosity') > 1:
        #    print "\t%s..." % app,

        # Step 1: find out the app's __path__ Import errors here will
        # (and should) bubble up, but a missing __path__ (which is
        # legal, but weird) fails silently -- apps that do weird things
        # with __path__ might need to roll their own registration.
        try:
            app_path = import_module(app).__path__
        except AttributeError:
            #if options.get('verbosity') > 1:
            #    print "cannot inspect app"
            continue

        # Step 2: use imp.find_module to find the app's assets.py.
        # For some reason imp.find_module raises ImportError if the
        # app can't be found but doesn't actually try to import the
        # module. So skip this app if its assetse.py doesn't exist
        try:
            imp.find_module('assets', app_path)
        except ImportError:
            #if options.get('verbosity') > 1:
            #    print "no assets module"
            continue

        # Step 3: import the app's assets file. If this has errors we
        # want them to bubble up.
        import_module("%s.assets" % app)
        #if options.get('verbosity') > 1:
        #    print "assets module loaded"

    # Look for an assets.py at the project level
    try:
        import_module('assets')
    except ImportError:
        # not found, just ignore
        pass

    _ASSETS_LOADED = True