Esempio n. 1
0
def register_assets(assets: Environment):
    assets.register('scss_campaign',
                    'scss/campaign/campaign.scss',
                    filters='pyscss',
                    output='css/campaign.css')

    assets.register('scss_handout',
                    'scss/campaign/handout.scss',
                    filters='pyscss',
                    output='css/handout.css')
Esempio n. 2
0
    def test_story_of_two_envs(self):
        """If an app is served by multiple processes, and auto_build is used,
        if one process rebuilds a bundle, the other one must know (instead of
        continuing to serve the old version.

        For this reason, if auto_build is enabled, the version will always be
        determined anew in every request, rather than using a cached version.
        (This is the reason why get_version() has the refresh=True option).
        """
        # Prepare a manifest and bundle in env1. Use a file manifest
        env1 = self.env
        env1.url_expire = True
        bundle1 = self.mkbundle('in', output='out-%(version)s')

        # Prepare an identical setup, simulating the second process
        env2 = Environment(env1.directory, env1.url)
        env2.config.update(copy.deepcopy(env1.config._dict))
        bundle2  = self.mkbundle('in', output='out-%(version)s')
        bundle2.env = env2

        # Both have auto build enabled, both are using the same manifest
        env1.auto_build = env2.auto_build = True
        env1.manifest = 'file'; env2.manifest = 'file'
        env1.updater = 'timestamp'; env2.updater = 'timestamp'

        # Do the initial build, both envs think they are running the
        # latest version.
        env1.versions.version = bundle1.version = 'old'
        env2.versions.version = bundle2.version = 'old'
        bundle1.build()
        assert env2.updater.needs_rebuild(bundle2, env2) == False

        # At this point, both return the old version in urls
        assert bundle1.urls() ==['/out-old?old']
        assert bundle2.urls() ==['/out-old?old']

        # Now let env1 do an update.
        env1.versions.version = 'new'
        bundle1.build(force=True)
        assert bundle1.urls() == ['/out-new?new']

        # If auto_build is False, env2 will continue to use the old version.
        env2.auto_build = False
        assert bundle2.urls() == ['/out-old?old']
        # However, if auto_build is True, env2 will know the new version.
        # This is because env1 wrote it to the manifest during build.
        env2.auto_build = True
        assert bundle2.get_version() == 'old'    # urls() causes the refresh
        assert bundle2.urls() == ['/out-new?new']
        assert bundle2.get_version() == 'new'

        # The reverse works as well.
        env2.versions.version = 'latest'
        bundle2.build(force=True)
        assert bundle1.urls() == bundle2.urls() == ['/out-latest?latest']
Esempio n. 3
0
class AssetsExtension(object):
    css_all = Bundle('css/main.css', 'css/gh-fork-ribbon.css', 'css/flags.css',
                     output='css/all.min.css', filters='yui_css')
    css_admin = Bundle('css/selectize.css', 'css/datetimepicker.css',
                       output='css/admin.min.css', filters='yui_css')
    css_ie = Bundle('css/gh-fork-ribbon.ie.css',
                    output='css/ie.min.css', filters='yui_css')

    js_all = Bundle('js/main.js', output='js/all.min.js', filters='yui_js')
    js_admin = Bundle('js/admin.js', 'js/selectize.js', 'js/moment.js',
                      'js/datetimepicker.js', output='js/admin.min.js',
                      filters='yui_js')
    js_maps = Bundle('js/gmap3.js', 'js/loadgpx.js',
                     output='js/maps.min.js', filters='yui_js')

    def __init__(self, app=None):
        self.app = app
        self.env = Environment(
            join(dirname(__file__), '../static'), '/static',
            auto_build=app.debug if app else False,
            debug=(True if app.debug else 'merge') if app else False,
            manifest="cache" if app and app.debug else None,
            cache=app.debug if app else False,
            yui_compressor_path='/usr/share/yui-compressor/yui-compressor.jar')

        if self.app:
            self.app.jinja_env.add_extension(extension=JinjaExtension)
            self.app.jinja_env.assets_environment = self.env

        self._register_bundles()

    def _register_bundles(self):
        for key in dir(self):
            value = getattr(self, key)
            if isinstance(value, Bundle):
                self.env.register(key, value)

    def build(self):
        logger = getLogger('webassets')
        logger.addHandler(StreamHandler())
        logger.setLevel(DEBUG)

        cli_environment = CommandLineEnvironment(self.env, logger)
        cli_environment.invoke("build", args={})
