2016-03-03 13:51:36 +08:00
|
|
|
#!/usr/bin/env python
|
2016-03-01 09:32:14 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import re
|
2016-03-03 13:51:36 +08:00
|
|
|
from io import StringIO
|
2016-03-01 09:32:14 +08:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
def _raise_err(format, *args) :
|
|
|
|
raise ValueError(format % args)
|
|
|
|
|
|
|
|
|
|
|
|
def _load_yaml(yaml_file) :
|
2016-03-03 13:51:36 +08:00
|
|
|
print(u'load: ' + yaml_file)
|
|
|
|
with open(yaml_file, u'r') as f :
|
2016-03-01 09:32:14 +08:00
|
|
|
return yaml.load(f.read())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Node :
|
|
|
|
all = {}
|
|
|
|
keys = []
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def init(cls) :
|
|
|
|
Node.all = {}
|
|
|
|
Node.keys = []
|
|
|
|
|
2016-03-03 13:51:36 +08:00
|
|
|
for node_type in [u'person', u'company'] :
|
|
|
|
for node_id in os.listdir(u'../data/' + node_type) :
|
|
|
|
yaml_file = u'../data/%s/%s/brief.yaml' % (node_type, node_id)
|
2016-03-01 09:32:14 +08:00
|
|
|
node = Node(_load_yaml(yaml_file), node_id, node_type)
|
|
|
|
if node_id in Node.all :
|
|
|
|
_raise_err(u'Node id conflict: "%s"!', node_id)
|
|
|
|
|
|
|
|
Node.all[node_id] = node
|
|
|
|
Node.keys.append(node_id)
|
2016-03-03 13:51:36 +08:00
|
|
|
print(u'Node number: %d' % len(Node.all))
|
2016-03-01 09:32:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, yaml, node_id, type) :
|
|
|
|
self.id = node_id
|
|
|
|
self.type = type
|
2016-03-03 13:51:36 +08:00
|
|
|
self.name = yaml[u'name']
|
|
|
|
if u'other_names' in yaml : # person
|
|
|
|
self.other_names = yaml[u'other_names']
|
|
|
|
if u'sex' in yaml : # person
|
|
|
|
self.sex = yaml[u'sex']
|
|
|
|
if u'full_name' in yaml : # company
|
|
|
|
self.full_name = yaml[u'full_name']
|
|
|
|
self.birth = yaml[u'birth']
|
|
|
|
self.death = yaml[u'death']
|
|
|
|
self.desc = yaml[u'desc']
|
|
|
|
self.links = yaml[u'links']
|
2016-03-01 09:32:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Relation :
|
|
|
|
all = {}
|
|
|
|
keys = []
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def init(cls) :
|
2016-03-03 13:51:36 +08:00
|
|
|
for family_id in os.listdir(u'../data/family/') :
|
|
|
|
family_id = family_id.replace(u'.yaml', u'')
|
|
|
|
yaml_file = u'../data/family/%s.yaml' % (family_id,)
|
2016-03-01 09:32:14 +08:00
|
|
|
if family_id not in Node.all :
|
|
|
|
_raise_err(u'Invalid family name: "%s"!', family_id)
|
|
|
|
|
|
|
|
yaml = _load_yaml(yaml_file)
|
2016-03-03 13:51:36 +08:00
|
|
|
for lst in yaml[u'relations'] :
|
2016-03-01 09:32:14 +08:00
|
|
|
relation = Relation(lst)
|
|
|
|
Relation.all[relation.name] = relation
|
|
|
|
Relation.keys.append(relation.name)
|
2016-03-03 13:51:36 +08:00
|
|
|
print(u'Relation number: %d' % len(Relation.all))
|
2016-03-01 09:32:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, lst) :
|
|
|
|
self.node_from = lst[0]
|
|
|
|
self.node_to = lst[1]
|
|
|
|
self.desc = lst[2]
|
2016-03-03 13:51:36 +08:00
|
|
|
self.name = self.node_from + u'->' + self.node_to
|
2016-03-01 09:32:14 +08:00
|
|
|
|
|
|
|
if self.name in Relation.all :
|
|
|
|
_raise_err(u'Relation name conflict: "%s"!', self.name)
|
|
|
|
if self.node_from not in Node.all :
|
|
|
|
_raise_err(u'Invalid relation "from" attr: "%s"!', self.node_from)
|
|
|
|
if self.node_to not in Node.all :
|
|
|
|
_raise_err(u'Invalid relation "to" attr": "%s"!', self.node_to)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Family :
|
|
|
|
all = {}
|
|
|
|
keys = []
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def init(cls) :
|
2016-03-03 13:51:36 +08:00
|
|
|
for family_id in os.listdir(u'../data/family/') :
|
|
|
|
family_id = family_id.replace(u'.yaml', u'')
|
|
|
|
yaml_file = u'../data/family/%s.yaml' % (family_id,)
|
2016-03-01 09:32:14 +08:00
|
|
|
if family_id not in Node.all :
|
|
|
|
_raise_err(u'Invalid family name: "%s"!', family_id)
|
|
|
|
|
|
|
|
family = Family(_load_yaml(yaml_file))
|
|
|
|
Family.all[family_id] = family
|
|
|
|
Family.keys.append(family_id)
|
2016-03-03 13:51:36 +08:00
|
|
|
print(u'Family number: %d' % len(Family.all))
|
2016-03-01 09:32:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, yaml) :
|
2016-03-03 13:51:36 +08:00
|
|
|
self.name = yaml[u'name']
|
|
|
|
self.inner = yaml[u'inner']
|
|
|
|
self.outer = yaml[u'outer']
|
2016-03-01 09:32:14 +08:00
|
|
|
self.members = [self.name] + self.inner + self.outer
|
|
|
|
|
|
|
|
for name in self.members :
|
|
|
|
if name not in Node.all :
|
|
|
|
_raise_err(u'Invalid family members: "%s"!', name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Graph :
|
|
|
|
def __init__(self, yaml) :
|
2016-03-03 13:51:36 +08:00
|
|
|
self._name = yaml[u'name']
|
|
|
|
self._families = yaml[u'families']
|
2016-03-01 09:32:14 +08:00
|
|
|
self._families.reverse()
|
|
|
|
self._nodes = []
|
|
|
|
self._relations = []
|
|
|
|
for f in self._families :
|
|
|
|
family = Family.all[f]
|
|
|
|
for n in family.members :
|
|
|
|
if n not in self._nodes :
|
|
|
|
self._nodes.append(n)
|
|
|
|
for r in Relation.keys :
|
|
|
|
relation = Relation.all[r]
|
|
|
|
if relation.node_from in family.members \
|
|
|
|
and relation.node_to in family.members \
|
|
|
|
and r not in self._relations :
|
|
|
|
self._relations.append(r)
|
|
|
|
|
|
|
|
|
2016-03-03 13:51:36 +08:00
|
|
|
def dump(self) :
|
2016-03-01 09:32:14 +08:00
|
|
|
output = StringIO()
|
|
|
|
|
|
|
|
for n in self._nodes :
|
|
|
|
output.write(self._dot_node(n))
|
2016-03-03 13:51:36 +08:00
|
|
|
output.write(u'\n')
|
2016-03-01 09:32:14 +08:00
|
|
|
|
|
|
|
for r in self._relations :
|
|
|
|
output.write(self._dot_relation(r))
|
2016-03-03 13:51:36 +08:00
|
|
|
output.write(u'\n')
|
2016-03-01 09:32:14 +08:00
|
|
|
|
|
|
|
if len(self._families) > 1 :
|
|
|
|
for f in self._families :
|
|
|
|
output.write(self._dot_sub_graph(f))
|
|
|
|
|
|
|
|
template = u'''
|
|
|
|
digraph %s
|
|
|
|
{
|
|
|
|
\trankdir = "LR";
|
|
|
|
\tranksep = 0.5;
|
|
|
|
\tlabel = "%s";
|
|
|
|
\tlabelloc = "t";
|
|
|
|
\tfontsize = "24";
|
|
|
|
\tfontname = "SimHei";
|
|
|
|
|
|
|
|
\tgraph [style="filled", color="lightgrey"];
|
|
|
|
\tnode [fontname="SimSun"];
|
|
|
|
\tedge [fontname="SimSun"];
|
|
|
|
|
|
|
|
%s
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
return template % (self._name, self._name, output.getvalue())
|
|
|
|
|
|
|
|
|
|
|
|
def _node_color(self, node) :
|
|
|
|
if u'company' == node.type :
|
|
|
|
return u'green'
|
|
|
|
else :
|
|
|
|
return (u'blue' if u'M'==node.sex else u'red')
|
|
|
|
|
2016-03-01 11:35:52 +08:00
|
|
|
def _other_names(self, node) :
|
|
|
|
other_names = ''
|
|
|
|
if u'person'==node.type and node.other_names :
|
2016-03-03 13:51:36 +08:00
|
|
|
other_names = u', '.join([u'%s:%s' % (k,v) for k,v in node.other_names.items()])
|
2016-03-01 11:35:52 +08:00
|
|
|
elif u'company'==node.type and node.full_name :
|
|
|
|
other_names = node.full_name
|
2016-03-03 13:51:36 +08:00
|
|
|
return u'<tr><td>(%s)</td></tr>' % (other_names,) if other_names else ''
|
2016-03-01 11:35:52 +08:00
|
|
|
|
2016-03-01 09:32:14 +08:00
|
|
|
def _dot_node(self, node_id) :
|
|
|
|
node = Node.all[node_id]
|
2016-03-03 13:51:36 +08:00
|
|
|
template = u'\t%s [shape="%s", color="%s", ' \
|
|
|
|
u'label=<<table border="0" cellborder="0">' \
|
|
|
|
u'<tr><td>%s%s</td></tr>' \
|
|
|
|
u'%s' \
|
|
|
|
u'<tr><td>%s</td></tr>' \
|
|
|
|
u'<tr><td>%s</td></tr></table>>];\n'
|
2016-03-01 09:32:14 +08:00
|
|
|
|
|
|
|
portrait = u'../data/person/%s/portrait.png' % (node_id,)
|
2016-03-03 13:51:36 +08:00
|
|
|
portrait = u'<img src="%s"/>' % (portrait,) if os.path.exists(portrait) else ''
|
2016-03-01 09:32:14 +08:00
|
|
|
|
2016-03-01 11:35:52 +08:00
|
|
|
return template % (node.id,
|
2016-03-01 09:32:14 +08:00
|
|
|
u'box' if u'person'==node.type else u'ellipse',
|
|
|
|
self._node_color(node),
|
|
|
|
node.name,
|
|
|
|
(u'' if node.birth==u'N/A' else u' [%s]'%node.birth),
|
2016-03-01 11:35:52 +08:00
|
|
|
self._other_names(node),
|
2016-03-01 09:32:14 +08:00
|
|
|
portrait,
|
|
|
|
node.desc.replace(u'\n', u'<br/>'))
|
|
|
|
|
|
|
|
|
|
|
|
def _dot_relation(self, name) :
|
|
|
|
relation = Relation.all[name]
|
|
|
|
template = u'''\t%s -> %s [label="%s", style=%s, color="%s"];\n'''
|
|
|
|
|
2016-03-03 13:51:36 +08:00
|
|
|
if re.match(u'^夫|妻$', relation.desc) :
|
2016-03-01 09:32:14 +08:00
|
|
|
style = u'bold'
|
2016-03-03 13:51:36 +08:00
|
|
|
elif re.match(u'^父|母$', relation.desc) :
|
2016-03-01 09:32:14 +08:00
|
|
|
style = u'solid'
|
2016-03-03 13:51:36 +08:00
|
|
|
elif re.match(u'^(独|长|次|三|四|五|六|七)?(子|女)$', relation.desc) :
|
2016-03-01 09:32:14 +08:00
|
|
|
style = u'solid'
|
2016-03-03 13:51:36 +08:00
|
|
|
elif re.match(u'^.*?(兄|弟|姐|妹)$', relation.desc) :
|
2016-03-01 09:32:14 +08:00
|
|
|
style = u'dashed'
|
|
|
|
else :
|
|
|
|
style = u'dotted'
|
|
|
|
|
|
|
|
return template % (relation.node_from, relation.node_to,
|
|
|
|
relation.desc, style,
|
|
|
|
self._node_color(Node.all[relation.node_to]))
|
|
|
|
|
|
|
|
|
|
|
|
def _dot_sub_graph(self, name) :
|
|
|
|
node = Node.all[name]
|
|
|
|
if node.type == u'company' :
|
|
|
|
return self._dot_node(name)
|
|
|
|
|
|
|
|
family = Family.all[name]
|
|
|
|
template = u'''
|
|
|
|
\tsubgraph "cluster_%s"
|
|
|
|
\t{
|
|
|
|
\t\tfontsize="18";
|
|
|
|
\t\tlabel="%s家族";
|
|
|
|
\t\t%s;
|
|
|
|
\t}
|
|
|
|
'''
|
|
|
|
return template % (family.name, family.name,
|
2016-03-03 13:51:36 +08:00
|
|
|
u';'.join([name]+family.inner))
|
2016-03-01 09:32:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Builder :
|
|
|
|
def __init__(self) :
|
|
|
|
Node.init()
|
|
|
|
Relation.init()
|
|
|
|
Family.init()
|
|
|
|
|
|
|
|
def _mkdir(self, name) :
|
|
|
|
if os.path.exists(name) :
|
|
|
|
shutil.rmtree(name)
|
|
|
|
os.mkdir(name)
|
|
|
|
|
2016-03-03 13:51:36 +08:00
|
|
|
def _exec(self, cmd) :
|
|
|
|
print(cmd)
|
|
|
|
return os.system(cmd.encode(u'utf-8'))
|
2016-03-01 09:32:14 +08:00
|
|
|
|
|
|
|
def do(self, file_type) :
|
2016-03-03 13:51:36 +08:00
|
|
|
os.chdir(u'../download/')
|
|
|
|
self._mkdir(u'dot')
|
2016-03-01 09:32:14 +08:00
|
|
|
self._mkdir(file_type)
|
|
|
|
n = 0
|
2016-03-03 13:51:36 +08:00
|
|
|
for graph in _load_yaml(u'../data/graph.yaml') :
|
2016-03-01 09:32:14 +08:00
|
|
|
n += 1
|
2016-03-03 13:51:36 +08:00
|
|
|
name = u'%02d-%s' % (n, graph[u'name'])
|
|
|
|
dot_file = u'./dot/%s.dot' % (name,)
|
|
|
|
output_file = u'./%s/%s.%s' % (file_type, name, file_type)
|
2016-03-01 09:32:14 +08:00
|
|
|
|
2016-03-03 13:51:36 +08:00
|
|
|
with open(dot_file, u'wb') as f :
|
|
|
|
f.write(Graph(graph).dump().encode(u'utf-8'))
|
2016-03-01 09:32:14 +08:00
|
|
|
|
2016-03-03 13:51:36 +08:00
|
|
|
cmd = u'dot "%s" -T%s -o"%s"' % (dot_file, file_type, output_file)
|
|
|
|
if self._exec(cmd) != 0 :
|
|
|
|
_raise_err(u'Make "%s" failed!', dot_file)
|
2016-03-01 09:32:14 +08:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if '__main__' == __name__ :
|
|
|
|
try :
|
|
|
|
if len(sys.argv) != 2 :
|
2016-03-03 13:51:36 +08:00
|
|
|
print(u'''Usage:\n%s file_type
|
2016-03-01 09:32:14 +08:00
|
|
|
(file_type is pdf or jpg or png or gif or tiff or svg or ps)''' % sys.argv[0])
|
|
|
|
sys.exit(0)
|
2016-03-03 13:51:36 +08:00
|
|
|
sys.exit(Builder().do(sys.argv[1]))
|
2016-03-01 09:32:14 +08:00
|
|
|
except Exception as err :
|
2016-03-03 13:51:36 +08:00
|
|
|
print(u'Make abort!\n%s' % err)
|
2016-03-01 09:32:14 +08:00
|
|
|
sys.exit(1)
|