-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlossaryTermLinks.py
211 lines (177 loc) · 6.82 KB
/
GlossaryTermLinks.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python
"""Report of documents linking to a specified glossary term.
"""
from functools import cached_property
from cdrcgi import Controller, Reporter
from cdrapi.docs import Doc
class Control(Controller):
"""Report logic."""
SUBTITLE = "Glossary Term Links QC Report"
NAME_PATH = "/GlossaryTermName/TermName/TermNameString"
SOURCE_PATH = "/GlossaryTermName/TermName/TermNameSource"
COLUMNS = (
Reporter.Column("Doc ID", classes="doc-id"),
Reporter.Column("Doc Title", classes="doc-title"),
Reporter.Column("Element Name", classes="element-name"),
Reporter.Column("Fragment ID", classes="frag-id"),
)
def populate_form(self, page):
"""Add the two fields to the form for selecting a term name."""
fieldset = page.fieldset("Select a Glossary Term by Name or ID")
fieldset.append(page.text_field("id", label="CDR ID"))
fieldset.append(page.text_field("name", label="Term Name"))
page.form.append(fieldset)
@cached_property
def report(self):
"""Insert a custom table at the top of the report."""
tables = []
for doctype in sorted(self.types):
rows = []
for linker in self.types[doctype]:
rows += linker.rows
opts = dict(cols=self.COLUMNS, caption=doctype, classes="linkers")
tables.append(self.Reporter.Table(rows, **opts))
opts = dict(
banner=self.title,
footer=self.footer,
subtitle=self.subtitle,
page_opts=dict(session=self.session),
)
report = self.Reporter(self.title, tables, **opts)
B = report.page.B
table = B.TABLE(
B.CAPTION("Glossary Term"),
B.TR(B.TH("Name"), B.TD(self.name)),
B.TR(B.TH("Source"), B.TD(self.source)),
B.CLASS("usa-table usa-table--borderless"),
id="top-table"
)
h1 = report.page.main.find("div/h1")
h1.addnext(table)
table.addnext(B.H2("Documents Linked to Term Name"))
css = (
"#top-table th { font-weight: bold; text-align: right; }",
"form table { width: 100%; }",
)
report.page.add_css("\n".join(css))
return report
@property
def types(self):
"""Dictionary of linking document lists, indexed by document type."""
if not hasattr(self, "_types"):
fields = "d.id", "t.name"
query = self.Query("document d", *fields).unique().order("d.id")
query.join("doc_type t", "t.id = d.doc_type")
query.join("query_term l", "l.doc_id = d.id")
query.where(query.Condition("l.int_val", self.id))
self._types = {}
for row in query.execute(self.cursor).fetchall():
doc = LinkingDoc(self, row.id)
if row.name not in self._types:
self._types[row.name] = [doc]
else:
self._types[row.name].append(doc)
return self._types
@property
def cdr_id(self):
"""String version if the glossary term's CDR ID."""
if not hasattr(self, "_cdr_id"):
self._cdr_id = f"CDR{self.id:010d}"
return self._cdr_id
@property
def id(self):
"""Integer for the glossary term name document ID."""
if not hasattr(self, "_id"):
id = self.fields.getvalue("id")
if id:
try:
self._id = Doc.extract_id(id)
except Exception:
self.bail(f"Invalid id: {id!r}")
else:
name = self.fields.getvalue("name")
if not name:
self.show_form()
query = self.Query("query_term", "doc_id").unique()
query.where(f"path = '{self.NAME_PATH}'")
query.where(query.Condition("value", name))
rows = query.execute(self.cursor).fetchall()
if len(rows) > 1:
self.bail(f"Ambiguous term name: {name!r}")
elif not rows:
self.bail(f"Unknown term {name!r}")
self._id = rows[0].doc_id
return self._id
@property
def name(self):
"""String for the glossary term name."""
if not hasattr(self, "_name"):
query = self.Query("query_term", "value")
query.where(f"path = '{self.NAME_PATH}'")
query.where(query.Condition("doc_id", self.id))
rows = query.execute(self.cursor).fetchall()
self._name = rows[0].value
return self._name
@property
def source(self):
"""Source for the term name, if any."""
if not hasattr(self, "_source"):
query = self.Query("query_term", "value")
query.where(f"path = '{self.SOURCE_PATH}'")
query.where(query.Condition("doc_id", self.id))
rows = query.execute(self.cursor).fetchall()
self._source = rows[0].value if rows else None
return self._source
@property
def xpath(self):
"""Search path for finding links to this document."""
if not hasattr(self, "_xpath"):
tests = (
f"@cdr:ref='{self.cdr_id}'",
f"@cdr:href='{self.cdr_id}'",
)
self._xpath = f"//*[{' or '.join(tests)}]"
return self._xpath
class LinkingDoc:
"""Document linking to our glossary term name."""
XPATH_OPTS = dict(namespaces=Doc.NSMAP)
CDR_ID = f"{{{Doc.NS}}}id"
def __init__(self, control, id):
"""Capture the caller's values."""
self.__control = control
self.__id = id
@property
def id(self):
"""CDR ID for the linking document."""
return self.__id
@property
def control(self):
"""Access to information about the linked glossary term name."""
return self.__control
@property
def doc(self):
if not hasattr(self, "_doc"):
self._doc = Doc(self.control.session, id=self.id)
return self._doc
@property
def rows(self):
"""Table rows for the report."""
if not hasattr(self, "_rows"):
rows = []
root = self.doc.root
for node in root.xpath(self.control.xpath, **self.XPATH_OPTS):
parent = node.getparent()
tag = parent.tag
node_id = str(parent.get(self.CDR_ID))
row = (
Reporter.Cell(self.doc.cdr_id, center=True),
self.doc.title,
tag,
Reporter.Cell(node_id, center=True),
)
rows.append(row)
self._rows = rows
return self._rows
if __name__ == "__main__":
"""Don't execute the script if loaded as a module."""
Control().run()