From 841ecf14ca3ff9238bb7d3a74f6748d4a2f4628d Mon Sep 17 00:00:00 2001 From: Romain Dorgueil Date: Tue, 8 Nov 2016 13:36:35 +0100 Subject: [PATCH] assets service: use json instead of simplejson, adds path attribute to allow overriding base (web) path of assets. --- windflow/services/assets.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/windflow/services/assets.py b/windflow/services/assets.py index 7ff8792..4fcc189 100644 --- a/windflow/services/assets.py +++ b/windflow/services/assets.py @@ -1,8 +1,14 @@ -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] @@ -10,7 +16,7 @@ def get_style(self, name): return '' try: - return '' + return '' except KeyError as e: return '' @@ -21,7 +27,7 @@ def get_script(self, name): return '' try: - return '' + return '' except KeyError as e: return '' @@ -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).")') - # def __getattr__(self, item): - # return self - class WebpackAssets(Service): """ @@ -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)