Skip to content

Commit

Permalink
[#2618] Minimise the monkey patching of fanstatic
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Aug 28, 2012
1 parent ec105c4 commit 4b9cd18
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 117 deletions.
112 changes: 0 additions & 112 deletions ckan/lib/fanstatic_extensions.py
Expand Up @@ -58,118 +58,6 @@ def render_js(url):
return '<script type="text/javascript" src="%s"></script>' % (url,)


# Fanstatic Patch #
# FIXME add full license info & push upstream
def __init__(self, library, relpath, **kw):

depends = kw.get('depends', None)
supersedes = kw.get('supersedes', None)
bottom = kw.get('bottom', False)
renderer = kw.get('renderer', None)
dont_bundle = kw.get('dont_bundle', False)
custom_renderer_order = kw.get('custom_renderer_order', None)
custom_order = kw.get('custom_order', 0)

# we don't want to pass these again
minified = kw.pop('minified', None)
debug = kw.pop('debug', None)

self.library = library
fullpath = os.path.normpath(os.path.join(library.path, relpath))
if core._resource_file_existence_checking \
and not os.path.exists(fullpath) \
and not kw.get('fake_resource', False):
raise core.UnknownResourceError("Resource file does not exist: %s" %
fullpath)
self.relpath = relpath
self.dirname, self.filename = os.path.split(relpath)
if self.dirname and not self.dirname.endswith('/'):
self.dirname += '/'
self.bottom = bottom
self.dont_bundle = dont_bundle
self.custom_order = custom_order

self.ext = os.path.splitext(self.relpath)[1]
if renderer is None:
# No custom, ad-hoc renderer for this Resource, so lookup
# the default renderer by resource filename extension.
if self.ext not in core.inclusion_renderers:
raise core.UnknownResourceExtensionError(
"Unknown resource extension %s for resource: %s" %
(self.ext, repr(self)))
self.order, self.renderer = core.inclusion_renderers[self.ext]
else:
# Use the custom renderer.
self.renderer = renderer
# If we do not know about the filename extension inclusion
# order, we render the resource after all others.
self.order, _ = core.inclusion_renderers.get(
self.ext, (sys.maxint, None))

if custom_renderer_order is not None:
self.order = custom_renderer_order
assert not isinstance(depends, basestring)
self.depends = set()
if depends is not None:
# Normalize groups into the underlying resources...
depends = core.normalize_groups(depends)
# ...before updating the set of dependencies of this resource.
self.depends.update(depends)

self.resources = set([self])
for depend in self.depends:
self.resources.update(depend.resources)

# Check for library dependency cycles.
self.library.check_dependency_cycle(self)

# generate an internal number for sorting the resource
# on dependency within the library
self.init_dependency_nr()

self.modes = {}
for mode_name, argument in [(core.DEBUG, debug),
(core.MINIFIED, minified)]:
if argument is None:
continue
elif isinstance(argument, basestring):
mode_resource = Resource(library, argument, **kw)
else:
# The dependencies of a mode resource should be the same
# or a subset of the dependencies this mode replaces.
if len(argument.depends - self.depends) > 0:
raise core.ModeResourceDependencyError
mode_resource = argument

mode_resource.dependency_nr = self.dependency_nr
self.modes[mode_name] = mode_resource

assert not isinstance(supersedes, basestring)
self.supersedes = supersedes or []

self.rollups = []
# create a reference to the superseder in the superseded resource
for resource in self.supersedes:
resource.rollups.append(self)
# also create a reference to the superseding mode in the superseded
# mode
# XXX what if mode is full-fledged resource which lists
# supersedes itself?
for mode_name, mode in self.modes.items():
for resource in self.supersedes:
superseded_mode = resource.mode(mode_name)
# if there is no such mode, let's skip it
if superseded_mode is resource:
continue
mode.supersedes.append(superseded_mode)
superseded_mode.rollups.append(mode)

# Register ourself with the Library.
self.library.register(self)

core.Resource.__init__ = __init__


def render(self, library_url):

paths = [resource.relpath for resource in self._resources]
Expand Down
22 changes: 17 additions & 5 deletions ckan/lib/fanstatic_resources.py
Expand Up @@ -68,22 +68,20 @@ def create_resource(path, lib_name, count, inline=False):
kw['bottom'] = True
if filename.endswith('.css'):
renderer = core.render_css
core.set_resource_file_existence_checking(True)
else:
kw['fake_resource'] = True
# This doesn't exist so stop fanstatic checking the filesystem
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
if path in custom_render_order:
kw['custom_renderer_order'] = custom_render_order[path]
kw['custom_order'] = count
# IE conditionals
condition = None
other_browsers = False
Expand All @@ -102,6 +100,20 @@ def create_resource(path, lib_name, count, inline=False):
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)
Expand Down

0 comments on commit 4b9cd18

Please sign in to comment.