Esempio n. 4
0
def get_webassets_env_from_settings(settings, prefix='webassets'):
    """This function will take all webassets.* parameters, and
    call the ``Environment()`` constructor with kwargs passed in.

    The only two parameters that are not passed as keywords are:

    * base_dir
    * base_url

    which are passed in positionally.

    Read the ``WebAssets`` docs for ``Environment`` for more details.
    """
    # Make a dictionary of the webassets.* elements...
    kwargs = {}  # assets settings
    cut_prefix = len(prefix) + 1
    for k in settings:
        if k.startswith(prefix):
            kwargs[k[cut_prefix:]] = settings[k]

    if 'base_dir' not in kwargs:
        raise Exception(
            "You need to provide webassets.base_dir in your configuration")
    if 'base_url' not in kwargs:
        raise Exception(
            "You need to provide webassets.base_url in your configuration")

    asset_dir = kwargs.pop('base_dir')
    asset_url = kwargs.pop('base_url')

    if 'debug' in kwargs:
        dbg = kwargs['debug'].lower()

        if dbg == 'false' or dbg == 'true':
            dbg = asbool(dbg)

        kwargs['debug'] = dbg

    if 'cache' in kwargs:
        kwargs['cache'] = asbool(kwargs['cache'])

    # 'updater' is just passed in...

    if 'jst_compiler' in kwargs:
        kwargs['JST_COMPILER'] = kwargs.pop('jst_compiler')

    if 'manifest' in kwargs:
        kwargs['manifest'] = asbool(kwargs.pop('manifest'))

    assets_env = Environment(asset_dir, asset_url, **kwargs)

    return assets_env
Esempio n. 5
0
    def __init__(self, conf, env):
        self.conf = conf
        self.env = env

        self.environment = Environment(directory=conf.theme[0],
                                       url=env.path,
                                       updater='acrylic',
                                       versions='acrylic',
                                       cache=core.cache.cache_dir,
                                       load_path=[conf.theme[0]])

        # fix output directory creation
        self.environment.resolver = Acrylresolver(conf, self.environment)
Esempio n. 6
0
    def test_load_environment_with_prefix(self):
        import types
        module = types.ModuleType("testing")
        module2 = types.ModuleType("testing2")
        module.environment = Environment() # default name
        module2.assets = Environment()
        sys.modules["testing"] = module
        sys.modules["testing2"] = module2

        loader = PythonLoader("testing")
        env = loader.load_environment()
        assert env == module.environment

        loader2 = PythonLoader("testing:environment")
        assert loader2.environment == "environment"
        env2 = loader2.load_environment()
        assert env2 == module.environment

        loader3 = PythonLoader("testing2:assets")
        assert loader3.environment == "assets"
        env3 = loader3.load_environment()
        assert env3 == module2.assets
Esempio n. 7
0
    def test_story_of_two_envs(self):
        """If an app is served by multiple processes, and auto_build is used,
        if one process rebuilds a bundle, the other one must know (instead of
        continuing to serve the old version.

        For this reason, if auto_build is enabled, the version will always be
        determined anew in every request, rather than using a cached version.
        (This is the reason why get_version() has the refresh=True option).
        """
        # Prepare a manifest and bundle in env1. Use a file manifest
        env1 = self.env
        env1.url_expire = True
        bundle1 = self.mkbundle('in', output='out-%(version)s')

        # Prepare an identical setup, simulating the second process
        env2 = Environment(env1.directory, env1.url)
        env2.config.update(copy.deepcopy(env1.config._dict))
        bundle2 = self.mkbundle('in', output='out-%(version)s')
        bundle2.env = env2

        # Both have auto build enabled, both are using the same manifest
        env1.auto_build = env2.auto_build = True
        env1.manifest = 'file'
        env2.manifest = 'file'
        env1.updater = 'timestamp'
        env2.updater = 'timestamp'

        # Do the initial build, both envs think they are running the
        # latest version.
        env1.versions.version = bundle1.version = 'old'
        env2.versions.version = bundle2.version = 'old'
        bundle1.build()
        assert env2.updater.needs_rebuild(bundle2, env2) == False

        # At this point, both return the old version in urls
        assert bundle1.urls() == ['/out-old?old']
        assert bundle2.urls() == ['/out-old?old']

        # Now let env1 do an update.
        env1.versions.version = 'new'
        bundle1.build(force=True)
        assert bundle1.urls() == ['/out-new?new']

        # If auto_build is False, env2 will continue to use the old version.
        env2.auto_build = False
        assert bundle2.urls() == ['/out-old?old']
        # However, if auto_build is True, env2 will know the new version.
        # This is because env1 wrote it to the manifest during build.
        env2.auto_build = True
        assert bundle2.get_version() == 'old'  # urls() causes the refresh
        assert bundle2.urls() == ['/out-new?new']
        assert bundle2.get_version() == 'new'

        # The reverse works as well.
        env2.versions.version = 'latest'
        bundle2.build(force=True)
        assert bundle1.urls() == bundle2.urls() == ['/out-latest?latest']
