Skip to content

Commit

Permalink
🎨 refactoring the code
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed Aug 30, 2017
1 parent c65c835 commit 5c48ebe
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ before_install:
script:
- make tests
after_success:
codecov
- cd test
- codecov

4 changes: 1 addition & 3 deletions pyecharts/charts/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pyecharts.base import Base
from pyecharts.option import get_all_options
from pyecharts.template import CITY_NAME_PINYIN_MAP
from pyecharts.constants import CITY_NAME_PINYIN_MAP

PY2 = sys.version_info[0] == 2

Expand Down Expand Up @@ -63,8 +63,6 @@ def __add(self, name, attr, value,
"data": _data,
"roam": is_roam
})
if PY2:
maptype = maptype.decode('utf-8')
name_in_pinyin = CITY_NAME_PINYIN_MAP.get(maptype, maptype)
self._js_dependencies.add(name_in_pinyin)
self._config_components(**kwargs)
18 changes: 18 additions & 0 deletions pyecharts/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,30 @@
# coding=utf-8
from __future__ import unicode_literals

import os
import json
import codecs
from pyecharts.utils import get_resource_dir


DEFAULT_HOST = 'https://chfw.github.io/jupyter-echarts/echarts'

CONFIGURATION = dict(
HOST='/nbextensions/echarts'
)

DEFAULT_ECHARTS_REGISTRY = os.path.join(
get_resource_dir('templates'), 'js', 'echarts', 'registry.json')

with codecs.open(DEFAULT_ECHARTS_REGISTRY, 'r', 'utf-8') as f:
content = f.read()
CONFIG = json.loads(content)

DEFAULT_JS_LIBRARIES = CONFIG['FILE_MAP']

CITY_NAME_PINYIN_MAP = CONFIG['PINYIN_MAP']


CITY_GEO_COORDS = {
'阿城': [126.58, 45.32],
'阿克苏': [80.19, 41.09],
Expand Down
26 changes: 3 additions & 23 deletions pyecharts/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,9 @@
import codecs
from jinja2 import Environment, FileSystemLoader
import pyecharts.constants as constants
import json
from pyecharts.utils import get_resource_dir


def get_resource_dir(folder):
"""
:param folder:
:return:
"""
current_path = os.path.dirname(__file__)
resource_path = os.path.join(current_path, folder)
return resource_path


#with codecs.open(os.path.join(get_resource_dir('templates'), 'js', 'echarts', 'registry.json'), 'r', 'utf-8') as f:
with open(os.path.join(get_resource_dir('templates'), 'js', 'echarts', 'registry.json'), 'rb') as f:
content = f.read().decode('utf-8')
CONFIG = json.loads(content)

DEFAULT_JS_LIBRARIES = CONFIG['FILE_MAP']

CITY_NAME_PINYIN_MAP = CONFIG['PINYIN_MAP']

PY2 = sys.version_info[0] == 2

JS_PATTERN = re.compile(r'<!-- build -->(.*)<!-- endbuild -->',
Expand Down Expand Up @@ -107,7 +87,7 @@ def produce_require_configuration(dependencies, jshost):
require_conf_items = [
"'%s': '%s/%s'" % (key,
jshost,
DEFAULT_JS_LIBRARIES.get(key, key))
constants.DEFAULT_JS_LIBRARIES.get(key, key))
for key in _d]
require_libraries = ["'%s'" % key for key in _d]
return dict(
Expand All @@ -124,7 +104,7 @@ def produce_html_script_list(dependencies):
"""
_d = ensure_echarts_is_in_the_front(dependencies)
script_list = [
'%s' % DEFAULT_JS_LIBRARIES.get(key, key)
'%s' % constants.DEFAULT_JS_LIBRARIES.get(key, key)
for key in _d]
return script_list

Expand Down
12 changes: 12 additions & 0 deletions pyecharts/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os


def get_resource_dir(folder):
"""
:param folder:
:return:
"""
current_path = os.path.dirname(__file__)
resource_path = os.path.join(current_path, folder)
return resource_path
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cd test
nosetests --with-coverage --cover-package pyecharts --cover-package test
mv .coverage ../

2 changes: 1 addition & 1 deletion test/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import unicode_literals

import pyecharts.template as constants
import pyecharts.constants as constants
from nose.tools import eq_


Expand Down

0 comments on commit 5c48ebe

Please sign in to comment.