def __init__(self, library, path, defer=False, resource_type=None, **kwargs): set_resource_file_existence_checking(False) try: if 'renderer' in kwargs: del kwargs['renderer'] if 'bottom' not in kwargs: kwargs['bottom'] = path.endswith('.js') Resource.__init__(self, library, path, renderer=self.render, **kwargs) finally: set_resource_file_existence_checking(True) self.defer = defer if resource_type: self.resource_type = resource_type else: self.resource_type = path.rsplit('.', 1)[1].lower()
fanstatic_lib = Library('admin', 'static') admin_js = Resource( fanstatic_lib, 'build/admin/admin.js', depends=[fanstatic_resources.jqueryui]) admin_css = Resource( fanstatic_lib, 'build/admin/admin.css', depends=[fanstatic_resources.fa_uiadmin_css]) # HACK to invoke fanstatic to inject a script which content is dynamic: # the content of the script sets OpenLayers.ImgPath to an url that is # dynamically generated using request.static_url olimgpath_js = None # deactive resource checking in fanstatic set_resource_file_existence_checking(False) def get_fanstatic_resources(request): # pragma: no cover global olimgpath_js olimgpath = request.static_url('c2cgeoportal:static/lib/openlayers/img/') def olimgpath_renderer(url): return '<script>OpenLayers.ImgPath="%s";</script>' % olimgpath if olimgpath_js is None: olimgpath_js = Resource( fanstatic_lib, 'whatever.js', renderer=olimgpath_renderer, depends=[admin_js])
def create_resource(path, lib_name, count, inline=False): ''' create the fanstatic Resource ''' renderer = None kw = {} if not inline: # resource_name is name of the file without the .js/.css rel_path, filename = os.path.split(path) filename = os.path.join(rel_path, filename) path_min = min_path(os.path.join(resource_path, filename)) if os.path.exists(path_min): kw['minified'] = min_path(filename) if filename.endswith('.js'): renderer = core.render_js if path not in force_top: kw['bottom'] = True if filename.endswith('.css'): renderer = core.render_css core.set_resource_file_existence_checking(True) else: # This doesn't exist so stop fanstatic checking the filesystem if path not in force_top: kw['bottom'] = True core.set_resource_file_existence_checking(False) dependencies = [] if path in depends: for dependency in depends[path]: dependencies.append(get_resource(name, dependency)) if depend_base: dependencies.append(getattr(module, 'base/main')) if dependencies: kw['depends'] = dependencies if path in dont_bundle: kw['dont_bundle'] = True # IE conditionals condition = None other_browsers = False if path in IE_conditionals: other_browsers = ('others' in IE_conditionals[path]) condition = IE_conditionals[path][0] if inline or condition: kw['renderer'] = fanstatic_extensions.CkanCustomRenderer( condition=condition, script=inline, renderer=renderer, other_browsers=other_browsers) resource = Resource(library, path, **kw) # Add our customised ordering if path in custom_render_order: resource.order = custom_render_order[path] resource.custom_order = count # Update the attributes of the minified version of the resource to # that of the parents as fanstatic does not pass these on. update_attributes = [ 'custom_order', 'order', 'bottom', 'depends', 'dont_bundle', 'renderer' ] if 'minified' in resource.modes: min_res = resource.modes['minified'] for attribute in update_attributes: setattr(min_res, attribute, getattr(resource, attribute)) # add the resource to this module fanstatic_name = '%s/%s' % (lib_name, path) #log.debug('create resource %s' % fanstatic_name) setattr(module, fanstatic_name, resource) return resource
raise ValueError("No elements matched by %s." % repr(xpath)) # let's get rid of blank lines result = result.replace('\n\n', '\n') # self-closing tags are more readable with a space before the # end-of-tag marker result = result.replace('"/>', '" />') return result def format_html(input): # pylint: disable=redefined-builtin """Render formatted HTML code by removing empty lines and spaces ending lines >>> from pyams_utils.testing import format_html >>> format_html('''<div> \\n<b>This is a test</b> \\n\\n</div> ''') '<div>\\n<b>This is a test</b>\\n</div>' """ return '\n'.join(filter(bool, map(str.rstrip, input.split('\n')))) if sys.argv[-1].endswith('/bin/test'): library = Library('foo', '') # pylint: disable=invalid-name set_resource_file_existence_checking(False) res_x1 = Resource(library, 'x1.js') # pylint: disable=invalid-name res_x1.dependency_nr = 0 set_resource_file_existence_checking(True)
def create_resource(path, lib_name, count, inline=False): ''' create the fanstatic Resource ''' renderer = None kw = {} if not inline: # resource_name is name of the file without the .js/.css rel_path, filename = os.path.split(path) filename = os.path.join(rel_path, filename) path_min = min_path(os.path.join(resource_path, filename)) if os.path.exists(path_min): kw['minified'] = min_path(filename) if filename.endswith('.js'): renderer = core.render_js if path not in force_top: kw['bottom'] = True if filename.endswith('.css'): renderer = core.render_css core.set_resource_file_existence_checking(True) else: # This doesn't exist so stop fanstatic checking the filesystem if path not in force_top: kw['bottom'] = True core.set_resource_file_existence_checking(False) dependencies = [] if path in depends: for dependency in depends[path]: dependencies.append(get_resource(name, dependency)) if depend_base: dependencies.append(getattr(module, 'base/main')) if dependencies: kw['depends'] = dependencies if path in dont_bundle: kw['dont_bundle'] = True # IE conditionals condition = None other_browsers = False if path in IE_conditionals: other_browsers = ('others' in IE_conditionals[path]) condition = IE_conditionals[path][0] if inline or condition: kw['renderer'] = fanstatic_extensions.CkanCustomRenderer( condition=condition, script=inline, renderer=renderer, other_browsers=other_browsers) resource = Resource(library, path, **kw) # Add our customised ordering if path in custom_render_order: resource.order = custom_render_order[path] resource.custom_order = count # Update the attributes of the minified version of the resource to # that of the parents as fanstatic does not pass these on. update_attributes = ['custom_order', 'order', 'bottom', 'depends', 'dont_bundle', 'renderer'] if 'minified' in resource.modes: min_res = resource.modes['minified'] for attribute in update_attributes: setattr(min_res, attribute, getattr(resource, attribute)) # add the resource to this module fanstatic_name = '%s/%s' % (lib_name, path) setattr(module, fanstatic_name, resource) return resource
def create_resource(path, lib_name, count, inline=False): """ create the fanstatic Resource """ renderer = None kw = {} if not inline: # resource_name is name of the file without the .js/.css rel_path, filename = os.path.split(path) filename = os.path.join(rel_path, filename) path_min = min_path(os.path.join(resource_path, filename)) if os.path.exists(path_min): kw["minified"] = min_path(filename) if filename.endswith(".js"): renderer = core.render_js if path not in force_top: kw["bottom"] = True if filename.endswith(".css"): renderer = core.render_css core.set_resource_file_existence_checking(True) else: # This doesn't exist so stop fanstatic checking the filesystem if path not in force_top: kw["bottom"] = True core.set_resource_file_existence_checking(False) dependencies = [] if path in depends: for dependency in depends[path]: dependencies.append(get_resource(name, dependency)) if depend_base: dependencies.append(getattr(module, "base/main")) if dependencies: kw["depends"] = dependencies if path in dont_bundle: kw["dont_bundle"] = True # IE conditionals condition = None other_browsers = False if path in IE_conditionals: other_browsers = "others" in IE_conditionals[path] condition = IE_conditionals[path][0] if inline or condition: kw["renderer"] = fanstatic_extensions.CkanCustomRenderer( condition=condition, script=inline, renderer=renderer, other_browsers=other_browsers ) resource = Resource(library, path, **kw) # Add our customised ordering if path in custom_render_order: resource.order = custom_render_order[path] resource.custom_order = count # Update the attributes of the minified version of the resource to # that of the parents as fanstatic does not pass these on. update_attributes = ["custom_order", "order", "bottom", "depends", "dont_bundle", "renderer"] if "minified" in resource.modes: min_res = resource.modes["minified"] for attribute in update_attributes: setattr(min_res, attribute, getattr(resource, attribute)) # add the resource to this module fanstatic_name = "%s/%s" % (lib_name, path) # log.debug('create resource %s' % fanstatic_name) setattr(module, fanstatic_name, resource) return resource