Esempio n. 8
0
    def __init__(self, app=None):
        self.app = app
        self.env = Environment(
            join(dirname(__file__), '../static'), '/static',
            auto_build=app.debug if app else False,
            debug=(True if app.debug else 'merge') if app else False,
            manifest="cache" if app and app.debug else None,
            cache=app.debug if app else False,
            yui_compressor_path='/usr/share/yui-compressor/yui-compressor.jar')

        if self.app:
            self.app.jinja_env.add_extension(extension=JinjaExtension)
            self.app.jinja_env.assets_environment = self.env

        self._register_bundles()
Esempio n. 9
0
def register_assets(assets: Environment):
    assets.register('scss_character',
                    'scss/character/character.scss',
                    filters='pyscss',
                    output='css/character.css')
    assets.register('scss_character_coc7e',
                    'scss/character/character_coc7e.scss',
                    filters='pyscss',
                    output='css/character_coc7e.css')

    assets.register('scss_character_tftl',
                    'scss/character/character_tftl.scss',
                    filters='pyscss',
                    output='css/character_tftl.css')
Esempio n. 10
0
def get_webassets_env_from_settings(settings, prefix='webassets'):
    """This function will take all webassets.* parameters, and
    call the ``Environment()`` constructor with kwargs passed in.

    The only two parameters that are not passed as keywords are:

    * base_dir
    * base_url

    which are passed in positionally.

    Read the ``WebAssets`` docs for ``Environment`` for more details.
    """
    # Make a dictionary of the webassets.* elements...
    kwargs = {}   # assets settings
    cut_prefix = len(prefix) + 1
    for k in settings:
        if k.startswith(prefix):
            val = settings[k]
            if isinstance(val, six.string_types):
                if val.lower() in auto_booly:
                    val = asbool(val)
                elif val.lower().startswith('json:') and k[cut_prefix:] != 'manifest':
                    val = json.loads(val[5:])
            kwargs[k[cut_prefix:]] = val

    if 'base_dir' not in kwargs:
        raise Exception("You need to provide webassets.base_dir in your configuration")
    if 'base_url' not in kwargs:
        raise Exception("You need to provide webassets.base_url in your configuration")

    asset_dir = kwargs.pop('base_dir')
    asset_url = kwargs.pop('base_url')

    if ':' in asset_dir:
        try:
            asset_dir = AssetResolver(None).resolve(asset_dir).abspath()
        except ImportError:
            pass

    if 'debug' in kwargs:
        kwargs['debug'] = maybebool(kwargs['debug'])

    if 'cache' in kwargs:
        cache = kwargs['cache'] = maybebool(kwargs['cache'])

        if cache and isinstance(cache, six.string_types) and not path.isdir(cache):
            makedirs(cache)

    # 'updater' is just passed in...

    if 'auto_build' in kwargs:
        kwargs['auto_build'] = maybebool(kwargs['auto_build'])

    if 'jst_compiler' in kwargs:
        kwargs['JST_COMPILER'] = kwargs.pop('jst_compiler')

    if 'jst_namespace' in kwargs:
        kwargs['JST_NAMESPACE'] = kwargs.pop('jst_namespace')

    if 'manifest' in kwargs:
        kwargs['manifest'] = maybebool(kwargs['manifest'])

    if 'url_expire' in kwargs:
        kwargs['url_expire'] = maybebool(kwargs['url_expire'])

    if 'static_view' in kwargs:
        kwargs['static_view'] = asbool(kwargs['static_view'])
    else:
        kwargs['static_view'] = False

    if 'cache_max_age' in kwargs:
        kwargs['cache_max_age'] = int(kwargs.pop('cache_max_age'))
    else:
        kwargs['cache_max_age'] = None

    if 'load_path' in kwargs:
        # force load_path to be an array and split on whitespace
        if not isinstance(kwargs['load_path'], list):
            kwargs['load_path'] = kwargs['load_path'].split()

    paths = kwargs.pop('paths', None)

    if 'bundles' in kwargs:
        if isinstance(kwargs['bundles'], six.string_types):
            kwargs['bundles'] = kwargs['bundles'].split()

    bundles = kwargs.pop('bundles', None)

    assets_env = Environment(asset_dir, asset_url, **kwargs)

    if paths is not None:
        for map_path, map_url in json.loads(paths).items():
            assets_env.append_path(map_path, map_url)

    def yaml_stream(fname):
        if path.exists(fname):
            return open(fname, 'rb')
        else:
            return assets_env.resolver.resolver.resolve(fname).stream()

    if isinstance(bundles, list):
        loaded = {}
        for bpath in bundles:
            with closing(yaml_stream(bpath)) as s:
                loader = YAMLLoader(s)
                loaded.update(loader.load_bundles())
        assets_env.register(loaded)
    elif isinstance(bundles, dict):
        assets_env.register(bundles)

    return assets_env
