Пример #1
0
def webassets_init():
    global env

    static_path = get_webassets_path()

    public = config.get(u'ckan.base_public_folder')

    public_folder = os.path.abspath(
        os.path.join(os.path.dirname(__file__), u'..', public))

    base_path = os.path.join(public_folder, u'base')

    env = Environment()
    env.directory = static_path
    env.debug = asbool(config.get(u'debug', False))
    env.url = u'/webassets/'

    add_public_path(base_path, u'/base/')

    logger.debug(u'Base path {0}'.format(base_path))
    create_library(u'vendor', os.path.join(base_path, u'vendor'))

    create_library(u'base', os.path.join(base_path, u'javascript'))

    create_library(u'datapreview', os.path.join(base_path, u'datapreview'))

    create_library(u'css', os.path.join(base_path, u'css'))
Пример #2
0
def webassets_init():
    global env

    static_path = get_webassets_path()

    public = config.get(u'ckan.base_public_folder')

    public_folder = os.path.abspath(os.path.join(
        os.path.dirname(__file__), u'..', public))

    base_path = os.path.join(public_folder, u'base')

    env = Environment()
    env.directory = static_path
    env.debug = config.get(u'debug', False)
    env.url = u'/webassets/'

    env.append_path(base_path, u'/base/')

    logger.debug(u'Base path {0}'.format(base_path))
    create_library(u'vendor', os.path.join(
        base_path, u'vendor'))

    create_library(u'base', os.path.join(base_path, u'javascript'))

    create_library(u'datapreview', os.path.join(base_path, u'datapreview'))

    create_library(u'css', os.path.join(base_path, u'css'))
Пример #3
0
    def load_environment(self):
        """Load an ``Environment`` instance defined in the YAML file.

        Expects the following format::

            directory: ../static
            url: /media
            debug: True
            updater: timestamp
            config:
                compass_bin: /opt/compass
                another_custom_config_value: foo

            bundles:
                bundle-name:
                    filters: sass,cssutils
                    output: cache/default.css
                    contents:
                        - css/jquery.ui.calendar.css
                        - css/jquery.ui.slider.css
        """
        f, filename = self._open()
        try:
            obj = self.yaml.load(f) or {}

            env = Environment()

            # Load environment settings
            for setting in ('debug', 'cache', 'versions', 'url_expire',
                            'auto_build', 'url', 'directory',
                            # TODO: The deprecated values; remove at some point
                            'expire', 'updater'):
                if setting in obj:
                    setattr(env, setting, obj[setting])

            # Treat the 'directory' option special, make it relative to the
            # path of the YAML file, if we know it.
            if filename and 'directory' in env.config:
                env.directory = path.normpath(
                    path.join(path.dirname(filename), env.directory))

            # Load custom config options
            if 'config' in obj:
                env.config.update(obj['config'])

            # Load bundles
            bundles = self._get_bundles(obj.get('bundles', {}))
            for name, bundle in bundles.iteritems():
                env.register(name, bundle)

            return env
        finally:
            f.close()
Пример #4
0
def gen_static():
    # Setup a logger
    log = logging.getLogger('webassets')
    log.addHandler(logging.StreamHandler())
    log.setLevel(logging.DEBUG)
    assets_env = Environment()
    assets_env.directory = "static/"
    assets_env.debug = True
    assets_env.register(bundles)
    cmdenv = CommandLineEnvironment(assets_env, log)
    # This would also work
    cmdenv.build()
Пример #5
0
def register_assets(settings):
    """
    Initialize webassets environment and its bundles.

    Arguments:
        settings (conf.model.SettingsModel): Settings registry instance.

    Returns:
        webassets.Environment: New configured Webasset environment.
    """
    logger = logging.getLogger('optimus')
    if not settings.ENABLED_BUNDLES:
        logger.warning(("Asset registering skipped as there are no enabled "
                        "bundle"))
        return None
    logger.info("Starting asset registering")

    # Assets bundles
    AVAILABLE_BUNDLES = getattr(settings, 'BUNDLES', {})

    # Initialize webassets environment
    assets_env = AssetsEnvironment()
    assets_env.debug = settings.DEBUG
    assets_env.url = settings.STATIC_URL
    assets_env.directory = settings.STATIC_DIR
    assets_env.load_path = [settings.SOURCES_DIR]
    assets_env.cache = settings.WEBASSETS_CACHE
    assets_env.url_expire = settings.WEBASSETS_URLEXPIRE

    #
    assets_env.optimus_registry = AssetRegistry()

    # Register enabled assets bundles
    for bundle_name in settings.ENABLED_BUNDLES:
        logger.debug("Registering bundle: {}".format(bundle_name))

        assets_env.register(bundle_name, AVAILABLE_BUNDLES[bundle_name])
        assets_env.optimus_registry.add_bundle(bundle_name,
                                               AVAILABLE_BUNDLES[bundle_name])

    # When after bundle has been registered we can resolve it
    for bundle_name in settings.ENABLED_BUNDLES:
        logger.info("  Processing: {}".format(
            assets_env[bundle_name].resolve_output()
        ))
        # Avoid to loop on every bundle part if we are not in debug logger
        if logger.getEffectiveLevel() == logging.DEBUG:
            for url in assets_env[bundle_name].urls():
                logger.debug("  - {}".format(url))

    return assets_env
