-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextract_tables.py
116 lines (98 loc) · 3.43 KB
/
extract_tables.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
#!/usr/bin/env python3
"""Extract terminal block connections from QElectroTech SQLite database."""
import sqlite3
from .html_table import TableGeneratorHTML
def query_blocks():
"""Generate SQL query string for block summary."""
return '''
SELECT
substr("label", 0, instr("label", ':')) AS "tblock",
MAX(substr("label", instr("label", ':')+1)+0) AS "maxtnum",
COUNT(*) AS "mentions",
group_concat("folio" || '-' || "position", ', ') as "fpositions"
FROM "element_nomenclature_view" AS "t"
WHERE "element_type" = 'terminal' AND "label" LIKE '%:%'
GROUP BY "tblock"
ORDER BY "tblock" ASC
'''
def query_terminals(add_where=''):
"""Generate SQL query string for terminal mentions."""
return f'''
SELECT
substr("label", 0, instr("label", ':')) AS "tblock",
substr("label", instr("label", ':')+1)+0 AS "tnum",
( SELECT COUNT(*) FROM "element_nomenclature_view" AS "ct"
WHERE "ct"."label" = "t"."label" ) AS "mentions",
group_concat("folio" || '-' || "position", ', ') as "fpositions"
FROM "element_nomenclature_view" AS "t"
WHERE "element_type" = 'terminal' AND "label" LIKE '%:%' {add_where}
GROUP BY "label"
ORDER BY "tblock" ASC, "tnum" ASC
'''
def process_db(dbfile, header=None, footer=None, indent=0):
"""Generate HTML files from the given SQLite database."""
con = sqlite3.connect(dbfile)
blocks = con.cursor()
blocks.execute(query_blocks())
table_attr = 'border="0" cellspacing="0" cellpadding="0"'
for tblock, maxtnum, mentions, fpositions in blocks:
with open(f'terminals_{tblock}.html', 'wt') as outfile:
if header:
outfile.write(header)
with TableGeneratorHTML(outfile, table_attr, indent) as table:
table.writeheader((f'Block {tblock}',),
cell_attr='colspan="3"')
terminals = con.cursor()
terminals.execute(query_terminals(f"AND tblock='{tblock}'"))
used, rows = 0, 0
for term_block, tnum, term_mentions, fpositions in terminals:
used += 1
rows += 1
while rows < tnum:
# Insert blank rows for never mentioned terminals
table.writerow((rows, '(0)', ' '),
classes=('tnum', 'mentions', 'positions'))
rows += 1
table.writerow((tnum, f'({term_mentions})', fpositions),
classes=('tnum', 'mentions', 'positions'))
print(f'{outfile.name}: {mentions=} {maxtnum=} {rows=} {used=}')
if footer:
outfile.write(footer)
con.close()
def main(dbfile, wrap=False, css_add=''):
"""Run HTML generator with default output adjustments."""
footer = ''
indent = 0
# Mandatory style settings for grid alignment
css = '''\
td { line-height: 20px; }
th { line-height: 12px; }
'''
css += css_add
if not wrap:
header = f'''\
<style type="text/css">
{css}</style>
'''
else:
header = '''\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
'''
if css:
header += f'''\
<style type="text/css">
{css}
</style>
'''
header += '''\
</head>
<body>
'''
indent = 4
footer = '''\
</body>
</html>
'''
return process_db(dbfile, header, footer, indent)