Esempio n. 11
0
def test_builtin_manifest_accessors():
    env = Environment('', '')
    assert get_manifest('cache', env).__class__ == CacheManifest
    assert get_manifest('file', env).__class__ == FileManifest
    assert get_manifest('file:/tmp/foo', env).filename == '/tmp/foo'
Esempio n. 12
0
def get_webassets_env_from_settings(settings, prefix='webassets'):
    """This function will take all webassets.* parameters, and
    call the ``Environment()`` constructor with kwargs passed in.

    The only two parameters that are not passed as keywords are:

    * base_dir
    * base_url

    which are passed in positionally.

    Read the ``WebAssets`` docs for ``Environment`` for more details.
    """
    # Make a dictionary of the webassets.* elements...
    kwargs = {}   # assets settings
    cut_prefix = len(prefix) + 1
    for k in settings:
        if k.startswith(prefix):
            val = settings[k]
            if isinstance(val, six.string_types):
                if val.lower() in auto_booly:
                    val = asbool(val)
                elif val.lower().startswith('json:') and k[cut_prefix:] != 'manifest':
                    val = json.loads(val[5:])
            kwargs[k[cut_prefix:]] = val

    if 'base_dir' not in kwargs:
        raise Exception("You need to provide webassets.base_dir in your configuration")
    if 'base_url' not in kwargs:
        raise Exception("You need to provide webassets.base_url in your configuration")

    asset_dir = kwargs.pop('base_dir')
    asset_url = kwargs.pop('base_url')

    if ':' in asset_dir:
        try:
            asset_dir = AssetResolver(None).resolve(asset_dir).abspath()
        except ImportError:
            pass

    if 'debug' in kwargs:
        kwargs['debug'] = maybebool(kwargs['debug'])

    if 'cache' in kwargs:
        cache = kwargs['cache'] = maybebool(kwargs['cache'])

        if cache and isinstance(cache, six.string_types) and not path.isdir(cache):
            makedirs(cache)

    # 'updater' is just passed in...

    if 'auto_build' in kwargs:
        kwargs['auto_build'] = maybebool(kwargs['auto_build'])

    if 'jst_compiler' in kwargs:
        kwargs['JST_COMPILER'] = kwargs.pop('jst_compiler')

    if 'jst_namespace' in kwargs:
        kwargs['JST_NAMESPACE'] = kwargs.pop('jst_namespace')

    if 'manifest' in kwargs:
        kwargs['manifest'] = maybebool(kwargs['manifest'])

    if 'url_expire' in kwargs:
        kwargs['url_expire'] = maybebool(kwargs['url_expire'])

    if 'static_view' in kwargs:
        kwargs['static_view'] = asbool(kwargs['static_view'])
    else:
        kwargs['static_view'] = False

    if 'cache_max_age' in kwargs:
        kwargs['cache_max_age'] = int(kwargs.pop('cache_max_age'))
    else:
        kwargs['cache_max_age'] = None

    if 'load_path' in kwargs:
        # force load_path to be an array and split on whitespace
        if not isinstance(kwargs['load_path'], list):
            kwargs['load_path'] = kwargs['load_path'].split()

    paths = kwargs.pop('paths', None)

    if 'bundles' in kwargs:
        if isinstance(kwargs['bundles'], six.string_types):
            kwargs['bundles'] = kwargs['bundles'].split()

    bundles = kwargs.pop('bundles', None)

    assets_env = Environment(asset_dir, asset_url, **kwargs)

    if paths is not None:
        for map_path, map_url in json.loads(paths).items():
            assets_env.append_path(map_path, map_url)

    def yaml_stream(fname):
        if path.exists(fname):
            return open(fname, 'rb')
        else:
            return assets_env.resolver.resolver.resolve(fname).stream()

    if isinstance(bundles, list):
        loaded = {}
        for bpath in reversed(bundles):
            with closing(yaml_stream(bpath)) as s:
                loader = YAMLLoader(s)
                loaded.update(loader.load_bundles())
        assets_env.register(loaded)
    elif isinstance(bundles, dict):
        assets_env.register(bundles)

    return assets_env
