Mercurial > p > roundup > code
annotate scripts/schema_diagram.py @ 5395:23b8e6067f7c
Python 3 preparation: update calls to dict methods.
Tool-assisted patch. Changes of iterkeys / itervalues / iteritems to
keys / values / items are fully automated, but may make things less
efficient in Python 2. Automated tools want to add list() around many
calls to keys / values / items, but I thought most such list()
additions were unnecessary because it seemed the result of keys /
values / items was just iterated over while the set of dict keys
remained unchanged, rather than used in a way requiring an actual
list, or used while the set of keys in the dict could change. It's
quite possible I missed some cases where list() was really needed, or
left in some unnecessary list() calls.
In cases where list() was only needed because the resulting list was
then sorted in-place, I changed the code to use calls to sorted().
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Tue, 24 Jul 2018 23:04:42 +0000 |
| parents | 64b05e24dbd8 |
| children | 9c3ec0a5c7fc |
| rev | line source |
|---|---|
| 1166 | 1 #! /usr/bin/python |
| 2 # | |
| 3 # Schema diagram generator contributed by Stefan Seefeld of the fresco | |
| 4 # project http://www.fresco.org/. | |
| 5 # | |
| 6 # It generates a 'dot file' that is then fed into the 'dot' | |
| 7 # tool (http://www.graphviz.org) to generate a graph: | |
| 8 # | |
| 9 # %> ./schema.py | |
| 10 # %> dot -Tps schema.dot -o schema.ps | |
| 11 # %> gv schema.ps | |
| 12 # | |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
1166
diff
changeset
|
13 from __future__ import print_function |
| 1166 | 14 import sys |
| 15 import roundup.instance | |
| 16 | |
| 17 # open the instance | |
| 18 instance = roundup.instance.open(sys.argv[1]) | |
| 19 db = instance.open() | |
| 20 | |
| 21 # diagram preamble | |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
1166
diff
changeset
|
22 print('digraph schema {') |
|
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
1166
diff
changeset
|
23 print('size="8,6"') |
|
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
1166
diff
changeset
|
24 print('node [shape="record" bgcolor="#ffe4c4" style=filled]') |
|
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
1166
diff
changeset
|
25 print('edge [taillabel="1" headlabel="1" dir=back arrowtail=ediamond]') |
| 1166 | 26 |
| 27 # get all the classes | |
|
5395
23b8e6067f7c
Python 3 preparation: update calls to dict methods.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
28 types = list(db.classes.keys()) |
| 1166 | 29 |
| 30 # one record node per class | |
| 31 for i in range(len(types)): | |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
1166
diff
changeset
|
32 print('node%d [label=\"{%s|}"]'%(i, types[i])) |
| 1166 | 33 |
| 34 # now draw in the relations | |
| 35 for name in db.classes.keys(): | |
| 36 type = db.classes[name] | |
| 37 attributes = type.getprops() | |
| 38 for a in attributes.keys(): | |
| 39 attribute = attributes[a] | |
| 40 if isinstance(attribute, roundup.hyperdb.Link): | |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
1166
diff
changeset
|
41 print('node%d -> node%d [label=%s]'%(types.index(name), |
| 1166 | 42 types.index(attribute.classname), |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
1166
diff
changeset
|
43 a)) |
| 1166 | 44 elif isinstance(attribute, roundup.hyperdb.Multilink): |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
1166
diff
changeset
|
45 print('node%d -> node%d [taillabel="*" label=%s]'%(types.index(name), |
| 1166 | 46 types.index(attribute.classname), |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
1166
diff
changeset
|
47 a)) |
| 1166 | 48 # all done |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
1166
diff
changeset
|
49 print('}') |
