This code produces HTML database descriptions from create statements.
Feed into stdin, await from stdout.
For example: python dbdesc.py < DATASCHEME > dbdesc.html
It isn’t perfect nor meant to be, but gives a good starting point for a documentation.
#!/usr/bin/python
import sys
import re
a = ''
for i in sys.stdin.readlines():
a = a + " " + re.sub('--.*', '', i).strip()
a = a.strip().lower()
a = re.sub('\/\*[^*]*\*\/', '', a)
print """
<html>
<head>
<link rel="stylesheet" href="dbdesc.css" />
</head>
<body>
"""
for m in re.findall('create table ([^ (]*)[ (]*([^;]*)[ )]*;', a):
tablename = m[0]
content = m[1]
print "<h2>%s</h2>" % tablename
print """<table class="dbdesc"><thead><tr>
<th class="name">Feldname</th>
<th class="type">Typ</th>
<th class="option">Option</th>
<th class="comment">Bemerkung</th>
</tr></thead>"""
atts = {}
for l in content.split(','):
l = l.strip()
if l == '':
continue
if l.startswith('constraint') or l.startswith('foreign key'):
continue
l = l.split(None, 2)
if len(l)<2:
continue
name = l[0]
if name == 'constraint' or name.__contains__('(') or name.__contains__(')'):
continue
type = l[1]
if len(l)<3:
l.append('')
op = ""
if 'not null' in l[2]: op = op + '!'
if 'primary key' in l[2]: op = op + '1+P'
elif 'unique' in l[2]: op = op + '1'
print """ <tr>
<td class="name">%s</td>
<td class="type">%s</td>
<td class="option">%s</td>
<td class="comment"><!-- TODO: Bemerkungen --></td>
</tr>""" % (name, type, op)
print """ </table> """
print """</html> """
CSS file dbdesc.css
table.dbdesc{ width: 40em; border: 1px solid #080; border-width: 1px 0;}
table.dbdesc th{ border-bottom: 1px solid #080;}
table.dbdesc td{ border: none;}
table.dbdesc td, table.dbdesc th{ width: 15em;}
table.dbdesc th{ font-style: italic; font-weight: normal; }
table.dbdesc td.option{ text-align: center;}
table.dbdesc td.type{ text-transform: uppercase;}
Recent Comments