forked from yodamaster/awesome-ancient-chinese-books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
64 lines (47 loc) · 1.46 KB
/
util.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import os
import yaml
from collections import OrderedDict
def load(stream):
class Loader(yaml.CSafeLoader):
pass
def constructor(loader,node):
return OrderedDict(loader.construct_pairs(node))
Loader.add_constructor(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
constructor)
return yaml.load(stream, Loader=Loader)
def dump(data, stream=None):
class Dumper(yaml.CSafeDumper):
pass
def representer(dumper, data):
return dumper.represent_dict(data.iteritems())
Dumper.add_representer(OrderedDict, representer)
return yaml.dump(data, stream, Dumper=Dumper, encoding='utf-8', default_flow_style=False, allow_unicode=True)
def loadf(filename):
print 'load', filename
with open(filename,'r') as f:
return load(f)
def dumpf(data, filename):
print 'save', filename
with open(filename,'w') as f:
return dump(data, f)
DIRS = u"經史子集"
def load_all():
return {
x:
{y[:-5]: loadf(x+u"/"+y)
for y in os.listdir(x)
if y.endswith(u".yaml")}
for x in DIRS}
def save_all(data):
for x, d in data.iteritems():
for y, o in d.iteritems():
dumpf(o,x+u"/"+y+u".yaml")
def index_by_title(data):
return {o[u"題目"]: o
for _,x in data.iteritems()
for _,y in x.iteritems()
for _,z in y.iteritems()
for o in z}