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/__init__.py +157 -0
- sdb_reader/ask.py +437 -0
- sdb_reader/ask_record.schema.json +388 -0
- sdb_reader/cli.py +1079 -0
- sdb_reader/clp_statements_de.json +997 -0
- sdb_reader/compliance.py +546 -0
- sdb_reader/current.py +134 -0
- sdb_reader/diff.py +520 -0
- sdb_reader/embedded.py +250 -0
- sdb_reader/extraction.py +506 -0
- sdb_reader/layout.py +894 -0
- sdb_reader/line_breaks.py +40 -0
- sdb_reader/messages.py +394 -0
- sdb_reader/messages_data/de.toml +146 -0
- sdb_reader/messages_data/en.toml +141 -0
- sdb_reader/model.py +321 -0
- sdb_reader/parser.py +4601 -0
- sdb_reader/pictograms.py +374 -0
- sdb_reader/presets.py +110 -0
- sdb_reader/presets.toml +23 -0
- sdb_reader/py.typed +0 -0
- sdb_reader/read_record.schema.json +792 -0
- sdb_reader/reading.py +94 -0
- sdb_reader/scan.py +265 -0
- sdb_reader/schema.py +96 -0
- sdb_reader/serialise.py +463 -0
- sdb_reader/show.py +370 -0
- sdb_reader/statements.py +1011 -0
- sdb_reader/verification.py +322 -0
- sdb_reader/versions.py +31 -0
- sdb_reader-0.9.0.dist-info/METADATA +117 -0
- sdb_reader-0.9.0.dist-info/RECORD +37 -0
- sdb_reader-0.9.0.dist-info/WHEEL +5 -0
- sdb_reader-0.9.0.dist-info/entry_points.txt +2 -0
- sdb_reader-0.9.0.dist-info/licenses/LICENSE +661 -0
- sdb_reader-0.9.0.dist-info/licenses/NOTICE +40 -0
- sdb_reader-0.9.0.dist-info/top_level.txt +1 -0
sdb_reader/pictograms.py
ADDED
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
"""CLP-based GHS pictogram and signal word derivation from H-codes.
|
|
2
|
+
|
|
3
|
+
Based on CLP Regulation (EC) No 1272/2008 (Annex I).
|
|
4
|
+
|
|
5
|
+
Precedence rules (CLP Art. 26):
|
|
6
|
+
- GHS06 (skull) suppresses GHS07 (exclamation mark) for acute toxicity
|
|
7
|
+
- GHS05 (corrosion) suppresses GHS07 for skin/eye irritation
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
# ---------------------------------------------------------------------------
|
|
13
|
+
# H-code → GHS pictogram(s)
|
|
14
|
+
# ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
H_TO_PICTOGRAM: dict[str, set[str]] = {
|
|
17
|
+
# Physical hazards (H200 series)
|
|
18
|
+
"H200": {"GHS01"},
|
|
19
|
+
"H201": {"GHS01"},
|
|
20
|
+
"H202": {"GHS01"},
|
|
21
|
+
"H203": {"GHS01"},
|
|
22
|
+
"H204": {"GHS01"},
|
|
23
|
+
"H205": {"GHS01"},
|
|
24
|
+
"H206": {"GHS01", "GHS02"},
|
|
25
|
+
"H207": {"GHS01", "GHS02"},
|
|
26
|
+
"H208": {"GHS02"},
|
|
27
|
+
"H220": {"GHS02"},
|
|
28
|
+
"H221": {"GHS02"},
|
|
29
|
+
"H222": {"GHS02"},
|
|
30
|
+
"H223": {"GHS02"},
|
|
31
|
+
"H224": {"GHS02"},
|
|
32
|
+
"H225": {"GHS02"},
|
|
33
|
+
"H226": {"GHS02"},
|
|
34
|
+
"H228": {"GHS02"},
|
|
35
|
+
"H229": set(),
|
|
36
|
+
# Anhang I, Tabelle 2.2.3: chemically unstable and pyrophoric gases are
|
|
37
|
+
# flammable gases of category 1A and carry its flame.
|
|
38
|
+
"H230": {"GHS02"},
|
|
39
|
+
"H231": {"GHS02"},
|
|
40
|
+
"H232": {"GHS02"},
|
|
41
|
+
"H240": {"GHS01"},
|
|
42
|
+
"H241": {"GHS01", "GHS02"},
|
|
43
|
+
"H242": {"GHS02"},
|
|
44
|
+
"H250": {"GHS02"},
|
|
45
|
+
"H251": {"GHS02"},
|
|
46
|
+
"H252": {"GHS02"},
|
|
47
|
+
"H260": {"GHS02"},
|
|
48
|
+
"H261": {"GHS02"},
|
|
49
|
+
"H270": {"GHS03"},
|
|
50
|
+
"H271": {"GHS03"},
|
|
51
|
+
"H272": {"GHS03"},
|
|
52
|
+
"H280": {"GHS04"},
|
|
53
|
+
"H281": {"GHS04"},
|
|
54
|
+
"H290": {"GHS05"},
|
|
55
|
+
# Health hazards (H300 series)
|
|
56
|
+
"H300": {"GHS06"},
|
|
57
|
+
"H301": {"GHS06"},
|
|
58
|
+
"H302": {"GHS07"},
|
|
59
|
+
"H304": {"GHS08"},
|
|
60
|
+
"H310": {"GHS06"},
|
|
61
|
+
"H311": {"GHS06"},
|
|
62
|
+
"H312": {"GHS07"},
|
|
63
|
+
"H314": {"GHS05"},
|
|
64
|
+
"H315": {"GHS07"},
|
|
65
|
+
"H317": {"GHS07"},
|
|
66
|
+
"H318": {"GHS05"},
|
|
67
|
+
"H319": {"GHS07"},
|
|
68
|
+
"H330": {"GHS06"},
|
|
69
|
+
"H331": {"GHS06"},
|
|
70
|
+
"H332": {"GHS07"},
|
|
71
|
+
"H334": {"GHS08"},
|
|
72
|
+
"H335": {"GHS07"},
|
|
73
|
+
"H336": {"GHS07"},
|
|
74
|
+
"H340": {"GHS08"},
|
|
75
|
+
"H341": {"GHS08"},
|
|
76
|
+
"H350": {"GHS08"},
|
|
77
|
+
"H351": {"GHS08"},
|
|
78
|
+
"H360": {"GHS08"},
|
|
79
|
+
"H361": {"GHS08"},
|
|
80
|
+
"H362": set(),
|
|
81
|
+
"H370": {"GHS08"},
|
|
82
|
+
"H371": {"GHS08"},
|
|
83
|
+
"H372": {"GHS08"},
|
|
84
|
+
"H373": {"GHS08"},
|
|
85
|
+
# Environmental hazards (H400 series)
|
|
86
|
+
"H400": {"GHS09"},
|
|
87
|
+
"H410": {"GHS09"},
|
|
88
|
+
"H411": {"GHS09"},
|
|
89
|
+
"H412": set(),
|
|
90
|
+
"H413": set(),
|
|
91
|
+
"H420": {"GHS07"},
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
# Combined H-codes as printed in many SDS documents
|
|
95
|
+
COMBINED_H_CODES: dict[str, list[str]] = {
|
|
96
|
+
"H300+H310": ["H300", "H310"],
|
|
97
|
+
"H300+H330": ["H300", "H330"],
|
|
98
|
+
"H310+H330": ["H310", "H330"],
|
|
99
|
+
"H300+H310+H330": ["H300", "H310", "H330"],
|
|
100
|
+
"H301+H311": ["H301", "H311"],
|
|
101
|
+
"H301+H331": ["H301", "H331"],
|
|
102
|
+
"H311+H331": ["H311", "H331"],
|
|
103
|
+
"H301+H311+H331": ["H301", "H311", "H331"],
|
|
104
|
+
"H302+H312": ["H302", "H312"],
|
|
105
|
+
"H302+H332": ["H302", "H332"],
|
|
106
|
+
"H312+H332": ["H312", "H332"],
|
|
107
|
+
"H302+H312+H332": ["H302", "H312", "H332"],
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
# CLP Art. 26 precedence: if key pictogram is present, suppress value pictograms
|
|
111
|
+
# CLP Art. 26 precedence. Only rule (b) is unconditional; (c) and (d) depend on
|
|
112
|
+
# *why* GHS07 applies, so they cannot be expressed as a plain pictogram-to-
|
|
113
|
+
# pictogram table. Suppressing GHS07 whenever GHS05 is present drops the
|
|
114
|
+
# exclamation mark from, say, H302 + H318 -- acute oral toxicity, which Art. 26
|
|
115
|
+
# does not suppress at all.
|
|
116
|
+
GHS07_IRRITATION_CODES: frozenset[str] = frozenset({"H315", "H319"})
|
|
117
|
+
GHS07_SKIN_SENSITISATION_CODES: frozenset[str] = frozenset({"H317"})
|
|
118
|
+
GHS08_RESPIRATORY_SENSITISATION_CODES: frozenset[str] = frozenset({"H334"})
|
|
119
|
+
|
|
120
|
+
# ---------------------------------------------------------------------------
|
|
121
|
+
# H-code → signal word
|
|
122
|
+
# ---------------------------------------------------------------------------
|
|
123
|
+
|
|
124
|
+
_GEFAHR_CODES: frozenset[str] = frozenset(
|
|
125
|
+
{
|
|
126
|
+
# Anhang I, Tabelle 2.6.2: Flam. Liq. 2 carries "Gefahr". This said
|
|
127
|
+
# "Achtung" until the tables were checked against the regulation.
|
|
128
|
+
"H225",
|
|
129
|
+
# Anhang I, Tabelle 2.11.1: Self-heat. 1 carries "Gefahr".
|
|
130
|
+
"H251",
|
|
131
|
+
"H200",
|
|
132
|
+
"H201",
|
|
133
|
+
"H202",
|
|
134
|
+
"H203",
|
|
135
|
+
"H205",
|
|
136
|
+
"H206",
|
|
137
|
+
"H207",
|
|
138
|
+
"H220",
|
|
139
|
+
"H222",
|
|
140
|
+
"H224",
|
|
141
|
+
"H240",
|
|
142
|
+
"H241",
|
|
143
|
+
"H250",
|
|
144
|
+
"H260",
|
|
145
|
+
"H270",
|
|
146
|
+
"H271",
|
|
147
|
+
"H300",
|
|
148
|
+
"H301",
|
|
149
|
+
"H310",
|
|
150
|
+
"H311",
|
|
151
|
+
"H314",
|
|
152
|
+
"H318",
|
|
153
|
+
"H330",
|
|
154
|
+
"H331",
|
|
155
|
+
"H334",
|
|
156
|
+
"H340",
|
|
157
|
+
"H350",
|
|
158
|
+
"H360",
|
|
159
|
+
"H370",
|
|
160
|
+
"H372",
|
|
161
|
+
"H304",
|
|
162
|
+
# Anhang I, Tabelle 2.2.3: chemically unstable gases A and B, and
|
|
163
|
+
# pyrophoric gases, all within Flam. Gas 1A.
|
|
164
|
+
"H230",
|
|
165
|
+
"H231",
|
|
166
|
+
"H232",
|
|
167
|
+
# The hazard classes added in 2023 number their hazard statements as
|
|
168
|
+
# EUH and carry a signal word, but no pictogram. Category 1 of the
|
|
169
|
+
# endocrine disruptors (Tabellen 3.11.3 and 4.2.3), PBT and vPvB
|
|
170
|
+
# (4.3.1), PMT and vPvM (4.4.1).
|
|
171
|
+
"EUH380",
|
|
172
|
+
"EUH430",
|
|
173
|
+
"EUH440",
|
|
174
|
+
"EUH441",
|
|
175
|
+
"EUH450",
|
|
176
|
+
"EUH451",
|
|
177
|
+
}
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
_ACHTUNG_CODES: frozenset[str] = frozenset(
|
|
181
|
+
{
|
|
182
|
+
# H410 is Aquatic Chronic 1 and carries "Achtung"; H411, H412 and
|
|
183
|
+
# H413 are categories 2 to 4 and carry none. Anhang I, the table
|
|
184
|
+
# headed LANGFRISTIG (CHRONISCH) GEWÄSSERGEFÄHRDEND. H411 sat here
|
|
185
|
+
# until the verifier learned to read that table -- it is headed
|
|
186
|
+
# differently from the rest and was being skipped.
|
|
187
|
+
"H410",
|
|
188
|
+
# Endocrine disruptors of category 2 (Tabellen 3.11.3 and 4.2.3).
|
|
189
|
+
"EUH381",
|
|
190
|
+
"EUH431",
|
|
191
|
+
# Anhang I, Tabelle 2.1.2: explosives of division 1.4 carry "Achtung",
|
|
192
|
+
# the only division that does. This said "Gefahr" with the others.
|
|
193
|
+
"H204",
|
|
194
|
+
# Anhang I, Tabelle 2.5.2: gases under pressure carry "Achtung" in all
|
|
195
|
+
# four groups. Both said "Gefahr" until the verifier read the table: its
|
|
196
|
+
# row label breaks across a line and was not found.
|
|
197
|
+
"H280",
|
|
198
|
+
"H281",
|
|
199
|
+
# Anhang I, Tabelle 2.16.1: Met. Corr. 1 carries "Achtung". Corrosion
|
|
200
|
+
# towards metals, not towards skin -- this said "Gefahr".
|
|
201
|
+
"H290",
|
|
202
|
+
"H208",
|
|
203
|
+
"H221",
|
|
204
|
+
"H223",
|
|
205
|
+
"H226",
|
|
206
|
+
"H228",
|
|
207
|
+
# Anhang I, Tabelle 2.3.1: an aerosol of category 3 carries H229 alone
|
|
208
|
+
# and "Achtung". Beside H222 (category 1) the product's word is
|
|
209
|
+
# "Gefahr" through H222, beside H223 (category 2) "Achtung": the
|
|
210
|
+
# stronger word wins, so one entry holds for all three categories.
|
|
211
|
+
"H229",
|
|
212
|
+
"H242",
|
|
213
|
+
"H252",
|
|
214
|
+
"H261",
|
|
215
|
+
"H272",
|
|
216
|
+
"H302",
|
|
217
|
+
"H312",
|
|
218
|
+
"H315",
|
|
219
|
+
"H317",
|
|
220
|
+
"H319",
|
|
221
|
+
"H332",
|
|
222
|
+
"H335",
|
|
223
|
+
"H336",
|
|
224
|
+
"H341",
|
|
225
|
+
"H351",
|
|
226
|
+
"H361",
|
|
227
|
+
"H371",
|
|
228
|
+
"H373",
|
|
229
|
+
"H400",
|
|
230
|
+
"H420",
|
|
231
|
+
}
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
# Six statements carry different signal words depending on the category they
|
|
235
|
+
# were assigned, which a table keyed on the statement alone cannot express:
|
|
236
|
+
#
|
|
237
|
+
# H207 Gefahr in Kategorie 2, Achtung in Kategorie 3
|
|
238
|
+
# (Anhang I, Tabelle 2.17.2 -- desensitised explosives)
|
|
239
|
+
# H221 Gefahr in Kategorie 1B, Achtung in Kategorie 2
|
|
240
|
+
# (Anhang I, Tabelle 2.2.3 -- flammable gases)
|
|
241
|
+
# H228 Gefahr in Kategorie 1, Achtung in Kategorie 2
|
|
242
|
+
# (Anhang I, Tabelle 2.7.2 -- flammable solids)
|
|
243
|
+
# H242 Gefahr in Typen C & D, Achtung in Typen E & F
|
|
244
|
+
# (Anhang I, Tabelle 2.8.1 and 2.15.1 -- self-reactive substances and
|
|
245
|
+
# organic peroxides, two hazard classes sharing the statement)
|
|
246
|
+
# H261 Gefahr in Kategorie 2, Achtung in Kategorie 3
|
|
247
|
+
# (Anhang I, Tabelle 2.12.2 -- substances emitting flammable gases
|
|
248
|
+
# in contact with water)
|
|
249
|
+
# H272 Gefahr in Kategorie 2, Achtung in Kategorie 3
|
|
250
|
+
# (Anhang I, Tabelle 2.13.2 and 2.14.2 -- oxidising liquids and solids)
|
|
251
|
+
#
|
|
252
|
+
# Each is entered with one of the two answers. H229 looked like a fifth and is
|
|
253
|
+
# none: its table gives it two words too, but category 1 prints H222 beside it
|
|
254
|
+
# and the word comes from there, so "Achtung" for H229 is right in every
|
|
255
|
+
# category. tools/clp/verify_tables.py compares column by column for that
|
|
256
|
+
# reason: what counts is the word of the statements a category prints together.
|
|
257
|
+
#
|
|
258
|
+
# It costs little: a sheet that carries one of these nearly always prints its
|
|
259
|
+
# signal word, which is then read rather than derived. Resolving it properly
|
|
260
|
+
# needs the classification code -- "Flam. Sol. 1", "Org. Perox. CD" -- from
|
|
261
|
+
# section 2.1, which the parser does not read.
|
|
262
|
+
H_TO_SIGNAL_WORD: dict[str, str] = {c: "Gefahr" for c in _GEFAHR_CODES}
|
|
263
|
+
H_TO_SIGNAL_WORD.update({c: "Achtung" for c in _ACHTUNG_CODES})
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
# ---------------------------------------------------------------------------
|
|
267
|
+
# Public API
|
|
268
|
+
# ---------------------------------------------------------------------------
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def _resolve(code: str) -> list[str]:
|
|
272
|
+
"""Expand combined codes (e.g. 'H300+H310') to individual codes."""
|
|
273
|
+
upper = code.strip().upper()
|
|
274
|
+
return COMBINED_H_CODES.get(upper, [upper])
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def pictograms_for(h_codes: set[str], *, apply_precedence: bool = True) -> set[str]:
|
|
278
|
+
"""Derive GHS pictogram codes from a set of H-codes.
|
|
279
|
+
|
|
280
|
+
*h_codes* are H-statement codes, combined ones included
|
|
281
|
+
(``{"H225", "H302", "H300+H310"}``); a code the table does not know adds
|
|
282
|
+
nothing. With *apply_precedence*, the default, a pictogram that Article 26
|
|
283
|
+
of the CLP Regulation lets another one replace is left out.
|
|
284
|
+
|
|
285
|
+
>>> sorted(pictograms_for({"H225", "H302"}))
|
|
286
|
+
['GHS02', 'GHS07']
|
|
287
|
+
>>> sorted(pictograms_for({"H300", "H302"}))
|
|
288
|
+
['GHS06']
|
|
289
|
+
"""
|
|
290
|
+
resolved: set[str] = set()
|
|
291
|
+
for raw in h_codes:
|
|
292
|
+
resolved.update(_resolve(raw))
|
|
293
|
+
|
|
294
|
+
pictograms: set[str] = set()
|
|
295
|
+
for code in resolved:
|
|
296
|
+
pictograms.update(H_TO_PICTOGRAM.get(code, set()))
|
|
297
|
+
|
|
298
|
+
if apply_precedence and pictograms:
|
|
299
|
+
pictograms = suppress_pictograms(pictograms, resolved)
|
|
300
|
+
|
|
301
|
+
return pictograms
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def suppress_pictograms(pictograms: set[str], h_codes: set[str]) -> set[str]:
|
|
305
|
+
"""Apply the CLP Art. 26 precedence rules to *pictograms*.
|
|
306
|
+
|
|
307
|
+
*h_codes* must be the already-resolved H-codes behind them, because two of
|
|
308
|
+
the three rules turn on which hazard triggered GHS07:
|
|
309
|
+
|
|
310
|
+
(b) GHS06 present -> GHS07 is dropped outright.
|
|
311
|
+
(c) GHS05 present -> GHS07 is dropped only where it stands for skin or eye
|
|
312
|
+
irritation. Acute toxicity (H302, H312, H332) and the narcotic and
|
|
313
|
+
respiratory-irritation statements (H335, H336) keep it.
|
|
314
|
+
(d) GHS08 present *for respiratory sensitisation* (H334) -> GHS07 is dropped
|
|
315
|
+
where it stands for skin sensitisation or for irritation.
|
|
316
|
+
"""
|
|
317
|
+
|
|
318
|
+
result = set(pictograms)
|
|
319
|
+
if "GHS07" not in result:
|
|
320
|
+
return result
|
|
321
|
+
|
|
322
|
+
resolved: set[str] = set()
|
|
323
|
+
for raw in h_codes:
|
|
324
|
+
resolved.update(_resolve(raw))
|
|
325
|
+
ghs07_triggers = {
|
|
326
|
+
code for code in resolved if "GHS07" in H_TO_PICTOGRAM.get(code, set())
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if "GHS06" in result:
|
|
330
|
+
result.discard("GHS07")
|
|
331
|
+
return result
|
|
332
|
+
|
|
333
|
+
if "GHS05" in result and ghs07_triggers <= GHS07_IRRITATION_CODES:
|
|
334
|
+
result.discard("GHS07")
|
|
335
|
+
return result
|
|
336
|
+
|
|
337
|
+
if (
|
|
338
|
+
"GHS08" in result
|
|
339
|
+
and resolved & GHS08_RESPIRATORY_SENSITISATION_CODES
|
|
340
|
+
and ghs07_triggers <= (GHS07_IRRITATION_CODES | GHS07_SKIN_SENSITISATION_CODES)
|
|
341
|
+
):
|
|
342
|
+
result.discard("GHS07")
|
|
343
|
+
|
|
344
|
+
return result
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def get_signal_word(h_codes: set[str]) -> str | None:
|
|
348
|
+
"""Derive the signal word from a set of H-codes.
|
|
349
|
+
|
|
350
|
+
The EUH codes of the hazard classes added in 2023 count like H-codes:
|
|
351
|
+
the regulation gives them a signal word (SDB-7).
|
|
352
|
+
|
|
353
|
+
'Gefahr' takes precedence over 'Achtung' per CLP Art. 19(4).
|
|
354
|
+
|
|
355
|
+
Args:
|
|
356
|
+
h_codes: H-statement codes, e.g. ``{"H225", "H302"}``.
|
|
357
|
+
|
|
358
|
+
Returns:
|
|
359
|
+
``"Gefahr"``, ``"Achtung"``, or ``None`` (not classified / no match).
|
|
360
|
+
"""
|
|
361
|
+
has_gefahr = False
|
|
362
|
+
has_achtung = False
|
|
363
|
+
for raw in h_codes:
|
|
364
|
+
for code in _resolve(raw):
|
|
365
|
+
sw = H_TO_SIGNAL_WORD.get(code)
|
|
366
|
+
if sw == "Gefahr":
|
|
367
|
+
has_gefahr = True
|
|
368
|
+
elif sw == "Achtung":
|
|
369
|
+
has_achtung = True
|
|
370
|
+
if has_gefahr:
|
|
371
|
+
return "Gefahr"
|
|
372
|
+
if has_achtung:
|
|
373
|
+
return "Achtung"
|
|
374
|
+
return None
|
sdb_reader/presets.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""Request fields for the model families the command knows (SDW-31).
|
|
2
|
+
|
|
3
|
+
A model that reasons before it answers is asked to copy from a page, not to
|
|
4
|
+
deduce: reasoning costs time and tokens there and buys nothing. How it is
|
|
5
|
+
switched off differs from service to service and is no part of the standard
|
|
6
|
+
interface, so the strings ship as data: ``presets.toml``, one table per
|
|
7
|
+
preset, with the model and the day it was measured with.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import tomllib
|
|
13
|
+
from collections.abc import Mapping
|
|
14
|
+
from dataclasses import dataclass, field
|
|
15
|
+
from datetime import date
|
|
16
|
+
from importlib import resources
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
from .ask import OMITTABLE_REQUEST_FIELDS, RESERVED_REQUEST_FIELDS
|
|
21
|
+
|
|
22
|
+
_KEYS = frozenset({"description", "request", "omit", "tested_with", "tested_on"})
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True, slots=True)
|
|
26
|
+
class Preset:
|
|
27
|
+
"""What one preset puts into a request, and what is known about it."""
|
|
28
|
+
|
|
29
|
+
name: str
|
|
30
|
+
# One line per language.
|
|
31
|
+
description: Mapping[str, str]
|
|
32
|
+
# Merged into every request.
|
|
33
|
+
request: Mapping[str, Any] = field(default_factory=dict)
|
|
34
|
+
# Fields of the standard request left out.
|
|
35
|
+
omit: frozenset[str] = frozenset()
|
|
36
|
+
# The model and the day the preset was measured with, or neither.
|
|
37
|
+
tested_with: str | None = None
|
|
38
|
+
tested_on: date | None = None
|
|
39
|
+
|
|
40
|
+
def describe(self, language: str) -> str:
|
|
41
|
+
"""The preset's line in *language*, in English where it has none."""
|
|
42
|
+
return self.description.get(language) or self.description["en"]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def shipped_presets() -> dict[str, Preset]:
|
|
46
|
+
"""The presets the package ships, by name."""
|
|
47
|
+
source = resources.files("sdb_reader").joinpath("presets.toml")
|
|
48
|
+
return _presets(source.read_text(encoding="utf-8"), "presets.toml")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def load_presets(path: Path) -> dict[str, Preset]:
|
|
52
|
+
"""A caller's own presets: a TOML file like the shipped one.
|
|
53
|
+
|
|
54
|
+
Raises ValueError for a file that cannot be read as presets.
|
|
55
|
+
"""
|
|
56
|
+
try:
|
|
57
|
+
text = path.read_text(encoding="utf-8")
|
|
58
|
+
except OSError as exc:
|
|
59
|
+
raise ValueError(f"{path}: {exc}") from exc
|
|
60
|
+
return _presets(text, str(path))
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _presets(text: str, source: str) -> dict[str, Preset]:
|
|
64
|
+
try:
|
|
65
|
+
data = tomllib.loads(text)
|
|
66
|
+
except tomllib.TOMLDecodeError as exc:
|
|
67
|
+
raise ValueError(f"{source} is no TOML: {exc}") from exc
|
|
68
|
+
return {name: _preset(name, table, source) for name, table in data.items()}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _preset(name: str, table: object, source: str) -> Preset:
|
|
72
|
+
def refuse(why: str) -> ValueError:
|
|
73
|
+
return ValueError(f"{source}, preset {name}: {why}")
|
|
74
|
+
|
|
75
|
+
if not isinstance(table, dict):
|
|
76
|
+
raise refuse("is no table")
|
|
77
|
+
unknown = sorted(table.keys() - _KEYS)
|
|
78
|
+
if unknown:
|
|
79
|
+
raise refuse(f"unknown key {', '.join(unknown)}")
|
|
80
|
+
description = table.get("description")
|
|
81
|
+
if isinstance(description, str):
|
|
82
|
+
description = {"en": description}
|
|
83
|
+
if (
|
|
84
|
+
not isinstance(description, dict)
|
|
85
|
+
or not description.get("en")
|
|
86
|
+
or not all(isinstance(line, str) for line in description.values())
|
|
87
|
+
):
|
|
88
|
+
raise refuse("needs a description, a line or a table with a line for en")
|
|
89
|
+
request = table.get("request", {})
|
|
90
|
+
if not isinstance(request, dict):
|
|
91
|
+
raise refuse("request is no table")
|
|
92
|
+
taken = sorted(RESERVED_REQUEST_FIELDS & request.keys())
|
|
93
|
+
if taken:
|
|
94
|
+
raise refuse(f"request may not set {', '.join(taken)}")
|
|
95
|
+
omit = table.get("omit", [])
|
|
96
|
+
if not isinstance(omit, list) or not set(omit) <= OMITTABLE_REQUEST_FIELDS:
|
|
97
|
+
allowed = ", ".join(sorted(OMITTABLE_REQUEST_FIELDS))
|
|
98
|
+
raise refuse(f"omit is a list that may name only: {allowed}")
|
|
99
|
+
if not request and not omit:
|
|
100
|
+
raise refuse("changes nothing: it needs request or omit")
|
|
101
|
+
tested_with, tested_on = table.get("tested_with"), table.get("tested_on")
|
|
102
|
+
if (tested_with is None) != (tested_on is None):
|
|
103
|
+
raise refuse("tested_with and tested_on go together")
|
|
104
|
+
if tested_with is not None and (
|
|
105
|
+
not isinstance(tested_with, str)
|
|
106
|
+
or not tested_with
|
|
107
|
+
or type(tested_on) is not date
|
|
108
|
+
):
|
|
109
|
+
raise refuse("tested_with is a model's name and tested_on a date")
|
|
110
|
+
return Preset(name, description, request, frozenset(omit), tested_with, tested_on)
|
sdb_reader/presets.toml
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# What a model family needs in a request beyond the standard fields (SDW-31).
|
|
2
|
+
#
|
|
3
|
+
# One table per preset:
|
|
4
|
+
# description one line, per language of the command
|
|
5
|
+
# request fields merged into every request
|
|
6
|
+
# omit fields of the standard request left out (only "temperature")
|
|
7
|
+
# tested_with the model the preset was measured with by this command
|
|
8
|
+
# tested_on the day of that measurement
|
|
9
|
+
#
|
|
10
|
+
# A service can change what it takes: a preset says what held on the day it
|
|
11
|
+
# names, and a preset without that line was not measured with this command.
|
|
12
|
+
|
|
13
|
+
[deepseek-no-reasoning]
|
|
14
|
+
description = { de = "DeepSeek: das Nachdenken des Modells abschalten", en = "DeepSeek: switch the model's reasoning off" }
|
|
15
|
+
request = { thinking = { type = "disabled" } }
|
|
16
|
+
tested_with = "deepseek-flash"
|
|
17
|
+
tested_on = 2026-11-18
|
|
18
|
+
|
|
19
|
+
[ollama-no-reasoning]
|
|
20
|
+
description = { de = "Ollama: das Nachdenken eines Modells abschalten, das nachdenkt", en = "Ollama: switch the reasoning of a reasoning model off" }
|
|
21
|
+
request = { reasoning_effort = "none" }
|
|
22
|
+
tested_with = "qwen3.8:27b-mlx"
|
|
23
|
+
tested_on = 2026-11-18
|
sdb_reader/py.typed
ADDED
|
File without changes
|