forked from echarts-maps/echarts-china-cities-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakedemo.py
119 lines (104 loc) · 4.21 KB
/
makedemo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# coding=utf-8
from __future__ import unicode_literals
import os
import glob
import json
import pinyin
from collections import defaultdict
from jinja2 import Environment, FileSystemLoader
import codecs
from collections import OrderedDict
DEST_FOLDER = 'echarts-china-cities-js'
REGISTRY_FILE = 'registry.json'
MANUAL_FIX = {
"莆田": "pu3_tian2"
}
def list_base(src_folder):
for folder in glob.glob(src_folder):
if os.path.isdir(folder):
yield folder
def list_a_directory(src_folder):
for f in glob.glob(src_folder):
file_name = os.path.basename(f).split('.')[0]
if '市' in file_name:
file_name = file_name[:-1]
if file_name in MANUAL_FIX:
pinyin_name = MANUAL_FIX[file_name]
else:
pinyin_name = pinyin.get(
file_name, format="numerical", delimiter="_")
yield f, file_name, pinyin_name
def minify_js(src_js, min_js):
os.system('./node_modules/.bin/minify -o %s %s' % (min_js, src_js))
if __name__ == '__main__':
name_dict = {}
raw_rendering_dict = defaultdict(list)
for folder in list_base('src/*'):
cfolder = os.path.basename(folder)
pfolder = pinyin.get(cfolder, format="numerical", delimiter="_")
_dest_folder = DEST_FOLDER
name_dict[cfolder] = pfolder
if not os.path.exists(_dest_folder):
os.mkdir(_dest_folder)
all_files = list_a_directory(os.path.join(folder, "*.js"))
for src_file, cname, pname in all_files:
_dest_file = os.path.join(_dest_folder,
"%s_%s.js" % (pfolder, pname))
print("%s-> %s, %s -> %s" % (cname, pname, src_file, _dest_file))
minify_js(src_file, _dest_file)
raw_rendering_dict[cfolder].append((cname, pname))
# adding direct cities
cnames = ['北京', '澳门', '重庆', '上海', '天津', '香港']
cities = ['beijing', 'aomen', 'chongqing', 'shanghai',
'tianjin', 'xianggang']
for cname, pname in zip(cnames, cities):
src_file = os.path.join('node_modules',
'echarts',
'map',
'js',
'province',
'%s.js' % pname)
_dest_file = os.path.join('echarts-china-cities-js', '%s.js' % pname)
minify_js(src_file, _dest_file)
print("%s-> %s, %s -> %s" % (cname, pname, src_file, _dest_file))
raw_rendering_dict['直辖市'].append((cname, pname))
# statistics
count = 0
rendering_dict = OrderedDict()
sorted_provinces = sorted(raw_rendering_dict.keys(),
key=lambda x: pinyin.get(x, format='numerical'))
for cprovince in sorted_provinces:
count += len(raw_rendering_dict[cprovince])
rendering_dict[cprovince] = sorted(
raw_rendering_dict[cprovince], key=lambda x: x[1])
provinces, cities = len(rendering_dict.keys()), count
jinja2_env = Environment(
loader=FileSystemLoader('./templates'),
keep_trailing_newline=True,
trim_blocks=True,
lstrip_blocks=True)
template = jinja2_env.get_template('index.html')
html = template.render(names=name_dict, registry=rendering_dict,
num_cities=cities)
with codecs.open('preview.html', 'wb', 'utf-8') as f:
f.write(html)
config = jinja2_env.get_template('config.json')
config_json = config.render(names=name_dict, registry=rendering_dict)
registry_file = REGISTRY_FILE
with codecs.open(registry_file, 'w', 'utf-8') as f:
f.write(config_json)
readme = jinja2_env.get_template('README.md')
readme_txt = readme.render(
names=name_dict, registry=rendering_dict,
num_provinces=provinces, num_cities=cities
)
with codecs.open('README.md', 'wb', 'utf-8') as f:
f.write(readme_txt)
# custom data structure
external = defaultdict(list)
for key, value in raw_rendering_dict.items():
if key != '直辖市':
for city in value:
external[key].append(city[0])
with codecs.open('structure.json', 'wb', 'utf-8') as f:
json.dump(external, f)