def setupAssetEnviromentFromYAML(app, yamlFile): appDir = './' + app.config['APP_DIRECTORY'] + '/' assetEnv = AssetsEnvironment(appDir, '/') bundles = YAMLLoader(appDir + yamlFile).load_bundles() for bundle in bundles: assetEnv.register(bundle, bundles[bundle]) app.templateEnv.assets_environment = assetEnv
def _init_webassets(debug=False): assets_env = Environment(directory=SITE_ASSET_DIR, url=SITE_ASSET_URL_PREFIX, cache=WEBASSETS_CACHE_DIR, load_path=[SITE_ASSET_SRC_DIR]) assets_env.debug = debug js = Bundle('js/*.js', filters='uglifyjs', output='js/app.js') css = Bundle('sass/*.scss', filters='scss,cssmin', output='css/app.css') assets_env.register('app_js', js) assets_env.register('app_css', css) cmd = CommandLineEnvironment(assets_env, log) p = Process(target=lambda: cmd.watch()) def signal_handler(sig, frame): try: p.terminate() except Exception: pass sys.exit(0) signal.signal(signal.SIGINT, signal_handler) p.start() return assets_env
def compileJavascript(): environment = Environment('vendor/bootstrap/js/', 'javascript') output = '../../../codemitts/resources/static/javascript/bootstrap.js' bootstrap_javascript_bundle = Bundle('*', output=output) environment.register('bootstrap_javascript_bundle', bootstrap_javascript_bundle) environment['bootstrap_javascript_bundle'].urls()
def setup(self): # Setup the assets environment. assets_env = AssetsEnvironment('', '') self.foo_bundle = Bundle() self.bar_bundle = Bundle() assets_env.register('foo_bundle', self.foo_bundle) assets_env.register('bar_bundle', self.bar_bundle) # Inject a mock bundle class into the Jinja2 extension, so we # can check on what exactly it does. test_instance = self class MockBundle(Bundle): urls_to_fake = ['foo'] def __init__(self, *a, **kw): Bundle.__init__(self, *a, **kw) self.env = assets_env # Kind of hacky, but gives us access to the last Bundle # instance used by the extension. test_instance.the_bundle = self def urls(self, *a, **kw): return self.urls_to_fake self._old_bundle_class = AssetsExtension.BundleClass AssetsExtension.BundleClass = self.BundleClass = MockBundle # Setup the Jinja2 environment. self.jinja_env = JinjaEnvironment() self.jinja_env.add_extension(AssetsExtension) self.jinja_env.assets_environment = assets_env
def make_webassets_env_from_config(self, options, config): settings = {} settings.update(options) settings.update( coerce_config( config, 'webassets.', { 'debug': asbool, 'auto_build': asbool, 'manifest': self.string_or_bool, 'url_expire': asbool, 'cache': self.string_or_bool, 'load_path': aslist })) static_files_path = config['paths']['static_files'] asset_dir = settings.pop('base_dir', static_files_path) asset_url = settings.pop('base_url', '/') if not asset_url.startswith('/'): if urlparse(asset_url).scheme == '': asset_url = '/' + asset_url assets_env = Environment(asset_dir, asset_url, **settings) bundles = self.options.get('bundles', {}) for name, value in bundles.items(): if isinstance(value, Bundle): assets_env.register(name, value) else: # If it's not a bundle consider it a loader assets_env.register(value.load_bundles()) return assets_env
def generate_webasset_bundles(config): compo_bundles = extract_static_assets_from_components(epflutil.Discover.discovered_components) page_bundles = [] pages = [epflpage.Page] + epflutil.Discover.discovered_pages for cls in pages: page_bundles.append(extract_static_assets_from_components([cls])) if not asbool(config.registry.settings.get('epfl.webassets.active', False)): return ar = AssetResolver() epfl_static = ar.resolve('solute.epfl:static') my_env = Environment('%s/bundles' % epfl_static.abspath(), 'bundles') for i, page in enumerate(pages): js_paths, js_name, css_paths, css_name = page_bundles[i] js_paths += compo_bundles[0] js_name += compo_bundles[1] css_paths += compo_bundles[2] css_name += compo_bundles[3] my_env.register('js%s' % i, Bundle(js_paths, filters='rjsmin', output='epfl.%(version)s.js')) my_env.register('css%s' % i, Bundle(css_paths, output='epfl.%(version)s.css')) page.js_name += [("solute.epfl:static", url) for url in my_env['js%s' % i].urls()] page.css_name += [("solute.epfl:static", url) for url in my_env['css%s' % i].urls()] page.bundled_names = js_name + css_name
def setup(self): # Setup the assets environment. assets_env = AssetsEnvironment('', '') self.foo_bundle = Bundle() self.bar_bundle = Bundle() assets_env.register('foo_bundle', self.foo_bundle) assets_env.register('bar_bundle', self.bar_bundle) # Inject a mock bundle class into the Jinja2 extension, so we # can check on what exactly it does. test_instance = self class MockBundle(Bundle): urls_to_fake = ['foo'] def __init__(self, *a, **kw): Bundle.__init__(self, *a, **kw) self.env = assets_env # Kind of hacky, but gives us access to the last Bundle # instance used by the extension. test_instance.the_bundle = self def urls(self, *a, **kw): return self.urls_to_fake self._old_bundle_class = AssetsExtension.BundleClass AssetsExtension.BundleClass = self.BundleClass = MockBundle # Setup the Jinja2 environment. self.jinja_env = JinjaEnvironment() self.jinja_env.add_extension(AssetsExtension) self.jinja_env.assets_environment = assets_env
def get_assets_bottom_js_env(self, code_path, output_path): """ The directory structure of assets is: output_path static css js """ output_path = os.path.join(output_path, 'static') assets_env = Environment(output_path, '/static', debug=self.args.debug) assets_env.config['compass_config'] = { 'additional_import_paths': [os.path.join(code_path, 'scss-mixins')], #'sass_options': "cache: False", ??? i can't get it. 'http_path': "/static", } bottom_js_list = [] if "bottom_js" in self.config.data: bottom_js_list = self.config.data["bottom_js"] if bottom_js_list and len(bottom_js_list): bottom_js = Bundle(*bottom_js_list, filters='rjsmin', output='bottom.js') assets_env.register('bottom_js', bottom_js) return assets_env
def compile_pages_and_assets(dev=False): compiledSass = '' sass_files_to_compile = ['main.scss'] for sassFile in sass_files_to_compile: # with open('resources/css/scss/'+sassFile, 'r') as thisfile: # SassData=thisfile.read() compiledSass += sass.compile(filename='resources/css/scss/'+sassFile, include_paths='resources/css/scss/vendor/') with open('resources/css/sass.css', 'w') as outputFile: outputFile.write(compiledSass) if(dev): all_js = Bundle('**/*.js', filters='noop', output='packed.js') all_css = Bundle('**/*.css', filters='noop', output="main.css") else: all_js = Bundle('**/*.js', filters='jsmin', output='packed.js') all_css = Bundle('**/*.css', filters='cssmin', output="main.css") jinja_env = Jinja2Environment(extensions=[AssetsExtension]) jinja_env.loader = FileSystemLoader('.') assets_env = AssetsEnvironment(url='/assets', directory='resources') assets_env.register('all_js', all_js) assets_env.register('all_css', all_css) jinja_env.assets_environment = assets_env pages = [] pages_dir = 'pages' for path, subdirs, files in os.walk(pages_dir): for name in files: pages.append(os.path.join(path, name)[6:]) # print(pages) for page in pages: thisTemplate = jinja_env.get_template('pages/' + page) thisTempRendered = thisTemplate.render() file_name = 'output/' + page body_content_location = 'output/content/' + page pathlib.Path(os.path.dirname(file_name)).mkdir(parents=True, exist_ok=True) pathlib.Path(os.path.dirname(body_content_location)).mkdir(parents=True, exist_ok=True) with open(file_name, 'w') as tempFile: tempFile.write(thisTempRendered) # This bit is used for my ajax shenanigans # anything you want on a page needs to go on body though... result = re.search('<body>(.*)<\/body>', '"' + thisTempRendered.replace('"', '\"').replace('\n',' ') + '"') # print(result) onlyTheBodyPart = result.group(1) with open(body_content_location, 'w') as tempFile: tempFile.write(onlyTheBodyPart) src = 'resources' dst = 'output/assets' filelist = [] files = ['main.css', 'packed.js'] for filename in files: filelist.append(filename) fullpath = src + '/' + filename shutil.move(os.path.join(src, filename), os.path.join(dst, filename))
def get_assets_css_env(self, code_path, output_path): """ The directory structure of assets is: output_path static css js """ output_path = os.path.join(output_path, 'static') assets_env = Environment(output_path, '/static', debug=self.args.debug) assets_env.config['compass_config'] = { 'additional_import_paths': [os.path.join(code_path, 'scss-mixins')], #'sass_options': "cache: False", ??? i can't get it. 'http_path': "/static", } css_list = [] scss_list = [] if "css_or_scss" in self.config.data: for filename in self.config.data["css_or_scss"]: if self.args.verbose: print('Adding filename: %s' % (filename)) ext = os.path.splitext(filename)[1] if ext == '.scss': scss_list.append(filename) elif ext == '.css': css_list.append(filename) else: raise Exception( 'Bad extension: is %s instead of css/scss' % ext) if len(css_list) or len(scss_list): css_bundle = Bundle(*css_list) scss_bundle = [] for scss_file in scss_list: if self.args.verbose: print('Processing filename: %s' % (scss_file)) try: xxx = Bundle( scss_file, filters='compass', output=scss_file + '.css', ) scss_bundle.append(xxx) except Exception as error: print("ERROR processing filename '%s': %s" % (scss_file, error)) css = Bundle(css_bundle, *scss_bundle, filters='cssutils,cssrewrite', output='packed.css') assets_env.register('css', css) return assets_env
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 {} # construct the environment if not 'url' in obj or not 'directory' in obj: raise LoaderError('"url" and "directory" must be defined') directory = obj['directory'] if filename: # If we know the location of the file, make sure that the # paths included are considered relative to the file location. directory = path.normpath(path.join(path.dirname(filename), directory)) env = Environment(directory, obj['url']) # load environment settings for setting in ('debug', 'cache', 'versions', 'url_expire', 'auto_build', # TODO: The deprecated values; remove at some point 'expire', 'updater'): if setting in obj: setattr(env, setting, obj[setting]) # 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()
def includeme(config): """pyramid include. declare the add_thumb_view""" here = os.path.dirname(__file__) settings = config.registry.settings config_dir = settings.get('garasu_webassets.config', '{}/configs'.format(here)) LOG.debug(config_dir) # config_dir = AssetResolver(None).resolve(config_dir).abspath() asset_dir = settings.get('garasu_webassets.assets', '{}/assets'.format(here)) LOG.debug(asset_dir) # asset_dir = AssetResolver(None).resolve(asset_dir).abspath() env = Environment(directory=asset_dir, url=settings['garasu_webassets.url']) env.manifest = settings['garasu_webassets.manifest'] env.debug = asbool(settings['garasu_webassets.debug']) env.cache = asbool(settings['garasu_webassets.cache']) env.auto_build = asbool(settings['garasu_webassets.auto_build']) def text(value): if type(value) is six.binary_type: return value.decode('utf-8') else: return value def yaml_stream(fname, mode): if path.exists(fname): return open(fname, mode) raise FileNotFoundError fin = fileinput.input('/'.join([config_dir, settings['garasu_webassets.bundles']]), openhook=yaml_stream) with closing(fin): lines = [text(line).rstrip() for line in fin] stream_yaml = six.StringIO('\n'.join(lines)) loader = YAMLLoader(stream_yaml) result = loader.load_bundles() env.register(result) # for item in env: # LOG.debug(item.output) # path_file = '/'.join([public_dir, item.output]) # src_file = '/'.join([asset_dir, item.output]) # shutil.copy(src_file, path_file) def _get_assets(request, *args, **kwargs): bundle = Bundle(*args, **kwargs) with bundle.bind(env): urls = bundle.urls() return urls config.add_request_method(_get_assets, 'web_assets') def _get_assets_env(request): return env config.add_request_method(_get_assets_env, 'web_env', reify=True)
def test_jinja2_config(self): app_config = self.basic_valid_app_config.copy() dict_loader = DictLoader({}) app_config.update({"jinja2": {"loader": dict_loader, "use_webassets": True}}) webassets_env = Environment("/tmp", "/") webassets_env.register("js", "dummy.js", "dummy2.js", output="dummy.js") config = BlueberryPyConfiguration(app_config=app_config, webassets_env=webassets_env) self.assertEqual(config.jinja2_config, {"loader": dict_loader})
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()
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()
class WPConfModifChat(WPConferenceModifBase): def __init__(self, rh, conf): WPConferenceModifBase.__init__(self, rh, conf) self._conf = conf self._tabs = {} # list of Indico's Tab objects self._tabNames = rh._tabs self._activeTabName = rh._activeTabName self._aw = rh.getAW() self._tabCtrl = wcomponents.TabControl() plugin_htdocs = os.path.join(os.path.dirname(__file__), 'htdocs') self._plugin_asset_env = Environment(plugin_htdocs, '/InstantMessaging') self._plugin_asset_env.register('instant_messaging', Bundle('js/InstantMessaging.js', filters='jsmin', output="InstantMessaging_%(version)s.min.js")) def getJSFiles(self): return WPConferenceModifBase.getJSFiles(self) + \ self._plugin_asset_env['instant_messaging'].urls() def getCSSFiles(self): return WPConferenceModifBase.getCSSFiles(self) + \ ['InstantMessaging/im.css'] def _createTabCtrl(self): for tabName in self._tabNames: url = urlHandlers.UHConfModifChat.getURL(self._conf, tab = tabName) self._tabs[tabName] = self._tabCtrl.newTab(tabName, tabName, url) self._setActiveTab() def _setActiveSideMenuItem(self): if self._pluginsDictMenuItem.has_key('Instant Messaging'): self._pluginsDictMenuItem['Instant Messaging'].setActive(True) def _setActiveTab(self): self._tabs[self._activeTabName].setActive() def _getTitle(self): return WPConferenceModifBase._getTitle(self) + " - " + _("Chat management") def _getPageContent(self, params): if len(self._tabNames) > 0: self._createTabCtrl() wc = WConfModifChat.forModule(InstantMessaging, self._conf, self._activeTabName, self._tabNames, self._aw) return wcomponents.WTabControl(self._tabCtrl, self._getAW()).getHTML(wc.getHTML({})) else: return _("No available plugins, or no active plugins")
def create_and_register_bundles(section, filter, DEPLOY): environment = Environment('.') for option in config.options(section): deps = [v.strip() for v in config.get(section, option).split(',')] log.info("%s.%s has dependencies: %s", option, section, deps) bndl = Bundle(*deps, filters=filter, output=concat_string([DEPLOY, '/', option, '.', section])) environment.register(option, bndl) bndlgz = Bundle(*deps, filters=concat_string([filter, ', gzip']), output=concat_string([DEPLOY, '/', option, '.', section, '.gz'])) environment.register(concat_string([option, 'gz']), bndlgz) bndl.build() bndlgz.build()
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 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 {} # construct the environment if not 'url' in obj or not 'directory' in obj: raise LoaderError('"url" and "directory" must be defined') directory = obj['directory'] if filename: # If we know the location of the file, make sure that the # paths included are considered relative to the file location. directory = path.normpath( path.join(path.dirname(filename), directory)) env = Environment(directory, obj['url']) # load environment settings for setting in ( 'debug', 'cache', 'updater', 'expire', ): if setting in obj: setattr(env, setting, obj[setting]) # load bundles bundles = self._get_bundles(obj.get('bundles', {})) for name, bundle in bundles.iteritems(): env.register(name, bundle) return env finally: f.close()
def get_assets(options): ''' A helper to build the assets for the service :param options: The options to initialize with :returns: The initialized assets ''' arguments = { 'directory': './static', 'url': options.asset_url, 'debug': options.debug, 'updater': 'timestamp', 'expire': 'querystring', 'cache': True, } environment = Environment(**arguments) # ------------------------------------------------------------ # stylesheet bundle # ------------------------------------------------------------ stylesheet = Bundle('css/*.css', filters='cssutils', output='cache/speleo.css') environment.register('stylesheet', stylesheet) # ------------------------------------------------------------ # template bundle # ------------------------------------------------------------ template = Bundle('tmpl/*.tmpl', filters='jst', output='cache/speleo.jst.js', debug=False) environment.config['jst_compiler'] = '_.template' # ------------------------------------------------------------ # javascript bundle # ------------------------------------------------------------ javascript = Bundle( Bundle('js/lib/core/jquery.js', 'js/lib/core/underscore.js'), template, # needs underscore Bundle('js/lib/ace/ace.js', 'js/lib/ace/mode-javascript.js'), Bundle('js/lib/*.js', 'js/speleo.js'), filters='closure_js', output='cache/speleo.js') environment.register('javascript', javascript) # ------------------------------------------------------------ # output cache # ------------------------------------------------------------ if not os.path.exists(os.path.join('./static', 'cache')): os.mkdir(os.path.join('./static', 'cache')) # ------------------------------------------------------------ # initialized # ------------------------------------------------------------ return Assets(environment)
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
def main(): log = logging.getLogger('webassets') log.addHandler(logging.StreamHandler()) log.setLevel(logging.DEBUG) call(["coffee", "-c", "src/tambur.coffee"]) call(["coffee", "-c", "src/tambur_publisher.coffee"]) env = Environment('.', '/static') tamburjs = Bundle( 'deps/sockjs-0.3.js', 'src/tambur.js', output='out/tambur.js') tamburminjs = Bundle( 'deps/sockjs-0.3.js', 'src/tambur.js', filters='yui_js', output='out/tambur.min.js') publishjs = Bundle( 'deps/sha1.js', 'deps/oauth.js', 'src/tambur_publisher.js', output='out/tambur_pub.js') publishminjs = Bundle( 'deps/sha1.js', 'deps/oauth.js', 'src/tambur_publisher.js', filters='yui_js', output='out/tambur_pub.min.js') env.register('tambur.js', tamburjs) env.register('tambur.min.js', tamburminjs) env.register('tambur_pub.js', publishjs) env.register('tambur_pub.min.js', publishminjs) cmdenv = CommandLineEnvironment(env, log) cmdenv.build()
def run(self): from webassets import Bundle from webassets import Environment from webassets.script import CommandLineEnvironment css = Bundle('curious/src/css/app.css', output='curious/dist/curious.css') js = Bundle('curious/src/js/*.js', output='curious/dist/curious.js') jsm = Bundle('curious/src/js/*.js', filters='jsmin', output='curious/dist/curious.min.js') jst = Bundle('curious/src/html/*.html', filters='jst', output='curious/dist/curious_jst.js') assets_env = Environment('./curious/static') assets_env.cache = self.cache_dir assets_env.register('css', css) assets_env.register('js', js) assets_env.register('jsmin', jsm) assets_env.register('jst', jst) log = logging.getLogger('webassets') log.addHandler(logging.StreamHandler()) log.setLevel(logging.DEBUG) cmdenv = CommandLineEnvironment(assets_env, log) cmdenv.build()
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
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()
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
class AppHandler(BaseHandler): def initialize(self, static_path, **kw): super(AppHandler, self).initialize(**kw) self.static_path = static_path def get_template_namespace(self): _ = super(AppHandler, self).get_template_namespace() self.assets = Environment(self.static_path, '/static') css_all = Bundle('css/bootstrap.min.css', 'css/material.min.css', Bundle('css/custom.css', 'css/dropdown.css', filters='cssmin'), 'outdatedbrowser/outdatedbrowser.min.css', output='dict/plugin.min.css') js_all = Bundle( Bundle('outdatedbrowser/outdatedbrowser.min.js', 'js/react-0.13.2/react-with-addons.min.js', 'js/jquery-2.1.3.min.js', 'js/bootstrap-3.3.4.min.js', 'js/react-bootstrap.min.js', 'js/react-mini-router.min.js', 'js/marked.min.js', 'js/material.min.js', 'js/isMobile.min.js', 'js/moment-with-locales.min.js', 'js/dropdown.js', filters='jsmin'), Bundle( 'jsx/init.jsx', # 'jsx/mixin/*.jsx', 'jsx/lib/*.jsx', 'jsx/page/*.jsx', filters=('react', 'jsmin')), output='dict/plugin.min.js') self.assets.register('css_all', css_all) self.assets.register('js_all', js_all) _['css_urls'] = self.assets['css_all'].urls() _['js_urls'] = self.assets['js_all'].urls() return _ def get(self): self.render('app.html')
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 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 {} # construct the environment if not "url" in obj or not "directory" in obj: raise LoaderError('"url" and "directory" must be defined') directory = obj["directory"] if filename: # If we know the location of the file, make sure that the # paths included are considered relative to the file location. directory = path.normpath(path.join(path.dirname(filename), directory)) env = Environment(directory, obj["url"]) # load environment settings for setting in ("debug", "cache", "updater", "expire"): if setting in obj: setattr(env, setting, obj[setting]) # load bundles bundles = self._get_bundles(obj.get("bundles", {})) for name, bundle in bundles.iteritems(): env.register(name, bundle) return env finally: f.close()
class AppHandler(BaseHandler): def initialize(self, static_path, **kw): super(AppHandler, self).initialize(**kw) self.static_path = static_path def get_template_namespace(self): _ = super(AppHandler, self).get_template_namespace() self.assets = Environment(self.static_path, '/static') css_all = Bundle( 'css/bootstrap.min.css', 'css/material.min.css', Bundle('css/custom.css','css/dropdown.css', filters='cssmin'), 'outdatedbrowser/outdatedbrowser.min.css', output='dict/plugin.min.css') js_all = Bundle( Bundle( 'outdatedbrowser/outdatedbrowser.min.js', 'js/react-0.13.2/react-with-addons.min.js', 'js/jquery-2.1.3.min.js', 'js/bootstrap-3.3.4.min.js', 'js/react-bootstrap.min.js', 'js/react-mini-router.min.js', 'js/marked.min.js', 'js/material.min.js', 'js/isMobile.min.js', 'js/moment-with-locales.min.js', 'js/dropdown.js',filters='jsmin'), Bundle( 'jsx/init.jsx', # 'jsx/mixin/*.jsx', 'jsx/lib/*.jsx', 'jsx/page/*.jsx', filters=('react','jsmin')), output='dict/plugin.min.js') self.assets.register('css_all', css_all) self.assets.register('js_all', js_all) _['css_urls'] = self.assets['css_all'].urls() _['js_urls'] = self.assets['js_all'].urls() return _ def get(self): self.render('app.html')
def get_webassets_env(conf): '''Get a preconfigured WebAssets environment''' # Configure webassets assets_environment = AssetsEnvironment(conf.get('static_files_dir', DEFAULT_STATIC), '/') assets_environment.debug = conf.get('debug', False) assets_environment.auto_build = conf.get('debug', False) assets_environment.config['less_paths'] = ( 'bower/bootstrap/less', 'bower/etalab-assets/less', 'bower/bootstrap-markdown/less', ) # Load bundle from yaml file loader = YAMLLoader(resource_stream(__name__, '../assets.yaml')) bundles = loader.load_bundles() for name, bundle in bundles.items(): assets_environment.register(name, bundle) return assets_environment
class WPLiveSyncAdmin(WPAdminPlugins): def __init__(self, rh, templateClass): WPAdminPlugins.__init__(self, rh, "livesync", "") self._templateClass = templateClass self._plugin_asset_env = Environment(RHLiveSyncHtdocs._local_path, "/livesync") self._plugin_asset_env.register( "livesync", Bundle("js/livesync.js", filters="jsmin", output="InstantMessaging__%(version)s.min.js") ) def getJSFiles(self): return WPAdminPlugins.getJSFiles(self) + self._plugin_asset_env["livesync"].urls() def getCSSFiles(self): return WPAdminPlugins.getCSSFiles(self) + ["livesync/livesync.css"] def _getPageContent(self, params): return wcomponents.WTabControl(self._tabCtrl, self._getAW()).getHTML( self._templateClass.forModule(indico.ext.livesync).getHTML(params) )
def test_use_webassets(self): app_config = self.basic_valid_app_config.copy() app_config.update({"jinja2": {"use_webassets": True, "loader": DictLoader({})}}) self.assertRaisesRegexp(BlueberryPyNotConfiguredError, "Webassets configuration not found.", callable_obj=BlueberryPyConfiguration, app_config=app_config) webassets_env = Environment("/tmp", "/") self.assertRaisesRegexp(BlueberryPyNotConfiguredError, "No bundles found in webassets env.", callable_obj=BlueberryPyConfiguration, app_config=app_config, webassets_env=webassets_env) webassets_env = Environment("/tmp", "/") webassets_env.register("js", "dummy.js", "dummy2.js", output="dummy.js") config = BlueberryPyConfiguration(app_config=app_config, webassets_env=webassets_env) self.assertTrue(config.use_webassets)
def _init_webassets(debug=False, generate=False): assets_env = Environment(directory=SITE_ASSET_DIR, url=SITE_ASSET_URL_PREFIX, cache=WEBASSETS_CACHE_DIR, load_path=[SITE_ASSET_SRC_DIR]) assets_env.debug = debug js = Bundle('js/*.js', filters='uglifyjs', output='js/app.js') css = Bundle('sass/*.scss', filters='scss,cssmin', output='css/app.css') assets_env.register('app_js', js) assets_env.register('app_css', css) cmd = CommandLineEnvironment(assets_env, log) if generate: cmd.build() return assets_env Process(target=lambda: cmd.watch()).start() return assets_env
def generate_webasset_bundles(config): compo_bundles = extract_static_assets_from_components( epflutil.Discover.discovered_components) page_bundles = [] pages = [epflpage.Page] + epflutil.Discover.discovered_pages for cls in pages: page_bundles.append(extract_static_assets_from_components([cls])) if not asbool(config.registry.settings.get('epfl.webassets.active', False)): return ar = AssetResolver() epfl_static = ar.resolve('solute.epfl:static') my_env = Environment('%s/bundles' % epfl_static.abspath(), 'bundles') for i, page in enumerate(pages): js_paths, js_name, css_paths, css_name = page_bundles[i] js_paths += compo_bundles[0] js_name += compo_bundles[1] css_paths += compo_bundles[2] css_name += compo_bundles[3] my_env.register( 'js%s' % i, Bundle(js_paths, filters='rjsmin', output='epfl.%(version)s.js')) my_env.register('css%s' % i, Bundle(css_paths, output='epfl.%(version)s.css')) page.js_name += [("solute.epfl:static", url) for url in my_env['js%s' % i].urls()] page.css_name += [("solute.epfl:static", url) for url in my_env['css%s' % i].urls()] page.bundled_names = js_name + css_name
def _init_webassets(debug=False, generate=False): assets_env = Environment(directory=SITE_ASSET_DIR, url=SITE_ASSET_URL_PREFIX, cache=WEBASSETS_CACHE_DIR, load_path=[SITE_ASSET_SRC_DIR]) assets_env.debug = debug js = Bundle('js/*.js', filters='uglifyjs', output='js/app.js') css = Bundle('sass/*.scss', filters='scss,cssmin', output='css/app.css') assets_env.register('app_js', js) assets_env.register('app_css', css) cmd = CommandLineEnvironment(assets_env, log) if generate: cmd.build() return assets_env Process(target=lambda: cmd.watch()).start() return assets_env
def get_environment(self): """ Returns the webassets environment, registering all the bundles. """ debug = os.environ.get('MORE_WEBASSETS_DEBUG', '').lower().strip() in ( 'true', '1' ) env = Environment( directory=self.output_path, load_path=self.paths, url=self.url, debug=debug ) for asset in self.assets: bundles = tuple(self.get_bundles(asset)) js = tuple(b for b in bundles if b.output.endswith('.js')) css = tuple(b for b in bundles if b.output.endswith('.css')) if js: js_bundle = len(js) == 1 and js[0] or Bundle( *js, output='{}.bundle.js'.format(asset) ) else: js_bundle = None if css: css_bundle = len(css) == 1 and css[0] or Bundle( *css, output='{}.bundle.css'.format(asset) ) else: css_bundle = None if js_bundle and css_bundle: js_bundle.next_bundle = asset + '_1' env.register(asset, js_bundle) env.register(asset + '_1', css_bundle) elif js_bundle: env.register(asset, js_bundle) else: env.register(asset, css_bundle) return env
def get_environment(self): """ Returns the webassets environment, registering all the bundles. """ debug = os.environ.get('MORE_WEBASSETS_DEBUG', '').lower().strip() in ( 'true', '1' ) env = Environment( directory=self.output_path, load_path=self.paths, url=self.url, debug=debug ) for asset in self.assets: bundles = tuple(self.get_bundles(asset)) js = tuple(b for b in bundles if b.output.endswith('.js')) css = tuple(b for b in bundles if b.output.endswith('.css')) if js: js_bundle = len(js) == 1 and js[0] or Bundle( *js, output='{}.bundle.js'.format(asset) ) else: js_bundle = None if css: css_bundle = len(css) == 1 and css[0] or Bundle( *css, output='{}.bundle.css'.format(asset) ) else: css_bundle = None if js_bundle and css_bundle: js_bundle.next_bundle = asset + '_1' env.register(asset, js_bundle) env.register(asset + '_1', css_bundle) elif js_bundle: env.register(asset, js_bundle) else: env.register(asset, css_bundle) return env
def test_register_composed_bundle(self): """Test registering a bundle with `merge=False` """ env = Environment('tests') env.register( 'base1', 'helpers.py', 'test_ext/__init__.py', merge=False, output='build/%(name)s.%(version)s.css' ) assert 'base1' not in env assert 'base1/helpers.py' in env assert 'base1/__init__.py' in env env.register( '', 'helpers.py', 'test_ext/__init__.py', merge=False, output='build/%(name)s.css' ) assert 'helpers.py' in env assert '__init__.py' in env assert env['helpers.py'].output == 'build/helpers.css' assert env['__init__.py'].output == 'build/__init__.css' env.register( 'base2', 'helper*.*', merge=False, output='build/%(name)s.%(ext)s' ) assert 'base2/helpers.py' in env assert env['base2/helpers.py'].output == 'build/helpers.py' env.register( 'base3', 'test_ext/__init__.py', merge=False, output='build/%(path)s.css' ) assert env['base3/__init__.py'].output == 'build/test_ext/__init__.css'
def run(self): from webassets import Bundle from webassets import Environment from webassets.script import CommandLineEnvironment css = Bundle('curious/src/css/app.css', output='curious/dist/curious.css') js = Bundle('curious/src/js/*.js', output='curious/dist/curious.js') jsmin = Bundle('curious/src/js/*.js', filters='jsmin', output='curious/dist/curious.min.js') jst = Bundle('curious/src/html/*.html', filters='jst', output='curious/dist/curious_jst.js') assets_env = Environment('./curious/static') assets_env.cache = self.cache_dir assets_env.register('css', css) assets_env.register('js', js) assets_env.register('jsmin', jsmin) assets_env.register('jst', jst) log = logging.getLogger('webassets') log.addHandler(logging.StreamHandler()) log.setLevel(logging.DEBUG) cmdenv = CommandLineEnvironment(assets_env, log) cmdenv.build()
def test_register_composed_bundle(self): """Test registering a bundle with `merge=False` """ env = Environment('tests') env.register('base1', 'helpers.py', 'test_ext/__init__.py', merge=False, output='build/%(name)s.%(version)s.css') assert 'base1' not in env assert 'base1/helpers.py' in env assert 'base1/__init__.py' in env env.register('', 'helpers.py', 'test_ext/__init__.py', merge=False, output='build/%(name)s.css') assert 'helpers.py' in env assert '__init__.py' in env assert env['helpers.py'].output == 'build/helpers.css' assert env['__init__.py'].output == 'build/__init__.css' env.register('base2', 'helper*.*', merge=False, output='build/%(name)s.%(ext)s') assert 'base2/helpers.py' in env assert env['base2/helpers.py'].output == 'build/helpers.py' env.register('base3', 'test_ext/__init__.py', merge=False, output='build/%(path)s.css') assert env['base3/__init__.py'].output == 'build/test_ext/__init__.css'
def main(): my_env = Environment(directory='static', url='/static') # # The js for every pge all_js = Bundle( Bundle(*vendor_js), Bundle('coffee/common.coffee', filters='coffeescript'), # filters='jsmin', output='all.js') my_env.register('all_js', all_js) # # Per-page coffee page_bundles = [] for file in os.listdir(os.path.join(os.path.abspath('.'), 'static/coffee')): if file.endswith('.coffee') and not file.startswith( '#') and file != 'common.coffee': bundle_name = file.split('.')[0] bundle = Bundle(os.path.join('coffee', file), filters='coffeescript', output='%s.js' % bundle_name) my_env.register(bundle_name, bundle) page_bundles.append(bundle_name) # # CSS for every page all_css = Bundle( Bundle(*vendor_css # filters='cssmin' ), output='all.css') my_env.register('all_css', all_css) for js_url in my_env['all_js'].urls(): print(js_url) for css_url in my_env['all_css'].urls(): print(css_url) for page_bundle in page_bundles: for url in my_env[page_bundle].urls(): print(url)
def main(): my_env = Environment( directory='static', url='/static') # # The js for every pge all_js = Bundle( Bundle(*vendor_js), Bundle('coffee/common.coffee', filters='coffeescript'), # filters='jsmin', output='all.js' ) my_env.register('all_js', all_js) # # Per-page coffee page_bundles = [] for file in os.listdir(os.path.join(os.path.abspath('.'), 'static/coffee')): if file.endswith('.coffee') and not file.startswith('#') and file != 'common.coffee': bundle_name = file.split('.')[0] bundle = Bundle(os.path.join('coffee', file), filters='coffeescript', output='%s.js' % bundle_name) my_env.register(bundle_name, bundle) page_bundles.append(bundle_name) # # CSS for every page all_css = Bundle( Bundle(*vendor_css # filters='cssmin' ), output='all.css' ) my_env.register('all_css', all_css) for js_url in my_env['all_js'].urls(): print(js_url) for css_url in my_env['all_css'].urls(): print(css_url) for page_bundle in page_bundles: for url in my_env[page_bundle].urls(): print(url)
from webassets import Environment, Bundle environment = Environment('static', '') environment.debug = True css_map_bundle = Bundle( 'css/style.css', 'map/leaflet/leaflet.css', 'map/leaflet-sidebar/src/L.Control.Sidebar.css', 'map/leaflet-control-geocoder/Control.Geocoder.css', 'map/Osmose.Editor.css', 'map/style.css', 'map/Osmose.Menu.css', filters='cssrewrite,cssmin', output='gen/map-%(version)s.css') environment.register('css_map', css_map_bundle) js_map_bundle = Bundle( 'js/jquery-1.7.2.js', 'js/jquery-ui-1.10.4.dialog.js', 'js/mustache.js', 'map/leaflet/leaflet-src.js', 'map/leaflet-plugins/control/Permalink.js', 'map/leaflet-plugins/control/Permalink.Layer.js', 'map/Permalink.Overlay.js', 'map/Permalink.Item.js', 'map/leaflet-plugins/layer/tile/Bing.js', 'map/layers.js', 'map/leaflet.active-layers.min.js', 'map/leaflet.select-layers.min.js', 'map/leaflet-sidebar/src/L.Control.Sidebar.js',
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()
class TestEnvApi(object): """General Environment functionality.""" def setup(self): self.m = Environment(None, None) def test_single_bundle(self): """Test self.m.registering a single ``Bundle`` object. """ b = Bundle() self.m.register('foo', b) assert self.m['foo'] == b def test_new_bundle(self): """Test self.m.registering a new bundle on the fly. """ b = Bundle() self.m.register('foo', b, 's2', 's3') assert b in self.m['foo'].contents # Special case of using only a single, non-bundle source argument. self.m.register('footon', 's1') assert 's1' in self.m['footon'].contents # Special case of specifying only a single bundle as a source, but # additional options - this also creates a wrapping bundle. self.m.register('foofighters', b, output="bar") assert b in self.m['foofighters'].contents def test_invalid_call(self): """Test calling self.m.register with an invalid syntax. """ assert_raises(TypeError, self.m.register) assert_raises(TypeError, self.m.register, 'one-argument-only') def test_duplicate(self): """Test name clashes. """ b1 = Bundle() b2 = Bundle() self.m.register('foo', b1) # Multiple calls with the same name are ignored if the given bundle # is the same as originally passed. self.m.register('foo', b1) assert len(self.m) == 1 # Otherwise, an error is raised. assert_raises(RegisterError, self.m.register, 'foo', b2) assert_raises(RegisterError, self.m.register, 'foo', 's1', 's2', 's3') def test_anon_bundle(self): """Self registering an anonymous bundle. """ b = Bundle() self.m.add(b) assert len(self.m) == 1 assert list(self.m) == [b] def test_contains(self): """Test __contains__. """ b = Bundle() self.m.register('foo', b) assert 'foo' in self.m assert not 'bar' in self.m
from webassets import Environment, Bundle environment = Environment('static', '') environment.debug = True css_map_bundle = Bundle('css/style.css', 'map/leaflet/leaflet.css', 'map/leaflet-sidebar/src/L.Control.Sidebar.css', 'map/leaflet-control-geocoder/Control.Geocoder.css', 'map/Osmose.Editor.css', 'map/style.css', 'map/Osmose.Menu.css', filters='cssrewrite,cssmin', output='gen/map-%(version)s.css') environment.register('css_map', css_map_bundle) js_map_bundle = Bundle('js/jquery-1.7.2.js', 'js/jquery-ui-1.10.4.dialog.js', 'js/mustache.js', 'map/leaflet/leaflet-src.js', 'map/leaflet-plugins/control/Permalink.js', 'map/leaflet-plugins/control/Permalink.Layer.js', 'map/Permalink.Overlay.js', 'map/Permalink.Item.js', 'map/leaflet-plugins/layer/tile/Bing.js', 'map/Leaflet.VectorGrid.bundled.js', 'map/Mapillary.js', 'map/layers.js', 'map/leaflet.active-layers.min.js', 'map/leaflet-sidebar/src/L.Control.Sidebar.js',
from os import path, environ from falcon import HTTPNotFound from jinja2 import FileSystemLoader from jinja2.environment import Environment as Jinja2Environment from webassets import Environment as AssetsEnvironment, Bundle from webassets.ext.jinja2 import AssetsExtension from webassets.script import CommandLineEnvironment STATIC_ROOT = environ.get('STATIC_ROOT', path.abspath(path.dirname(__file__))) assets_env = AssetsEnvironment(path.join(STATIC_ROOT, 'static'), url='/static') assets_env.register( 'libs', Bundle('js/jquery-2.1.4.min.js', 'js/handlebars.min.js', 'js/bootstrap.min.js', 'js/moment.js', 'js/moment-timezone.js', 'js/moment-tz-data.js', 'js/typeahead.js', output='bundles/libs.js')) assets_env.register( 'oncall_js', Bundle('js/navigo.js', 'js/incalendar.js', 'js/oncall.js', output='bundles/oncall.bundle.js')) assets_env.register( 'css_libs', Bundle('css/bootstrap.min.css', 'fonts/Source-Sans-Pro.css', output='bundles/libs.css'))
class TestEnvApi: """General Environment functionality.""" def setup(self): self.m = Environment(None, None) def test_single_bundle(self): """Test self.m.registering a single ``Bundle`` object. """ b = Bundle() self.m.register('foo', b) assert self.m['foo'] == b def test_new_bundle(self): """Test self.m.registering a new bundle on the fly. """ b = Bundle() self.m.register('foo', b, 's2', 's3') assert b in self.m['foo'].contents # Special case of using only a single, non-bundle source argument. self.m.register('footon', 's1') assert 's1' in self.m['footon'].contents # Special case of specifying only a single bundle as a source, but # additional options - this also creates a wrapping bundle. self.m.register('foofighters', b, output="bar") assert b in self.m['foofighters'].contents def test_invalid_call(self): """Test calling self.m.register with an invalid syntax. """ assert_raises(TypeError, self.m.register) assert_raises(TypeError, self.m.register, 'one-argument-only') def test_duplicate(self): """Test name clashes. """ b1 = Bundle() b2 = Bundle() self.m.register('foo', b1) # Multiple calls with the same name are ignored if the given bundle # is the same as originally passed. self.m.register('foo', b1) assert len(self.m) == 1 # Otherwise, an error is raised. assert_raises(RegisterError, self.m.register, 'foo', b2) assert_raises(RegisterError, self.m.register, 'foo', 's1', 's2', 's3') def test_anon_bundle(self): """Self registering an anonymous bundle. """ b = Bundle() self.m.add(b) assert len(self.m) == 1 assert list(self.m) == [b] def test_contains(self): """Test __contains__. """ b = Bundle() self.m.register('foo', b) assert 'foo' in self.m assert not 'bar' in self.m
from iris.ui import auth from beaker.middleware import SessionMiddleware logger = logging.getLogger(__name__) _filename_ascii_strip_re = re.compile(r'[^A-Za-z0-9_.-]') ui_root = os.environ.get('STATIC_ROOT', os.path.abspath(os.path.dirname(__file__))) assets_env = AssetsEnvironment(os.path.join(ui_root, 'static'), url='/static') assets_env.register( 'jquery_libs', Bundle('js/jquery-2.1.4.min.js', 'js/jquery.dataTables.min.js', 'js/handlebars.min.js', 'js/hopscotch.min.js', output='bundles/jquery.libs.js')) assets_env.register( 'bootstrap_libs', Bundle('js/bootstrap.min.js', 'js/typeahead.js', 'js/bootstrap-datetimepicker.js', output='bundles/bootstrap.libs.js')) assets_env.register( 'iris_js', Bundle('js/iris.js', filters='rjsmin', output='bundles/iris.js')) assets_env.register( 'css_libs', Bundle('css/bootstrap.min.css',
'js/lib/jquery.touchclick.js', 'bootstrap/js/bootstrap.min.js', 'js/audio.js', 'js/app.js', filters='uglifyjs', output='js/electris-footer.min.js') css = Bundle('bootstrap/css/bootstrap.min.css', 'bootstrap/css/bootstrap-responsive.min.css', 'css/template.css', 'css/app.css', 'css/sponsorship.css', filters='yui_css', output='css/electris.min.css') static.register('js_header', header_js) static.register('js_footer', footer_js) static.register('css', css) header_js.urls() footer_js.urls() css.urls() import logging from webassets.script import CommandLineEnvironment # Setup a logger log = logging.getLogger('webassets') log.addHandler(logging.StreamHandler()) log.setLevel(logging.DEBUG)
'js/jquery.dataTables.min.js', 'js/oms.min.js', Bundle('js/bootstrapx-clickover.js', 'js/tree.jquery.js', 'js/leaflet-providers.js', 'js/leaflet.label.js', 'js/Control.FullScreen.js', 'js/leaflet-hash.js', 'js/clld.js', 'project.js', filters='yui_js'), ], 'css': [ Bundle('css/leaflet.css', filters=(skip, )), Bundle('css/clld.css', 'css/jqtree.css', 'css/leaflet.label.css', 'css/hint.css', 'css/jquery.dataTables.css', filters='yui_css'), 'css/bootstrap.min.css', 'css/bootstrap-responsive.min.css', Bundle('project.css', filters='yui_css'), ], } for name in bundles: environment.register( name, *bundles[name], **dict(output='{0}/packed.%(version)s.{0}'.format(name)))
#!/usr/bin/env python # -*- coding: utf-8 -*- from webassets import Bundle from webassets import Environment import app.config as config env = Environment('./static', '/static') env.debug = config.DEBUG env.register( 'base_css', Bundle("bootstrap/css/bootstrap.min.css", "bootstrap/css/bootstrap-responsive.min.css", "bootstrap-datepicker/css/datepicker.css", "bootstrap-fileupload/css/bootstrap-fileupload.min.css", "css/main.css")) env.register( 'base_js', Bundle("jquery-form/jquery.form.js", "bootstrap/js/bootstrap.min.js", "bootstrap-datepicker/js/bootstrap-datepicker.js", "underscore/js/underscore.min.js", "js/sprintf.js", "js/common.js", "js/logger.js", "js/formatter.js", "js/palette.manager.js", "js/expenses.ui.js", "js/expenses.manager.js")) env.register('users_js', Bundle("js/users.ui.js", "js/users.manager.js")) env.register('categories_js', Bundle("js/categories.ui.js", "js/categories.manager.js"))
from iris.ui import auth from beaker.middleware import SessionMiddleware logger = logging.getLogger(__name__) _filename_ascii_strip_re = re.compile(r'[^A-Za-z0-9_.-]') ui_root = os.environ.get('STATIC_ROOT', os.path.abspath(os.path.dirname(__file__))) assets_env = AssetsEnvironment(os.path.join(ui_root, 'static'), url='/static') assets_env.register( 'jquery_libs', Bundle('js/jquery-3.3.1.min.js', 'js/jquery.dataTables.min.js', 'js/handlebars-4.0.12.min.js', 'js/hopscotch.min.js', 'js/marked.min.js', output='bundles/jquery.libs.js')) assets_env.register( 'bootstrap_libs', Bundle('js/bootstrap.min.js', 'js/typeahead.js', 'js/bootstrap-datetimepicker.js', 'js/moment-timezone.js', 'js/moment-tz-data.js', output='bundles/bootstrap.libs.js')) assets_env.register( 'iris_js', Bundle('js/iris.js', filters='rjsmin', output='bundles/iris.js')) assets_env.register(
class TestEnvApi(object): """General Environment functionality.""" def setup(self): self.m = Environment(None, None) def test_register_single_bundle(self): """Test registering a single ``Bundle`` object. """ b = Bundle() self.m.register('foo', b) assert self.m['foo'] == b def test_register_dict(self): """Register a bunch of bundles at once.""" a = Bundle() b = Bundle() self.m.register({'foo': a, 'bar': b}) assert self.m['foo'] == a assert self.m['bar'] == b def test_register_new_bundle(self): """Test self.m.registering a new bundle on the fly. """ b = Bundle() self.m.register('foo', b, 's2', 's3') assert b in self.m['foo'].contents # Special case of using only a single, non-bundle source argument. self.m.register('footon', 's1') assert 's1' in self.m['footon'].contents # Special case of specifying only a single bundle as a source, but # additional options - this also creates a wrapping bundle. self.m.register('foofighters', b, output="bar") assert b in self.m['foofighters'].contents def test_register_invalid_call(self): """Test calling self.m.register with an invalid syntax. """ assert_raises(TypeError, self.m.register) assert_raises(TypeError, self.m.register, 'one-argument-only') def test_register_duplicate(self): """Test name clashes. """ b1 = Bundle() b2 = Bundle() self.m.register('foo', b1) # Multiple calls with the same name are ignored if the given bundle # is the same as originally passed. self.m.register('foo', b1) assert len(self.m) == 1 # Otherwise, an error is raised. assert_raises(RegisterError, self.m.register, 'foo', b2) assert_raises(RegisterError, self.m.register, 'foo', 's1', 's2', 's3') def test_register_anon_bundle(self): """Self registering an anonymous bundle. """ b = Bundle() self.m.add(b) assert len(self.m) == 1 assert list(self.m) == [b] def test_contains(self): """Test __contains__. """ b = Bundle() self.m.register('foo', b) assert 'foo' in self.m assert not 'bar' in self.m def test_url_and_directory(self): """The url and directory options are a bit special, because they are so essential. """ # An environment can be constructed without given url or directory. env = Environment() # But then accessing them will fail, and with it most operations. assert_raises(EnvironmentError, getattr, env, 'url') assert_raises(EnvironmentError, getattr, env, 'directory') # Test constructing the environment with values for url and directory env = Environment('foo', 'bar') assert env.url == 'bar' assert env.config['directory'] == 'foo' assert env.directory == os.path.join(os.getcwd(), 'foo') def test_append_load_path(self): env = Environment() assert env.load_path == [] env.append_path('foo', '/foo') assert env.load_path == ['foo'] assert env.url_mapping == {'foo': '/foo'} # Works without path env.append_path('bar') assert env.load_path == ['foo', 'bar'] assert env.url_mapping == {'foo': '/foo'}
class IndicoPlugin(Plugin): """Base class for an Indico plugin All your plugins need to inherit from this class. It extends the `Plugin` class from Flask-PluginEngine with useful indico-specific functionality that makes it easier to write custom plugins. When creating your plugin, the class-level docstring is used to generate the friendly name and description of a plugin. Its first line becomes the name while everything else goes into the description. This class provides methods for some of the more common hooks Indico provides. Additional signals are defined in :mod:`~indico.core.signals` and can be connected to custom functions using :meth:`connect`. """ #: WTForm for the plugin's settings (requires `configurable=True`). #: All fields must return JSON-serializable types. settings_form = None #: A dictionary which can contain the kwargs for a specific field in the `settings_form`. settings_form_field_opts = {} #: A dictionary containing default values for settings default_settings = {} #: A dictionary containing default values for event-specific settings default_event_settings = {} #: A dictionary containing default values for user-specific settings default_user_settings = {} #: A set containing the names of settings which store ACLs acl_settings = frozenset() #: A set containing the names of event-specific settings which store ACLs acl_event_settings = frozenset() #: A dict containing custom converters for settings settings_converters = {} #: A dict containing custom converters for event-specific settings event_settings_converters = {} #: A dict containing custom converters for user-specific settings user_settings_converters = {} #: If the plugin should link to a details/config page in the admin interface configurable = False #: The group category that the plugin belongs to category = None #: If `settings`, `event_settings` and `user_settings` should use strict #: mode, i.e. only allow keys in `default_settings`, `default_event_settings` #: or `default_user_settings` (or the related `acl_settings` sets) strict_settings = False def init(self): """Called when the plugin is being loaded/initialized. If you want to run custom initialization code, this is the method to override. Make sure to call the base method or the other overridable methods in this class will not be called anymore. """ assert self.configurable or not self.settings_form, 'Non-configurable plugin cannot have a settings form' self.alembic_versions_path = os.path.join(self.root_path, 'migrations') self.connect(signals.plugin.cli, self.add_cli_command) self.connect(signals.plugin.get_blueprints, lambda app: self.get_blueprints()) self.template_hook('vars-js', self.inject_vars_js) self._setup_assets() self._import_models() def _setup_assets(self): config = Config.getInstance() url_base_path = urlparse(config.getBaseURL()).path output_dir = os.path.join(config.getHtdocsDir(), 'static', 'assets', 'plugins', self.name) output_url = '{}/static/assets/plugins/{}'.format(url_base_path, self.name) static_dir = os.path.join(self.root_path, 'static') static_url = '{}/static/plugins/{}'.format(url_base_path, self.name) self.assets = Environment(output_dir, output_url, debug=config.getDebug()) self.assets.append_path(output_dir, output_url) self.assets.append_path(static_dir, static_url) configure_pyscss(self.assets) self.register_assets() def _import_models(self): old_models = set(db.Model._decl_class_registry.items()) import_all_models(self.package_name) added_models = set(db.Model._decl_class_registry.items()) - old_models # Ensure that only plugin schemas have been touched. It would be nice if we could actually # restrict a plugin to plugin_PLUGNNAME but since we load all models from the plugin's package # which could contain more than one plugin this is not easily possible. for name, model in added_models: schema = model.__table__.schema if not schema.startswith('plugin_'): raise Exception("Plugin '{}' added a model which is not in a plugin schema ('{}' in '{}')" .format(self.name, name, schema)) def connect(self, signal, receiver, **connect_kwargs): connect_kwargs['weak'] = False func = wrap_in_plugin_context(self, receiver) func.indico_plugin = self signal.connect(func, **connect_kwargs) def get_blueprints(self): """Return blueprints to be registered on the application A single blueprint can be returned directly, for multiple blueprint you need to yield them or return an iterable. """ pass def get_vars_js(self): """Return a dictionary with variables to be added to vars.js file""" return None @cached_property def translation_path(self): """ Return translation files to be used by the plugin. By default, get <root_path>/translations, unless it does not exist """ translations_path = os.path.join(self.root_path, 'translations') return translations_path if os.path.exists(translations_path) else None @cached_property def translation_domain(self): """Return the domain for this plugin's translation_path""" path = self.translation_path return Domain(path) if path else NullDomain() def add_cli_command(self, manager): """Add custom commands/submanagers to the manager of the `indico` cli tool.""" pass def register_assets(self): """Add assets to the plugin's webassets environment. In most cases the whole method can consist of calls to :meth:`register_js_bundle` and :meth:`register_css_bundle`. """ pass def register_js_bundle(self, name, *files): """Registers a JS bundle in the plugin's webassets environment""" pretty_name = re.sub(r'_js$', '', name) bundle = Bundle(*files, filters='rjsmin', output='js/{}_%(version)s.min.js'.format(pretty_name)) self.assets.register(name, bundle) def register_css_bundle(self, name, *files): """Registers an SCSS bundle in the plugin's webassets environment""" pretty_name = re.sub(r'_css$', '', name) bundle = Bundle(*files, filters=('pyscss', 'cssrewrite', 'cssmin'), output='css/{}_%(version)s.min.css'.format(pretty_name), depends=SASS_BASE_MODULES) self.assets.register(name, bundle) def inject_css(self, name, view_class=None, subclasses=True, condition=None): """Injects a CSS bundle into Indico's pages :param name: Name of the bundle :param view_class: If a WP class is specified, only inject it into pages using that class :param subclasses: also inject into subclasses of `view_class` :param condition: a callable to determine whether to inject or not. only called, when the view_class criterion matches """ self._inject_asset(signals.plugin.inject_css, name, view_class, subclasses, condition) def inject_js(self, name, view_class=None, subclasses=True, condition=None): """Injects a JS bundle into Indico's pages :param name: Name of the bundle :param view_class: If a WP class is specified, only inject it into pages using that class :param subclasses: also inject into subclasses of `view_class` :param condition: a callable to determine whether to inject or not. only called, when the view_class criterion matches """ self._inject_asset(signals.plugin.inject_js, name, view_class, subclasses, condition) def _inject_asset(self, signal, name, view_class=None, subclasses=True, condition=None): """Injects an asset bundle into Indico's pages :param signal: the signal to use for injection :param name: Name of the bundle :param view_class: If a WP class is specified, only inject it into pages using that class :param subclasses: also inject into subclasses of `view_class` :param condition: a callable to determine whether to inject or not. only called, when the view_class criterion matches """ def _do_inject(sender): if condition is None or condition(): return self.assets[name].urls() if view_class is None: self.connect(signal, _do_inject) elif not subclasses: self.connect(signal, _do_inject, sender=view_class) else: def _func(sender): if issubclass(sender, view_class): return _do_inject(sender) self.connect(signal, _func) def inject_vars_js(self): """Returns a string that will define variables for the plugin in the vars.js file""" vars_js = self.get_vars_js() if vars_js: return 'var {}Plugin = {};'.format(self.name.title(), json.dumps(vars_js)) def template_hook(self, name, receiver, priority=50, markup=True): """Registers a function to be called when a template hook is invoked. For details see :func:~`indico.web.flask.templating.register_template_hook` """ register_template_hook(name, receiver, priority, markup, self) @classproperty @classmethod def logger(cls): return Logger.get('plugin.{}'.format(cls.name)) @cached_classproperty @classmethod def settings(cls): """:class:`SettingsProxy` for the plugin's settings""" if cls.name is None: raise RuntimeError('Plugin has not been loaded yet') instance = cls.instance with instance.plugin_context(): # in case the default settings come from a property return SettingsProxy('plugin_{}'.format(cls.name), instance.default_settings, cls.strict_settings, acls=cls.acl_settings, converters=cls.settings_converters) @cached_classproperty @classmethod def event_settings(cls): """:class:`EventSettingsProxy` for the plugin's event-specific settings""" if cls.name is None: raise RuntimeError('Plugin has not been loaded yet') instance = cls.instance with instance.plugin_context(): # in case the default settings come from a property return EventSettingsProxy('plugin_{}'.format(cls.name), instance.default_event_settings, cls.strict_settings, acls=cls.acl_event_settings, converters=cls.event_settings_converters) @cached_classproperty @classmethod def user_settings(cls): """:class:`UserSettingsProxy` for the plugin's user-specific settings""" if cls.name is None: raise RuntimeError('Plugin has not been loaded yet') instance = cls.instance with instance.plugin_context(): # in case the default settings come from a property return UserSettingsProxy('plugin_{}'.format(cls.name), instance.default_user_settings, cls.strict_settings, converters=cls.user_settings_converters)
import logging from webassets import Environment from webassets import Bundle from webassets.script import CommandLineEnvironment # Bundle and minify the javascript # Since Flask isn't serving the JS this needs to be done here # before the static files are pulled down on nginx # Kind of not the way I was hoping to handle this log = logging.getLogger('webassets') log.addHandler(logging.StreamHandler()) log.setLevel(logging.DEBUG) env = Environment('mojibake/static', '/static') js = Bundle('js/jquery.min.js', 'js/jquery-ui.custom.js', 'js/skel.min.js', 'js/skel-panels.min.js', 'js/init.js', 'js/mojibake.js', filters='jsmin', output='js/packed.js') env.register('js_all', js) # From the docs # https://webassets.readthedocs.org/en/latest/script.html cmdenv = CommandLineEnvironment(env, log) cmdenv.build()
class TestEnvApi(object): """General Environment functionality.""" def setup(self): self.m = Environment(None, None) def test_single_bundle(self): """Test registering a single ``Bundle`` object. """ b = Bundle() self.m.register('foo', b) assert self.m['foo'] == b def test_new_bundle(self): """Test self.m.registering a new bundle on the fly. """ b = Bundle() self.m.register('foo', b, 's2', 's3') assert b in self.m['foo'].contents # Special case of using only a single, non-bundle source argument. self.m.register('footon', 's1') assert 's1' in self.m['footon'].contents # Special case of specifying only a single bundle as a source, but # additional options - this also creates a wrapping bundle. self.m.register('foofighters', b, output="bar") assert b in self.m['foofighters'].contents def test_invalid_call(self): """Test calling self.m.register with an invalid syntax. """ assert_raises(TypeError, self.m.register) assert_raises(TypeError, self.m.register, 'one-argument-only') def test_duplicate(self): """Test name clashes. """ b1 = Bundle() b2 = Bundle() self.m.register('foo', b1) # Multiple calls with the same name are ignored if the given bundle # is the same as originally passed. self.m.register('foo', b1) assert len(self.m) == 1 # Otherwise, an error is raised. assert_raises(RegisterError, self.m.register, 'foo', b2) assert_raises(RegisterError, self.m.register, 'foo', 's1', 's2', 's3') def test_anon_bundle(self): """Self registering an anonymous bundle. """ b = Bundle() self.m.add(b) assert len(self.m) == 1 assert list(self.m) == [b] def test_contains(self): """Test __contains__. """ b = Bundle() self.m.register('foo', b) assert 'foo' in self.m assert not 'bar' in self.m def test_url_and_directory(self): """The url and directory options are a bit special, because they are so essential. """ # An environment can be constructed without given url or directory. env = Environment() # But then accessing them will fail, and with it most operations. assert_raises(EnvironmentError, getattr, env, 'url') assert_raises(EnvironmentError, getattr, env, 'directory') # Test constructing the environment with values for url and directory env = Environment('foo', 'bar') assert env.directory == 'foo' assert env.url == 'bar'