Skip to content

Commit

Permalink
assets service: use json instead of simplejson, adds path attribute t…
Browse files Browse the repository at this point in the history
…o allow overriding base (web) path of assets.
  • Loading branch information
hartym committed Nov 8, 2016
1 parent 010d2d5 commit 841ecf1
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions windflow/services/assets.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import simplejson
import json
import os

from windflow.services import Service


class Assets(dict):
def __init__(self, path, iterable=None, **kwargs):
super().__init__(iterable or (), **kwargs)
self._path = path

def get_style(self, name):
try:
bundle = self[name]
except KeyError as e:
return ''

try:
return '<link href="' + bundle['css'] + '" rel="stylesheet">'
return '<link href="' + os.path.join(self._path, bundle['css']) + '" rel="stylesheet">'
except KeyError as e:
return ''

Expand All @@ -21,7 +27,7 @@ def get_script(self, name):
return ''

try:
return '<script src="' + bundle['js'] + '" type="text/javascript"></script>'
return '<script src="' + os.path.join(self._path, bundle['js']) + '" type="text/javascript"></script>'
except KeyError as e:
return ''

Expand All @@ -35,9 +41,6 @@ def get_script(self, name):
'bundling process is still running, or that the webpack AssetsPlugin did not run. Look for the '
'assets.json file in your static directory (and look at webpack output).")</script>')

# def __getattr__(self, item):
# return self


class WebpackAssets(Service):
"""
Expand All @@ -48,12 +51,16 @@ class WebpackAssets(Service):
"""

filename = 'static/assets.json'
path = '/'

assets_type = Assets
unavailable_assets_type = UnavailableAssets

def get(self):
try:
with open(self.filename) as f:
return Assets(simplejson.load(f))
return self.assets_type(self.path, json.load(f))
except:
# todo this is evil, but we'd need to find out errors e do want to "ignore" or how to tell user about it.
# at the very minimum: simplejson.scanner.JSONDecodeError and probably missing file
return UnavailableAssets()
return self.unavailable_assets_type(self.path)

0 comments on commit 841ecf1

Please sign in to comment.