From db79da3f1fe986812d10cdfdc3d9aa51f11739c5 Mon Sep 17 00:00:00 2001 From: tobes Date: Mon, 20 Aug 2012 13:29:44 +0100 Subject: [PATCH] [#2618] Fanstatic refactoring --- ckan/lib/fanstatic_resources.py | 56 ++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/ckan/lib/fanstatic_resources.py b/ckan/lib/fanstatic_resources.py index 0706277b96c..13ecc52919a 100644 --- a/ckan/lib/fanstatic_resources.py +++ b/ckan/lib/fanstatic_resources.py @@ -59,9 +59,9 @@ def get_resource(lib_name, resource_name): def min_path(path): ''' return the .min filename eg moo.js -> moo.min.js ''' - if f.endswith('.js'): + if path.endswith('.js'): return path[:-3] + '.min.js' - if f.endswith('.css'): + if path.endswith('.css'): return path[:-4] + '.min.css' def minify(filename, resource_path, min_function): @@ -136,6 +136,12 @@ def create_resource(path, lib_name, count, inline=False): setattr(module, fanstatic_name, resource) return resource + resource_path = os.path.join(os.path.dirname(__file__), path) + library = Library(name, path) + module = sys.modules[__name__] + + + # config options order = [] dont_bundle = [] force_top = [] @@ -146,18 +152,18 @@ def create_resource(path, lib_name, count, inline=False): inline_scripts = {} # parse the resource.config file if it exists - resource_path = os.path.dirname(__file__) - resource_path = os.path.join(resource_path, path) config_path = os.path.join(resource_path, 'resource.config') if os.path.exists(config_path): config = ConfigParser.RawConfigParser() config.read(config_path) + if config.has_option('main', 'order'): order = config.get('main', 'order').split() if config.has_option('main', 'dont_bundle'): dont_bundle = config.get('main', 'dont_bundle').split() if config.has_option('main', 'force_top'): force_top = config.get('main', 'force_top').split() + if config.has_section('depends'): items = config.items('depends') depends = dict((n, v.split()) for (n, v) in items) @@ -180,6 +186,7 @@ def create_resource(path, lib_name, count, inline=False): IE_conditionals[f].append(n) + # add dependencies for resources in groups for group in groups: if group in depends: for resource in groups[group]: @@ -189,11 +196,8 @@ def create_resource(path, lib_name, count, inline=False): if dep not in depends[resource]: depends[resource].append(dep) - library = Library(name, path) - module = sys.modules[__name__] - # process each .js/.css file found - file_list = [] + resource_list = [] for dirname, dirnames, filenames in os.walk(resource_path): for f in filenames: rel_path = dirname[len(path):] @@ -202,10 +206,10 @@ def create_resource(path, lib_name, count, inline=False): filepath = os.path.join(rel_path, f) if f.endswith('.js') and not f.endswith('.min.js'): minify(f, dirname, jsmin) - file_list.append(filepath) + resource_list.append(filepath) if f.endswith('.css') and not f.endswith('.min.css'): minify(f, dirname, cssmin) - file_list.append(filepath) + resource_list.append(filepath) # if groups are defined make sure the order supplied there is honored for group in groups: @@ -219,18 +223,24 @@ def create_resource(path, lib_name, count, inline=False): order.append(dep) order.append(resource) - for x in reversed(order): - if x in file_list: - file_list.remove(x) - file_list.insert(0, x) - count = 0 - for f in file_list: - create_resource(f, name, count) - count += 1 - - #inline scripts + # add inline scripts for inline in inline_scripts: - create_resource(inline, name, count, inline=inline_scripts[inline].strip()) + resource_list.append(inline) + + # order resource_list so that resources are created in the correct order + for resource_name in reversed(order): + if resource_name in resource_list: + resource_list.remove(resource_name) + resource_list.insert(0, resource_name) + + # create the resources and keep them ordered as we define them. + count = 0 + for resource_name in resource_list: + if resource_name in inline_scripts: + inline = inline_scripts[resource_name].strip() + else: + inline = None + create_resource(resource_name, name, count, inline=inline) count += 1 # add groups @@ -239,10 +249,6 @@ def create_resource(path, lib_name, count, inline=False): for member in groups[group_name]: fanstatic_name = '%s/%s' % (name, member) members.append(getattr(module, fanstatic_name)) - if group_name in depends: - # add dependencies for each resource in the group - for dependency in depends[group_name]: - members = [get_resource(name, dependency)] + members group = Group(members) fanstatic_name = '%s/%s' % (name, group_name) setattr(module, fanstatic_name, group)