Пример #6
0
def register_assets():
    """
    Initialize webassets environment and its bundles
    
    NOTE: The asset bundles building is lazy, webassets only do building when he is 
          invoked by his template tag **assets** and if it detect that a file in a 
          bundle has changed.
    """
    logger = logging.getLogger('optimus')
    if not settings.ENABLED_BUNDLES:
        logger.warning(
            "Asset registering skipped as there are no enabled bundles")
        return None
    logger.info("Starting asset registering")

    # Assets bundles
    AVAILABLE_BUNDLES = getattr(settings, 'BUNDLES', {})

    # Initialize webassets environment
    assets_env = AssetsEnvironment()
    assets_env.debug = settings.DEBUG
    assets_env.url = settings.STATIC_URL
    assets_env.directory = settings.STATIC_DIR
    assets_env.load_path = [settings.SOURCES_DIR]
    assets_env.cache = settings.WEBASSETS_CACHE

    #
    assets_env.optimus_registry = AssetRegistry()

    # Register enabled assets bundles
    for bundle_name in settings.ENABLED_BUNDLES:
        logger.debug("Registering bundle: %s", bundle_name)
        # Little trick because Bundle does not know their used name in the webassets
        # environment
        AVAILABLE_BUNDLES[bundle_name]._internal_env_name = bundle_name

        assets_env.register(bundle_name, AVAILABLE_BUNDLES[bundle_name])
        assets_env.optimus_registry.add_bundle(AVAILABLE_BUNDLES[bundle_name])

    # for debugging purpopse
    for bundle_name in settings.ENABLED_BUNDLES:
        logger.info(" Processing: %s",
                    assets_env[bundle_name].resolve_output())
        # TODO: conditionnal on the log level to avoid to loop on multiple items if not
        #       in a debug log level
        for url in assets_env[bundle_name].urls():
            logger.debug(" - %s", url)

    return assets_env
Пример #7
0
def scss_compile(static_path):
    webassets = Environment()
    webassets.directory = static_path
    webassets.url = static_path
    webassets.register('css_all', css_all)
    #webassets.manifest = 'cache' if not app.debug else False
    webassets.manifest = False
    webassets.cache = False
    webassets.debug = False
    log = logging.getLogger('webassets')
    log.addHandler(logging.StreamHandler())
    log.setLevel(logging.DEBUG)
    cmdenv = CommandLineEnvironment(webassets, log)
    # This would also work
    cmdenv.build()
Пример #8
0
def register_assets():
    """
    Initialize webassets environment and its bundles
    
    NOTE: The asset bundles building is lazy, webassets only do building when he is 
          invoked by his template tag **assets** and if it detect that a file in a 
          bundle has changed.
    """
    logger = logging.getLogger('optimus')
    if not settings.ENABLED_BUNDLES:
        logger.warning("Asset registering skipped as there are no enabled bundles")
        return None
    logger.info("Starting asset registering")
    
    # Assets bundles
    AVAILABLE_BUNDLES = copy.deepcopy(COMMON_BUNDLES)
    AVAILABLE_BUNDLES.update(getattr(settings, 'EXTRA_BUNDLES', {}))
    
    # Initialize webassets environment
    assets_env = AssetsEnvironment()
    assets_env.debug = settings.DEBUG
    assets_env.url = settings.STATIC_URL
    assets_env.directory = settings.STATIC_DIR
    assets_env.load_path = [settings.SOURCES_DIR]
    assets_env.cache = settings.WEBASSETS_CACHE
    
    #
    assets_env.optimus_registry = AssetRegistry()

    # Register enabled assets bundles
    for bundle_name in settings.ENABLED_BUNDLES:
        logger.debug("Registering bundle: %s", bundle_name)
        # Little trick because Bundle does not know their used name in the webassets 
        # environment
        AVAILABLE_BUNDLES[bundle_name]._internal_env_name = bundle_name
        
        assets_env.register(bundle_name, AVAILABLE_BUNDLES[bundle_name])
        assets_env.optimus_registry.add_bundle(AVAILABLE_BUNDLES[bundle_name])
        
    # for debugging purpopse
    for bundle_name in settings.ENABLED_BUNDLES:
        logger.info(" Processing: %s", assets_env[bundle_name].resolve_output())
        # TODO: conditionnal on the log level to avoid to loop on multiple items if not 
        #       in a debug log level
        for url in assets_env[bundle_name].urls():
            logger.debug(" - %s", url)
    
    return assets_env
