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_add_host(self): m_context = {'echarts_version': '3.8.5'} mhs = MapHostStore(context=m_context, default_host='pyecharts') mhs.add_host('/demo/', 'demo') mhs.add_host('/demo2/{echarts_version}', 'demo2') self.assertEqual('/demo/fujian.js', mhs.generate_js_link('fujian', js_host='demo')) self.assertEqual('/demo2/3.8.5/fujian.js', mhs.generate_js_link('fujian', js_host='demo2'))
def test_map_host(self): m_context = {'STATIC_URL': '/static/', 'echarts_version': '3.7.0'} hs = MapHostStore(context=m_context, default_host='echarts') self.assertEqual( 'https://echarts-maps.github.io/echarts-china-provinces-js/china.js', hs.generate_js_link('china', js_host='china-provinces')) # Add hs.add_host('https://amap.com/js', 'amap') self.assertEqual('https://amap.com/js/fujian.js', hs.generate_js_link('fujian', 'amap')) self.assertEqual('http://echarts.baidu.com/asset/map/js/china.js', hs.generate_js_link('china'))
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)