Esempio n. 13
0
        logging.warning("Config not found! Please copy config.default.json to config.json")
        sys.exit()


config = get_config()

app = Flask(__name__, static_url_path='')

app.jinja_loader = jinja2.FileSystemLoader([
    os.path.abspath(os.path.join(app.root_path, "templates")),
    os.path.abspath(os.path.abspath("static-templates")),
])

push = PushBullet(config["pushbullet"]["api-key"])

assets = Environment(os.path.abspath(os.path.join("static", "assets")), "assets/")
assets.append_path(app.static_folder, "/")
assets.auto_build = False
assets.url_expire = True
assets.cache = False
assets.manifest = "file:{}".format(os.path.abspath(os.path.join("static", ".webassets-manifest")))

# Create assets
assets.register('bootstrap-css', 'css/bootstrap.css',
                filters='cssmin', output='bootstrap.css')

assets.register('sidebar-css', 'css/bootstrap.css', 'css/shared-sidebar.css',
                filters='cssmin', output='shared.css')

assets.register('frc-css', 'css/bootstrap.css', 'css/frc.css',
                filters='cssmin', output='frc.css')
Esempio n. 14
0
def get_webassets_env_from_settings(settings, prefix='webassets'):
    """This function will take all webassets.* parameters, and
    call the ``Environment()`` constructor with kwargs passed in.

    The only two parameters that are not passed as keywords are:

    * base_dir
    * base_url

    which are passed in positionally.

    Read the ``WebAssets`` docs for ``Environment`` for more details.
    """
    # Make a dictionary of the webassets.* elements...
    kwargs = {}  # assets settings
    cut_prefix = len(prefix) + 1
    for k in settings:
        if k.startswith(prefix):
            val = settings[k]
            if isinstance(val, six.string_types):
                if val.lower() in auto_booly:
                    val = asbool(val)
                elif val.lower().startswith('json:'):
                    val = json.loads(val[5:])
            kwargs[k[cut_prefix:]] = val

    if 'base_dir' not in kwargs:
        raise Exception(
            "You need to provide webassets.base_dir in your configuration")
    if 'base_url' not in kwargs:
        raise Exception(
            "You need to provide webassets.base_url in your configuration")

    asset_dir = kwargs.pop('base_dir')
    asset_url = kwargs.pop('base_url')

    if 'debug' in kwargs:
        kwargs['debug'] = maybebool(kwargs['debug'])

    if 'cache' in kwargs:
        cache = kwargs['cache'] = maybebool(kwargs['cache'])

        if cache and isinstance(cache,
                                six.string_types) and not path.isdir(cache):
            makedirs(cache)

    # 'updater' is just passed in...

    if 'auto_build' in kwargs:
        kwargs['auto_build'] = maybebool(kwargs['auto_build'])

    if 'jst_compiler' in kwargs:
        kwargs['JST_COMPILER'] = kwargs.pop('jst_compiler')

    if 'jst_namespace' in kwargs:
        kwargs['JST_NAMESPACE'] = kwargs.pop('jst_namespace')

    if 'manifest' in kwargs:
        kwargs['manifest'] = maybebool(kwargs['manifest'])

    if 'url_expire' in kwargs:
        kwargs['url_expire'] = maybebool(kwargs['url_expire'])

    if 'static_view' in kwargs:
        kwargs['static_view'] = asbool(kwargs['static_view'])
    else:
        kwargs['static_view'] = False

    if 'cache_max_age' in kwargs:
        kwargs['cache_max_age'] = int(kwargs.pop('cache_max_age'))
    else:
        kwargs['cache_max_age'] = None

    if 'load_path' in kwargs:
        # force load_path to be an array and split on whitespace
        if not isinstance(kwargs['load_path'], list):
            kwargs['load_path'] = kwargs['load_path'].split()

    assets_env = Environment(asset_dir, asset_url, **kwargs)

    return assets_env