Пример #9
0
    def load_environment(self):
        """Load an :class:`Environment` instance defined in the YAML file.

        Expects the following format:

        .. code-block:: yaml

            directory: ../static
            url: /media
            debug: True
            updater: timestamp
            config:
                compass_bin: /opt/compass
                another_custom_config_value: foo

            bundles:
                # ...

        All values, including ``directory`` and ``url`` are optional. The
        syntax for defining bundles is the same as for
        :meth:`~.YAMLLoader.load_bundles`.

        Sample usage::

            from webassets.loaders import YAMLLoader
            loader = YAMLLoader('asset.yml')
            env = loader.load_environment()

            env['some-bundle'].urls()
        """
        f, filename = self._open()
        try:
            obj = self.yaml.load(f) or {}

            env = Environment()

            # Load environment settings
            for setting in ('debug', 'cache', 'versions', 'url_expire',
                            'auto_build', 'url', 'directory', 'manifest',
                            # TODO: The deprecated values; remove at some point
                            'expire', 'updater'):
                if setting in obj:
                    setattr(env, setting, obj[setting])

            # Treat the 'directory' option special, make it relative to the
            # path of the YAML file, if we know it.
            if filename and 'directory' in env.config:
                env.directory = path.normpath(
                    path.join(path.dirname(filename),
                              env.config['directory']))

            # Load custom config options
            if 'config' in obj:
                env.config.update(obj['config'])

            # Load bundles
            bundles = self._get_bundles(obj.get('bundles', {}))
            for name, bundle in six.iteritems(bundles):
                env.register(name, bundle)

            return env
        finally:
            f.close()
Пример #10
0
    def load_environment(self):
        """Load an :class:`Environment` instance defined in the YAML file.

        Expects the following format:

        .. code-block:: yaml

            directory: ../static
            url: /media
            debug: True
            updater: timestamp
            config:
                compass_bin: /opt/compass
                another_custom_config_value: foo

            bundles:
                # ...

        All values, including ``directory`` and ``url`` are optional. The
        syntax for defining bundles is the same as for
        :meth:`~.YAMLLoader.load_bundles`.

        Sample usage::

            from webassets.loaders import YAMLLoader
            loader = YAMLLoader('asset.yml')
            env = loader.load_environment()

            env['some-bundle'].urls()
        """
        f, filename = self._open()
        try:
            obj = self.yaml.load(f) or {}

            env = Environment()

            # Load environment settings
            for setting in (
                    'debug',
                    'cache',
                    'versions',
                    'url_expire',
                    'auto_build',
                    'url',
                    'directory',
                    'manifest',
                    # TODO: The deprecated values; remove at some point
                    'expire',
                    'updater'):
                if setting in obj:
                    setattr(env, setting, obj[setting])

            # Treat the 'directory' option special, make it relative to the
            # path of the YAML file, if we know it.
            if filename and 'directory' in env.config:
                env.directory = path.normpath(
                    path.join(path.dirname(filename), env.config['directory']))

            # Load custom config options
            if 'config' in obj:
                env.config.update(obj['config'])

            # Load bundles
            bundles = self._get_bundles(obj.get('bundles', {}))
            for name, bundle in six.iteritems(bundles):
                env.register(name, bundle)

            return env
        finally:
            f.close()
