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 _setup_env(app='', debug=True, cache=True): """Setup the webassets environment.""" if app: app_path = path.join('..', '..', app, 'static') env = Environment(path.join(INPUT_FILES, '..', '..', 'skel', 'assets'), path.join(BASE_LOCATION, '..')) else: app_path = path.join('..', 'static') env = Environment(path.join(INPUT_FILES, '..', '..', 'skel', 'assets'), path.join(BASE_LOCATION)) # We use underscore's templates by default. env.config['JST_COMPILER'] = '_.template' if debug: env.config['CLOSURE_COMPRESSOR_OPTIMIZATION'] = 'WHITESPACE_ONLY' env.manifest = False else: env.config[ 'CLOSURE_COMPRESSOR_OPTIMIZATION'] = 'ADVANCED_OPTIMIZATIONS' env.debug = False env.cache = cache #javascript _bundle_skel(app_path, env, debug) #css _bundle_3rd_party_css(app_path, env, debug) #images _bundle_images(app, env, is_skel=True) return 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 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 test_options(self): """Test option declaration. """ class TestFilter(Filter): options = { 'attr1': 'ATTR1', 'attr2': ('secondattr', 'ATTR2'), 'attr3': (False, 'ATTR3'), 'attr4': ('attr4', False), } # Test __init__ arguments assert TestFilter(attr1='foo').attr1 == 'foo' assert TestFilter(secondattr='foo').attr2 == 'foo' assert_raises(TypeError, TestFilter, attr3='foo') assert TestFilter(attr4='foo').attr4 == 'foo' # Test config vars env = Environment(None, None) env.config['attr1'] = 'bar' env.config['attr4'] = 'bar' f = TestFilter() f.env = env f.setup() assert f.attr1 == 'bar' assert f.attr4 is None # Was configured to not support 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 test_options(self): """Test option declaration. """ class TestFilter(Filter): options = { "attr1": "ATTR1", "attr2": ("secondattr", "ATTR2"), "attr3": (False, "ATTR3"), "attr4": ("attr4", False), } # Test __init__ arguments assert TestFilter(attr1="foo").attr1 == "foo" assert TestFilter(secondattr="foo").attr2 == "foo" assert_raises(TypeError, TestFilter, attr3="foo") assert TestFilter(attr4="foo").attr4 == "foo" # Test config vars env = Environment(None, None) env.config["attr1"] = "bar" env.config["attr4"] = "bar" f = TestFilter() f.env = env f.setup() assert f.attr1 == "bar" assert f.attr4 is None # Was configured to not support env
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 webassets_init(): global env static_path = get_webassets_path() public = config.get(u'ckan.base_public_folder') public_folder = os.path.abspath( os.path.join(os.path.dirname(__file__), u'..', public)) base_path = os.path.join(public_folder, u'base') env = Environment() env.directory = static_path env.debug = asbool(config.get(u'debug', False)) env.url = u'/webassets/' add_public_path(base_path, u'/base/') logger.debug(u'Base path {0}'.format(base_path)) create_library(u'vendor', os.path.join(base_path, u'vendor')) create_library(u'base', os.path.join(base_path, u'javascript')) create_library(u'datapreview', os.path.join(base_path, u'datapreview')) create_library(u'css', os.path.join(base_path, u'css'))
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 _set_env(debug=True, cache=False): env = Environment(BASE_PATH) if debug: env.manifest = False env.cache = cache return 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 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 get_template_namespace(self): _ = super(BaseHandler, self).get_template_namespace() self.assets = Environment( os.path.join(os.path.dirname(__file__), '../static'), '/static') css_all = Bundle('css/bootstrap.min.css', 'css/material.min.css', Bundle('css/schoolcms.css', 'css/dropdown.css', filters='cssmin'), 'outdatedbrowser/outdatedbrowser.min.css', output='dict/plugin.min.css') js_all = Bundle(Bundle('outdatedbrowser/outdatedbrowser.min.js', 'react-0.13.2/react-with-addons.min.js', 'js/jquery-2.1.3.min.js', 'js/bootstrap.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('schoolcms/init.jsx', 'schoolcms/mixin/*.jsx', 'schoolcms/component/*.jsx', 'schoolcms/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() _['system_name'] = options.system_name _['SERVER_DEBUG'] = options.server_debug _['ip'] = self.request.remote_ip _['system_version'] = system_version _['_host'] = self.request.host _['_protocol'] = self.request.protocol if self.current_user: groups = GroupList.get_user_groups(self.current_user.key, self.sql_session) else: groups = [] _['current_user'] = self.current_user.to_dict( ) if self.current_user else None _['current_groups'] = groups # Call this to set the cookie self.xsrf_token return _
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 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"}
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 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 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 setUp(self): res = dirname(__file__)+'/resources/' self.env = Environment(dirname(__file__)+"/out", '/media-prefix') self.env.cache = False self.env.manifest = False self.env.append_path(res+"assets", "/") self.env.append_path(res+"vendor", "/vendors") self.env.append_path(res, None) self.env.config["compass_bin"] = "/home/arkus/.gem/ruby/1.9.1/bin/compass" self.env.config["vendor_path"] = "vendor"
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 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 _setup_env(app='', debug=True, cache=True): """Setup the webassets environment.""" if app: app_path = path.join('..', '..', app, 'static') env = Environment( path.join(INPUT_FILES, '..', '..', 'skel', 'assets'), path.join(BASE_LOCATION, '..')) else: app_path = path.join('..', 'static') env = Environment( path.join(INPUT_FILES, '..', '..', 'skel', 'assets'), path.join(BASE_LOCATION)) # We use underscore's templates by default. env.config['JST_COMPILER'] = '_.template' if debug: env.config['CLOSURE_COMPRESSOR_OPTIMIZATION'] = 'WHITESPACE_ONLY' env.manifest = False else: env.config['CLOSURE_COMPRESSOR_OPTIMIZATION'] = 'ADVANCED_OPTIMIZATIONS' env.debug = False env.cache = cache #javascript _bundle_app_coffee(app_path, env, debug) _bundle_3rd_party_js(app_path, env, debug) #css _bundle_3rd_party_css(app_path, env, debug) #images _bundle_images(app, env, is_skel=True) return env
def _setup_env(debug=True, cache=True): """Setup the webassets environment.""" env = Environment(INPUT_FILES, OUTPUT_FILES) # We use underscore's templates by default. env.config['JST_COMPILER'] = '_.template' if debug: env.config['CLOSURE_COMPRESSOR_OPTIMIZATION'] = 'WHITESPACE_ONLY' env.manifest = False else: env.config[ 'CLOSURE_COMPRESSOR_OPTIMIZATION'] = 'ADVANCED_OPTIMIZATIONS' env.debug = False env.cache = cache #javascript _bundle_app_jsts(env, debug) _bundle_app_coffee(env, debug) _bundle_3rd_party_js(env, debug) #css _bundle_app_less(env, debug) _bundle_3rd_party_css(env, debug) #images _bundle_images(env) return env
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'
def test_getconfig_os_env_types(self): """Test type conversion for values read from the environment. """ m = Environment(None, None) f = Filter() f.set_environment(m) get_config = f.get_config with os_environ_sandbox(): os.environ["foo"] = "one,two\,three" assert list(get_config(env="foo", type=list)) == ["one", "two,three"] # Make sure the split is not applied to env config values m.config["foo"] = "one,two\,three" assert get_config(setting="foo", type=list) == "one,two\,three"
def test_getconfig_os_env_types(self): """Test type conversion for values read from the environment. """ m = Environment(None, None) f = Filter() f.set_environment(m) get_config = f.get_config with os_environ_sandbox(): os.environ['foo'] = 'one,two\,three' assert get_config(env='foo', type=list) == ['one', 'two,three'] # Make sure the split is not applied to env config values m.config['foo'] = 'one,two\,three' assert get_config(setting='foo', type=list) == 'one,two\,three'
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 create_assets_env(generator): """Define the assets environment and pass it to the generator.""" theme_static_dir = generator.settings["THEME_STATIC_DIR"] assets_destination = os.path.join(generator.output_path, theme_static_dir) generator.env.assets_environment = Environment(assets_destination, theme_static_dir) if "ASSET_CONFIG" in generator.settings: for item in generator.settings["ASSET_CONFIG"]: generator.env.assets_environment.config[item[0]] = item[1] if "ASSET_BUNDLES" in generator.settings: for name, args, kwargs in generator.settings["ASSET_BUNDLES"]: generator.env.assets_environment.register(name, *args, **kwargs) if "ASSET_DEBUG" in generator.settings: generator.env.assets_environment.debug = generator.settings[ "ASSET_DEBUG"] elif logging.getLevelName(logger.getEffectiveLevel()) == "DEBUG": generator.env.assets_environment.debug = True for path in generator.settings[ "THEME_STATIC_PATHS"] + generator.settings.get( "ASSET_SOURCE_PATHS", []): full_path = os.path.join(generator.theme, path) generator.env.assets_environment.append_path(full_path)
def test_initial_values_override_defaults(self): """[Bug] If a dict of initial values are passed to the environment, they override any defaults the environment might want to set. """ env = Environment(None, None, debug='foo') assert env.debug == 'foo'
def create_assets_env(generator): """Define the assets environment and pass it to the generator.""" theme_static_dir = generator.settings['THEME_STATIC_DIR'] assets_src = os.path.join(generator.output_path, theme_static_dir) generator.env.assets_environment = Environment(assets_src, theme_static_dir) if 'ASSET_CONFIG' in generator.settings: for item in generator.settings['ASSET_CONFIG']: generator.env.assets_environment.config[item[0]] = item[1] if 'ASSET_BUNDLES' in generator.settings: for name, args, kwargs in generator.settings['ASSET_BUNDLES']: generator.env.assets_environment.register(name, *args, **kwargs) if logging.getLevelName(logger.getEffectiveLevel()) == "DEBUG": generator.env.assets_environment.debug = True if 'ASSET_SOURCE_PATHS' in generator.settings: # the default load path gets overridden if additional paths are # specified, add it back generator.env.assets_environment.append_path(assets_src) for path in generator.settings['ASSET_SOURCE_PATHS']: full_path = os.path.join(generator.theme, path) generator.env.assets_environment.append_path(full_path)
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 __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") )
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 _setup_env(debug=True, cache=True): """Setup the webassets environment.""" env = Environment(INPUT_FILES, OUTPUT_FILES) # We use underscore's templates by default. env.config['JST_COMPILER'] = '_.template' if debug: env.config['CLOSURE_COMPRESSOR_OPTIMIZATION'] = 'WHITESPACE_ONLY' env.manifest = False else: env.config['CLOSURE_COMPRESSOR_OPTIMIZATION'] = 'ADVANCED_OPTIMIZATIONS' env.debug = False env.cache = cache #javascript _bundle_app_jsts(env, debug) _bundle_app_coffee(env, debug) _bundle_3rd_party_js(env, debug) #css _bundle_app_less(env, debug) _bundle_3rd_party_css(env, debug) #images _bundle_images(env) return env
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 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 setup(self): self.dir_created = tempfile.mkdtemp() self.m = Environment(self.dir_created, '') # Unless we explicitly test it, we don't want to use the cache # during testing. self.m.cache = False # Some generic files to be used by simple tests self.create_files(self.default_files)
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 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_get_config(self): """Test the ``get_config`` helper. """ m = Environment(None, None) f = Filter() f.set_environment(m) get_config = f.get_config # For the purposes of the following tests, we use two test # names which we expect to be undefined in os.env. NAME = 'FOO%s' % id(object()) NAME2 = 'FOO%s' % id(NAME) assert NAME != NAME2 assert not NAME in os.environ and not NAME2 in os.environ try: # Test raising of error, and test not raising it. assert_raises(EnvironmentError, get_config, NAME) assert get_config(NAME, require=False) == None # Start with only the environment variable set. os.environ[NAME] = 'bar' assert get_config(NAME) == 'bar' assert get_config(env=NAME, setting=False) == 'bar' assert_raises(EnvironmentError, get_config, setting=NAME, env=False) # Set the value in the environment as well. m.config[NAME] = 'foo' # Ensure that settings take precedence. assert_equals(get_config(NAME), 'foo') # Two different names can be supplied. assert get_config(setting=NAME2, env=NAME) == 'bar' # Unset the env variable, now with only the setting. del os.environ[NAME] assert get_config(NAME) == 'foo' assert get_config(setting=NAME, env=False) == 'foo' assert_raises(EnvironmentError, get_config, env=NAME) finally: if NAME in os.environ: del os.environ[NAME]
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 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
def create_assets_env(generator): """Define the assets environment and pass it to the generator.""" assets_url = 'theme/' assets_src = os.path.join(generator.output_path, 'theme') generator.env.assets_environment = Environment(assets_src, assets_url) logger = logging.getLogger(__name__) if logging.getLevelName(logger.getEffectiveLevel()) == "DEBUG": generator.env.assets_environment.debug = True
class TestCLI(object): def setup(self): self.assets_env = Environment("", "") self.cmd_env = CommandLineEnvironment(self.assets_env, logging) def test_rebuild_container_bundles(self): """Test the rebuild command can deal with container bundles. """ a = MockBundle(output="a") b1 = MockBundle(output="b1") b2 = MockBundle(output="b2") b = MockBundle(b1, b2) self.assets_env.add(a, b) self.cmd_env.rebuild() assert a.build_called assert not b.build_called assert b1.build_called assert b2.build_called
class TestCLI(object): def setup(self): self.assets_env = Environment('', '') self.cmd_env = CommandLineEnvironment(self.assets_env, logging) def test_rebuild_container_bundles(self): """Test the rebuild command can deal with container bundles. """ a = MockBundle(output='a') b1 = MockBundle(output='b1') b2 = MockBundle(output='b2') b = MockBundle(b1, b2) self.assets_env.add(a, b) self.cmd_env.rebuild() assert a.build_called assert not b.build_called assert b1.build_called assert b2.build_called
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_get_config(self): """Test the ``get_config`` helper. """ m = Environment(None, None) f = Filter() f.set_environment(m) get_config = f.get_config # For the purposes of the following tests, we use two test # names which we expect to be undefined in os.env. NAME = "FOO%s" % id(object()) NAME2 = "FOO%s" % id(NAME) assert NAME != NAME2 assert not NAME in os.environ and not NAME2 in os.environ try: # Test raising of error, and test not raising it. assert_raises(EnvironmentError, get_config, NAME) assert get_config(NAME, require=False) == None # Start with only the environment variable set. os.environ[NAME] = "bar" assert get_config(NAME) == "bar" assert get_config(env=NAME, setting=False) == "bar" assert_raises(EnvironmentError, get_config, setting=NAME, env=False) # Set the value in the environment as well. m.config[NAME] = "foo" # Ensure that settings take precedence. assert_equals(get_config(NAME), "foo") # Two different names can be supplied. assert get_config(setting=NAME2, env=NAME) == "bar" # Unset the env variable, now with only the setting. del os.environ[NAME] assert get_config(NAME) == "foo" assert get_config(setting=NAME, env=False) == "foo" assert_raises(EnvironmentError, get_config, env=NAME) finally: if NAME in os.environ: del os.environ[NAME]
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 get_template_namespace(self): _ = super(BaseHandler, self).get_template_namespace() self.assets = Environment( os.path.join(os.path.dirname(__file__), '../static'),'/static') css_all = Bundle( 'css/bootstrap.min.css', 'css/material.min.css', Bundle('css/schoolcms.css','css/dropdown.css', filters='cssmin'), 'outdatedbrowser/outdatedbrowser.min.css', output='dict/plugin.min.css') js_all = Bundle( Bundle( 'outdatedbrowser/outdatedbrowser.min.js', 'react-0.13.2/react-with-addons.min.js', 'js/jquery-2.1.3.min.js', 'js/bootstrap.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( 'schoolcms/init.jsx', 'schoolcms/mixin/*.jsx', 'schoolcms/component/*.jsx', 'schoolcms/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() _['system_name'] = options.system_name _['SERVER_DEBUG'] = options.server_debug _['ip'] = self.request.remote_ip _['system_version'] = system_version _['_host'] = self.request.host _['_protocol'] = self.request.protocol if self.current_user: groups = GroupList.get_user_groups(self.current_user.key, self.sql_session) else: groups = [] _['current_user'] = self.current_user.to_dict() if self.current_user else None _['current_groups'] = groups # Call this to set the cookie self.xsrf_token return _
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 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 create_assets_env(generator): """Define the assets environment and pass it to the generator.""" assets_url = 'theme/' assets_src = os.path.join(generator.output_path, 'theme') generator.env.assets_environment = Environment(assets_src, assets_url) if 'ASSET_CONFIG' in generator.settings: for item in generator.settings['ASSET_CONFIG']: generator.env.assets_environment.config[item[0]] = item[1] if logging.getLevelName(logger.getEffectiveLevel()) == "DEBUG": generator.env.assets_environment.debug = True