Esempio n. 15
0
def get_webassets_env_from_settings(settings, prefix='webassets'):
    """This function will take all webassets.* parameters, and
    call the ``Environment()`` constructor with kwargs passed in.

    The only two parameters that are not passed as keywords are:

    * base_dir
    * base_url

    which are passed in positionally.

    Read the ``WebAssets`` docs for ``Environment`` for more details.
    """
    # Make a dictionary of the webassets.* elements...
    kwargs = {}   # assets settings
    cut_prefix = len(prefix) + 1
    for k in settings:
        if k.startswith(prefix):
            kwargs[k[cut_prefix:]] = settings[k]

    if 'base_dir' not in kwargs:
        raise Exception("You need to provide webassets.base_dir in your configuration")
    if 'base_url' not in kwargs:
        raise Exception("You need to provide webassets.base_url in your configuration")

    asset_dir = kwargs.pop('base_dir')
    asset_url = kwargs.pop('base_url')

    load_path = [x.strip() for x in kwargs.pop('load_path').splitlines()] if 'load_path' in kwargs else None

    if 'debug' in kwargs:
        dbg = kwargs['debug'].lower()

        if dbg == 'false' or dbg == 'true':
            dbg = asbool(dbg)

        kwargs['debug'] = dbg

    if 'cache' in kwargs:
        cache = kwargs['cache'].lower()

        if cache == 'false' or cache == 'true':
            kwargs['cache'] = asbool(kwargs['cache'])

    # 'updater' is just passed in...

    if 'jst_compiler' in kwargs:
        kwargs['JST_COMPILER'] = kwargs.pop('jst_compiler')

    if 'manifest' in kwargs:
        manifest = kwargs['manifest'].lower()

        if manifest == 'false' or manifest == 'none':
            kwargs['manifest'] = asbool(kwargs['manifest'])

    assets_env = Environment(asset_dir, asset_url, **kwargs)

    if load_path:
        for path in load_path:
            assets_env.append_path(path)

    return assets_env
Esempio n. 16
0
configuration_resources.set_working_directory()
configuration_resources.configure_logger("debug.log")

config = configuration_resources.get_config()

app = Flask(__name__, static_url_path='')

app.jinja_loader = jinja2.FileSystemLoader([
    os.path.abspath(os.path.join(app.root_path, "templates")),
    os.path.abspath(os.path.abspath("static-templates")),
])

push = configuration_resources.get_pushbullet()
redis = StrictRedis()

assets = Environment(os.path.abspath(os.path.join("static", "assets")),
                     "assets/")
assets.append_path(app.static_folder, "/")
assets.auto_build = False
assets.url_expire = True
assets.cache = False
assets.manifest = "file:{}".format(
    os.path.abspath(os.path.join("static", ".webassets-manifest")))

# Create assets
assets.register('bootstrap-css',
                'css/bootstrap.css',
                filters='cssmin',
                output='bootstrap.css')

assets.register('sidebar-css',
                'css/shared-sidebar.css',