Пример #11
0
    def load_environment(self):
        """Load an :class:`Environment` instance defined in the YAML file.

        Expects the following format:

        .. code-block:: yaml

            directory: ../static
            url: /media
            debug: True
            updater: timestamp
            filters:
                - my_custom_package.my_filter
            config:
                compass_bin: /opt/compass
                another_custom_config_value: foo

            bundles:
                # ...

        All values, including ``directory`` and ``url`` are optional. The
        syntax for defining bundles is the same as for
        :meth:`~.YAMLLoader.load_bundles`.

        Sample usage::

            from webassets.loaders import YAMLLoader
            loader = YAMLLoader('asset.yml')
            env = loader.load_environment()

            env['some-bundle'].urls()
        """
        f, filename = self._open()
        try:
            obj = self.yaml.load(f) or {}

            env = Environment()

            # Load environment settings
            for setting in ('debug', 'cache', 'versions', 'url_expire',
                            'auto_build', 'url', 'directory', 'manifest', 'load_path',
                            'cache_file_mode',
                            # TODO: The deprecated values; remove at some point
                            'expire', 'updater'):
                if setting in obj:
                    setattr(env, setting, obj[setting])

            # Treat the 'directory' option special, make it relative to the
            # path of the YAML file, if we know it.
            if filename and 'directory' in env.config:
                env.directory = path.normpath(
                    path.join(path.dirname(filename),
                              env.config['directory']))

            # Treat the 'filters' option special, it should resolve the
            # entries as classes and register them to the environment
            if 'filters' in obj:
                try:
                    resolve_dotted = self._get_import_resolver()
                except ImportError:
                    raise EnvironmentError(
                        "In order to use custom filters in the YAMLLoader "
                        "you must install the zope.dottedname package")
                for filter_class in obj['filters']:
                    try:
                        cls = resolve_dotted(filter_class)
                    except ImportError:
                        raise LoaderError("Unable to resolve class %s" % filter_class)
                    if inspect.isclass(cls):
                        register_filter(cls)
                    else:
                        raise LoaderError("Custom filters must be classes "
                            "not modules or functions")

            # Load custom config options
            if 'config' in obj:
                env.config.update(obj['config'])

            # Load bundles
            bundles = self._get_bundles(obj.get('bundles', {}))
            for name, bundle in six.iteritems(bundles):
                env.register(name, bundle)

            return env
        finally:
            f.close()
Пример #12
0
    def load_environment(self):
        """Load an :class:`Environment` instance defined in the YAML file.

        Expects the following format:

        .. code-block:: yaml

            directory: ../static
            url: /media
            debug: True
            updater: timestamp
            filters:
                - my_custom_package.my_filter
            config:
                compass_bin: /opt/compass
                another_custom_config_value: foo

            bundles:
                # ...

        All values, including ``directory`` and ``url`` are optional. The
        syntax for defining bundles is the same as for
        :meth:`~.YAMLLoader.load_bundles`.

        Sample usage::

            from webassets.loaders import YAMLLoader
            loader = YAMLLoader('asset.yml')
            env = loader.load_environment()

            env['some-bundle'].urls()
        """
        f, filename = self._open()
        try:
            obj = self.yaml.load(f) or {}

            env = Environment()

            # Load environment settings
            for setting in (
                    'debug',
                    'cache',
                    'versions',
                    'url_expire',
                    'auto_build',
                    'url',
                    'directory',
                    'manifest',
                    'load_path',
                    'cache_file_mode',
                    # TODO: The deprecated values; remove at some point
                    'expire',
                    'updater'):
                if setting in obj:
                    setattr(env, setting, obj[setting])

            # Treat the 'directory' option special, make it relative to the
            # path of the YAML file, if we know it.
            if filename and 'directory' in env.config:
                env.directory = path.normpath(
                    path.join(path.dirname(filename), env.config['directory']))

            # Treat the 'filters' option special, it should resolve the
            # entries as classes and register them to the environment
            if 'filters' in obj:
                try:
                    resolve_dotted = self._get_import_resolver()
                except ImportError:
                    raise EnvironmentError(
                        "In order to use custom filters in the YAMLLoader "
                        "you must install the zope.dottedname package")
                for filter_class in obj['filters']:
                    try:
                        cls = resolve_dotted(filter_class)
                    except ImportError:
                        raise LoaderError("Unable to resolve class %s" %
                                          filter_class)
                    if inspect.isclass(cls):
                        register_filter(cls)
                    else:
                        raise LoaderError("Custom filters must be classes "
                                          "not modules or functions")

            # Load custom config options
            if 'config' in obj:
                env.config.update(obj['config'])

            # Load bundles
            bundles = self._get_bundles(obj.get('bundles', {}))
            for name, bundle in six.iteritems(bundles):
                env.register(name, bundle)

            return env
        finally:
            f.close()