sdb-reader 0.9.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sdb_reader/embedded.py ADDED
@@ -0,0 +1,250 @@
1
+ """The eSDScom data a supplier embedded in the PDF, and where the sheet read from
2
+ the printed text differs from it (SDW-11).
3
+
4
+ eSDScom is the XML exchange format for safety data sheets; an authoring system
5
+ can attach its export to the PDF it prints. That export is the supplier's own
6
+ statement of the sheet's fields, so it is a second reading that owes nothing to
7
+ this parser. It is compared, never used: what counts is the printed text, and
8
+ the exports seen so far leave out the classification as often as not.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import re
14
+ import xml.etree.ElementTree as ET
15
+ from dataclasses import dataclass
16
+ from pathlib import Path
17
+
18
+ import pymupdf
19
+
20
+ from .messages import Message
21
+ from .schema import SafetyDataSheet
22
+
23
+ # The root element of an eSDScom file, with or without the standard's namespace:
24
+ # CHEMDOX writes none.
25
+ _ROOT = "DatasheetFeed"
26
+
27
+ # A document type declaration is how an XML file defines entities, and an
28
+ # entity is how a small file expands into a huge one. eSDScom has none.
29
+ _DOCTYPE = re.compile(rb"<!\s*(?:DOCTYPE|ENTITY)", re.IGNORECASE)
30
+
31
+ # Larger than any export seen (160 kB); an attachment beyond it is something
32
+ # else or an attempt.
33
+ _MAX_BYTES = 20 * 1024 * 1024
34
+
35
+ _SIGNAL_WORDS = {"DGR": "Gefahr", "WNG": "Achtung"}
36
+
37
+ _EUH_IN_TEXT = re.compile(r"\bEUH\d{3}[A-Z]?\b")
38
+
39
+
40
+ @dataclass(frozen=True)
41
+ class EmbeddedSheet:
42
+ """The fields of an embedded eSDScom file that are compared."""
43
+
44
+ format_version: str | None = None
45
+ authoring_system: str | None = None
46
+ # As the file writes it, an ISO date: "2023-09-19".
47
+ date: str | None = None
48
+ signal_word: str | None = None
49
+ pictograms: frozenset[str] = frozenset()
50
+ hazard_codes: frozenset[str] = frozenset()
51
+ precautionary_codes: frozenset[str] = frozenset()
52
+ supplemental_codes: frozenset[str] = frozenset()
53
+ supplier_name: str | None = None
54
+ supplier_email: str | None = None
55
+
56
+
57
+ @dataclass(frozen=True)
58
+ class Difference:
59
+ """One field in which the embedded data and the sheet as read disagree."""
60
+
61
+ field: str
62
+ embedded: str | list[str] | None
63
+ read: str | list[str] | None
64
+
65
+
66
+ @dataclass(frozen=True)
67
+ class EmbeddedData:
68
+ """What a PDF's embedded eSDScom file says, or why it could not be read."""
69
+
70
+ filename: str
71
+ sheet: EmbeddedSheet | None = None
72
+ unreadable: Message | None = None
73
+ # The file itself, byte for byte, and only where it was read (SDW-13).
74
+ content: bytes | None = None
75
+
76
+
77
+ def read_embedded(path: Path) -> EmbeddedData | None:
78
+ """The first embedded eSDScom file of the PDF at *path*; None without one."""
79
+ with pymupdf.open(path) as document:
80
+ for index in range(document.embfile_count()):
81
+ name = str(document.embfile_names()[index])
82
+ content = document.embfile_get(index)
83
+ if _ROOT.encode() not in content[:4096]:
84
+ continue
85
+ return parse_embedded(name, content)
86
+ return None
87
+
88
+
89
+ def parse_embedded(name: str, content: bytes) -> EmbeddedData:
90
+ """An eSDScom file's *content*, as ``read_embedded`` reads it out of a PDF.
91
+
92
+ For a file that was taken out of its PDF before (``sdb-reader xml``).
93
+ *name* is the file's name. A file that is too large, carries a document
94
+ type declaration or is no eSDScom file comes back with ``unreadable`` set.
95
+
96
+ >>> data = parse_embedded(
97
+ ... "sds.xml",
98
+ ... b"<DatasheetFeed><Datasheet><HazardIdentification><HazardLabelling>"
99
+ ... b"<ClpLabellingInfo><ClpHazardStatement><PhraseCode>315</PhraseCode>"
100
+ ... b"</ClpHazardStatement></ClpLabellingInfo></HazardLabelling>"
101
+ ... b"</HazardIdentification></Datasheet></DatasheetFeed>",
102
+ ... )
103
+ >>> sorted(data.sheet.hazard_codes)
104
+ ['H315']
105
+ >>> parse_embedded("other.xml", b"<other/>").unreadable.code
106
+ 'embedded-wrong-root'
107
+ """
108
+ if len(content) > _MAX_BYTES:
109
+ return EmbeddedData(
110
+ name, unreadable=Message("embedded-too-large", {"limit_bytes": _MAX_BYTES})
111
+ )
112
+ if _DOCTYPE.search(content):
113
+ return EmbeddedData(name, unreadable=Message("embedded-doctype"))
114
+ try:
115
+ root = ET.fromstring(content)
116
+ except ET.ParseError as exc:
117
+ return EmbeddedData(
118
+ name, unreadable=Message("embedded-not-xml", {"detail": str(exc)})
119
+ )
120
+ if _local(root.tag) != _ROOT:
121
+ return EmbeddedData(
122
+ name,
123
+ unreadable=Message("embedded-wrong-root", {"root": _local(root.tag)}),
124
+ )
125
+ datasheet = _first(root, "Datasheet")
126
+ if datasheet is None:
127
+ return EmbeddedData(name, unreadable=Message("embedded-no-datasheet"))
128
+ return EmbeddedData(name, sheet=_sheet_of(datasheet), content=content)
129
+
130
+
131
+ def _sheet_of(datasheet: ET.Element) -> EmbeddedSheet:
132
+ exporting = _first(datasheet, "InformationFromExportingSystem")
133
+ identification = _first(datasheet, "IdentificationSubstPrep")
134
+ supplier = _first(identification, "SupplierInformation")
135
+ label = _first(datasheet, "ClpLabellingInfo")
136
+ system = _first(exporting, "SystemUsedInPreparation")
137
+ supplemental = _codes(label, "ClpSpecialSupplementalLabelInfoMixtures", "EUH")
138
+ # epos prints a further EUH statement as free text: "EUH211: Achtung! …".
139
+ for extra in _all(label, "ClpAdditionalLabellingInfo"):
140
+ supplemental |= set(_EUH_IN_TEXT.findall(_text(extra, "FullText") or ""))
141
+ signal = _first(label, "ClpSignalWord")
142
+ return EmbeddedSheet(
143
+ format_version=_text(exporting, "XmlStandardVersionNo"),
144
+ authoring_system=_joined_text(system),
145
+ date=_text(identification, "RevisionDate")
146
+ or _text(identification, "IssueDate"),
147
+ signal_word=_SIGNAL_WORDS.get(_text(signal, "PhraseCode") or "")
148
+ or _text(signal, "FullText"),
149
+ pictograms=frozenset(
150
+ code
151
+ for pictogram in _all(label, "ClpHazardPictogram")
152
+ if (code := _text(pictogram, "PhraseCode"))
153
+ ),
154
+ hazard_codes=frozenset(_codes(label, "ClpHazardStatement", "H")),
155
+ precautionary_codes=frozenset(_codes(label, "ClpPrecautionaryStatement", "P")),
156
+ supplemental_codes=frozenset(supplemental),
157
+ supplier_name=_text(supplier, "Name"),
158
+ supplier_email=_text(supplier, "Email"),
159
+ )
160
+
161
+
162
+ def differences(embedded: EmbeddedSheet, sheet: SafetyDataSheet) -> list[Difference]:
163
+ """The compared fields in which *sheet* as read differs from *embedded*."""
164
+ statements = sheet.hazard_statements
165
+ contact = sheet.contact_info
166
+ found: list[Difference] = []
167
+
168
+ def compare_sets(name: str, want: frozenset[str], got: set[str]) -> None:
169
+ if want != got:
170
+ found.append(Difference(name, sorted(want), sorted(got)))
171
+
172
+ def compare_text(name: str, want: str | None, got: str | None) -> None:
173
+ if _folded(want) != _folded(got):
174
+ found.append(Difference(name, want or None, got or None))
175
+
176
+ # A first issue has no revision, and the export then repeats the issue
177
+ # date as the revision's.
178
+ read_date = sheet.revision_date or sheet.issue_date
179
+ read_text = read_date.isoformat() if read_date else None
180
+ if (embedded.date or "")[:10] != (read_text or ""):
181
+ found.append(Difference("date", embedded.date, read_text))
182
+ compare_text("signal_word", embedded.signal_word, statements.signal_word)
183
+ compare_sets("pictograms", embedded.pictograms, set(statements.pictograms))
184
+ compare_sets("hazard_codes", embedded.hazard_codes, _parts(statements.hazard_codes))
185
+ compare_sets(
186
+ "precautionary_codes",
187
+ embedded.precautionary_codes,
188
+ _parts(statements.precautionary_codes),
189
+ )
190
+ compare_sets(
191
+ "supplemental_codes",
192
+ embedded.supplemental_codes,
193
+ _parts(statements.supplemental_codes),
194
+ )
195
+ compare_text("supplier_name", embedded.supplier_name, contact.supplier_name)
196
+ compare_text("supplier_email", embedded.supplier_email, contact.supplier_email)
197
+ return found
198
+
199
+
200
+ def _parts(codes: list[str]) -> set[str]:
201
+ """Each code of a joined one on its own: "P305+P351" is P305 and P351."""
202
+ return {part.strip() for code in codes for part in code.split("+") if part.strip()}
203
+
204
+
205
+ def _codes(parent: ET.Element | None, name: str, prefix: str) -> set[str]:
206
+ """The codes under *parent*; eSDScom writes "305+351+338" without the letter."""
207
+ found: set[str] = set()
208
+ for statement in _all(parent, name):
209
+ for part in (_text(statement, "PhraseCode") or "").split("+"):
210
+ part = part.strip()
211
+ if part:
212
+ found.add(part if part[0].isalpha() else prefix + part)
213
+ return found
214
+
215
+
216
+ def _folded(value: str | None) -> str:
217
+ return " ".join((value or "").split()).casefold()
218
+
219
+
220
+ def _local(tag: str) -> str:
221
+ return tag.rsplit("}", 1)[-1]
222
+
223
+
224
+ def _all(parent: ET.Element | None, name: str) -> list[ET.Element]:
225
+ if parent is None:
226
+ return []
227
+ return [element for element in parent.iter() if _local(element.tag) == name]
228
+
229
+
230
+ def _first(parent: ET.Element | None, name: str) -> ET.Element | None:
231
+ found = _all(parent, name)
232
+ return found[0] if found else None
233
+
234
+
235
+ def _text(parent: ET.Element | None, name: str) -> str | None:
236
+ """The text of *parent*'s own child *name*.
237
+
238
+ Its own child only: a supplier's "Name" has its contact person's below it.
239
+ """
240
+ if parent is None:
241
+ return None
242
+ element = next((child for child in parent if _local(child.tag) == name), None)
243
+ text = (element.text or "").strip() if element is not None else ""
244
+ return text or None
245
+
246
+
247
+ def _joined_text(element: ET.Element | None) -> str | None:
248
+ if element is None:
249
+ return None
250
+ return " ".join(part.strip() for part in element.itertext() if part.strip()) or None