def _setup(self): self._host_context = { 'echarts_version': self._settings['echarts_version'] } if 'STATIC_URL' in self._extra_settings: self._host_context.update( {'STATIC_URL': self._extra_settings['STATIC_URL']}) self.lib_host_store = LibHostStore( context=self._host_context, default_host=self._settings['lib_js_host']) self.map_host_store = MapHostStore( context=self._host_context, default_host=self._settings['map_js_host'])
def test_lib_host(self): # Basic tests m_context = {'STATIC_URL': '/static/', 'echarts_version': '3.7.0'} hs = LibHostStore(context=m_context, default_host='bootcdn') self.assertEqual( 'https://cdn.bootcss.com/echarts/3.7.0/echarts.min.js', hs.generate_js_link('echarts.min')) self.assertEqual( 'https://cdnjs.cloudflare.com/ajax/libs/echarts/3.7.0/echarts.min.js', hs.generate_js_link('echarts.min', js_host='cdnjs')) self.assertEqual( 'https://cdn.bootcss.com/echarts/3.7.0/echarts.min.js', hs.generate_js_link( 'echarts.min', js_host='https://cdn.bootcss.com/echarts/{echarts_version}'))
class SettingsStore(object): def __init__(self, *, echarts_settings=None, extra_settings=None, **kwargs): # Pre check settings self._extra_settings = extra_settings or {} if self._extra_settings.get('lib_js_host') == 'local_host': self._extra_settings['lib_js_host'] = echarts_settings[ 'local_host'] if self._extra_settings.get('map_js_host') == 'local_host': self._extra_settings['map_js_host'] = echarts_settings[ 'local_host'] # Merge echarts settings self._settings = {**DEFAULT_SETTINGS, **self._extra_settings} self.lib_host_store = None self.map_host_store = None self._check() self._setup() def _check(self): local_host = self._settings.get('local_host') static_url = self._extra_settings.get('STATIC_URL') if local_host: if static_url: if not local_host.startswith( '{STATIC_URL}') and not local_host.startswith( static_url): raise ValueError( 'The local_host must start with the value of "settings.STATIC_URL"' ) else: raise ValueError( "The local_host item requires a no-empty settings.STATIC_URL." ) def _setup(self): self._host_context = { 'echarts_version': self._settings['echarts_version'] } if 'STATIC_URL' in self._extra_settings: self._host_context.update( {'STATIC_URL': self._extra_settings['STATIC_URL']}) self.lib_host_store = LibHostStore( context=self._host_context, default_host=self._settings['lib_js_host']) self.map_host_store = MapHostStore( context=self._host_context, default_host=self._settings['map_js_host']) # #### Public API: Generate js link using current configure ######## def generate_js_link(self, js_name, js_host=None, **kwargs): if JsUtils.is_lib_js(js_name): hs = self.lib_host_store else: hs = self.map_host_store return hs.generate_js_link(js_name=js_name, js_host=js_host) def generate_lib_js_link(self, js_name, js_host=None, **kwargs): return self.lib_host_store.generate_js_link(js_name=js_name, js_host=js_host) def generate_map_js_link(self, js_name, js_host=None, **kwargs): return self.map_host_store.generate_js_link(js_name=js_name, js_host=js_host) def generate_local_url(self, js_name): """ Generate the local url for a js file. :param js_name: :return: """ host = self._settings['local_host'].format( **self._host_context).rstrip('/') return '{}/{}.js'.format(host, js_name) @property def settings(self): return self._settings def get(self, key, default=None): return self._settings.get(key, default)