Fundacja Hospicjum commited on
Commit
61edfdd
·
1 Parent(s): fc33ea8

Factor shared Sejm API helpers into sejm_api_common.py

Browse files

fetch_sejm_api_2011_2022.py kept byte-identical copies of sha256_file,
canonical_sha256, write_json and near-identical legal_evidence / stable_id
(differing only in signatures). The shared logic now lives once in
src/sejm_api_common.py; both fetchers import it (review item 4 on #18).

src/fetch_sejm_api.py CHANGED
@@ -41,26 +41,12 @@ class ContributionError(ValueError):
41
  """Raised when the contribution cannot meet its declared contract."""
42
 
43
 
44
- def canonical_sha256(value: Any) -> str:
45
- payload = json.dumps(
46
- value, ensure_ascii=False, sort_keys=True, separators=(",", ":")
47
- ).encode("utf-8")
48
- return hashlib.sha256(payload).hexdigest()
49
-
50
-
51
- def sha256_file(path: pathlib.Path) -> str:
52
- digest = hashlib.sha256()
53
- with path.open("rb") as stream:
54
- for chunk in iter(lambda: stream.read(1024 * 1024), b""):
55
- digest.update(chunk)
56
- return digest.hexdigest()
57
 
58
 
59
  def stable_document_id(row: dict[str, Any]) -> str:
60
- material = "\x1f".join(
61
- str(row.get(field, "")).strip() for field in ("date", "term", "speaker", "text")
62
- )
63
- return f"{SOURCE_NAME}_{hashlib.sha256(material.encode('utf-8')).hexdigest()}"
64
 
65
 
66
  def text_fingerprint(text: str) -> str:
@@ -181,40 +167,17 @@ def insert_source_registry_entry(registry: str, *, end_date: str) -> str:
181
 
182
 
183
  def legal_evidence(*, source_revision: str, target_revision: str) -> dict[str, Any]:
184
- return {
185
- "status": "reviewed-open-official-materials",
186
- "source": SOURCE_NAME,
187
- "dataset_record_license": SOURCE_LICENSE,
188
- "hf_source_license_name": "polish-public-sector-open-statutory-reuse",
189
- "provider": "Kancelaria Sejmu RP",
190
- "official_source": SEJM_API,
191
- "official_source_documentation": SEJM_API_DOCUMENTATION,
192
- "copyright_basis": {
193
- "act": "Polish Copyright and Related Rights Act",
194
- "article": "4(2)",
195
- "finding": "official documents and materials are excluded from copyright",
196
- "url": COPYRIGHT_ACT,
197
- },
198
- "reuse_basis": {
199
- "act": "Polish Open Data and Reuse of Public Sector Information Act",
200
- "articles": ["2(12)", "5", "6", "14(1)", "15", "17"],
201
- "url": OPEN_DATA_ACT,
202
- },
203
- "limitations": [
204
- "privacy and personal-data exceptions",
205
- "third-party rights and provider-specific conditions",
206
- "no upstream Creative Commons grant is asserted",
207
- ],
208
- "pinned_source": f"hf://datasets/{SOURCE_REPOSITORY}@{source_revision}",
209
- "pinned_target_base": f"hf://datasets/{TARGET_REPOSITORY}@{target_revision}",
210
- }
211
 
212
 
213
- def write_json(path: pathlib.Path, payload: Any) -> None:
214
- path.write_text(
215
- json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True) + "\n",
216
- encoding="utf-8",
217
- )
218
 
219
 
220
  def source_datasheet(
 
41
  """Raised when the contribution cannot meet its declared contract."""
42
 
43
 
44
+ import sejm_api_common as common
45
+ from sejm_api_common import canonical_sha256, sha256_file, write_json
 
 
 
 
 
 
 
 
 
 
 
46
 
47
 
48
  def stable_document_id(row: dict[str, Any]) -> str:
49
+ return common.stable_document_id(row, SOURCE_NAME)
 
 
 
50
 
51
 
52
  def text_fingerprint(text: str) -> str:
 
167
 
168
 
169
  def legal_evidence(*, source_revision: str, target_revision: str) -> dict[str, Any]:
170
+ return common.legal_evidence(
171
+ source_revision,
172
+ target_revision,
173
+ source_name=SOURCE_NAME,
174
+ source_license=SOURCE_LICENSE,
175
+ source_repository=SOURCE_REPOSITORY,
176
+ target_repository=TARGET_REPOSITORY,
177
+ )
178
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
 
 
 
 
 
 
181
 
182
 
183
  def source_datasheet(
src/fetch_sejm_api_2011_2022.py CHANGED
@@ -41,33 +41,15 @@ class BuildError(ValueError):
41
  pass
42
 
43
 
44
- def sha256_file(path: pathlib.Path) -> str:
45
- digest = hashlib.sha256()
46
- with path.open("rb") as stream:
47
- for chunk in iter(lambda: stream.read(1024 * 1024), b""):
48
- digest.update(chunk)
49
- return digest.hexdigest()
50
-
51
 
52
- def canonical_sha256(value: Any) -> str:
53
- payload = json.dumps(
54
- value, ensure_ascii=False, sort_keys=True, separators=(",", ":")
55
- ).encode("utf-8")
56
- return hashlib.sha256(payload).hexdigest()
57
 
58
 
59
- def write_json(path: pathlib.Path, payload: Any) -> None:
60
- path.write_text(
61
- json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True) + "\n",
62
- encoding="utf-8",
63
- )
64
 
65
 
66
  def stable_id(row: dict[str, Any]) -> str:
67
- material = "\x1f".join(
68
- str(row.get(field, "")).strip() for field in ("date", "term", "speaker", "text")
69
- )
70
- return f"{SOURCE_NAME}_{hashlib.sha256(material.encode('utf-8')).hexdigest()}"
71
 
72
 
73
  def registry_entry() -> str:
@@ -108,33 +90,14 @@ def update_registry(registry: str) -> str:
108
 
109
 
110
  def legal_evidence(source_revision: str, target_revision: str) -> dict[str, Any]:
111
- return {
112
- "status": "reviewed-open-official-materials",
113
- "source": SOURCE_NAME,
114
- "dataset_record_license": SOURCE_LICENSE,
115
- "hf_source_license_name": "polish-public-sector-open-statutory-reuse",
116
- "provider": "Kancelaria Sejmu RP",
117
- "official_source": SEJM_API,
118
- "official_source_documentation": SEJM_API_DOCUMENTATION,
119
- "copyright_basis": {
120
- "act": "Polish Copyright and Related Rights Act",
121
- "article": "4(2)",
122
- "finding": "official documents and materials are excluded from copyright",
123
- "url": COPYRIGHT_ACT,
124
- },
125
- "reuse_basis": {
126
- "act": "Polish Open Data and Reuse of Public Sector Information Act",
127
- "articles": ["2(12)", "5", "6", "14(1)", "15", "17"],
128
- "url": OPEN_DATA_ACT,
129
- },
130
- "limitations": [
131
- "privacy and personal-data exceptions",
132
- "third-party rights and provider-specific conditions",
133
- "no upstream Creative Commons grant is asserted",
134
- ],
135
- "pinned_source": f"hf://datasets/{SOURCE_REPOSITORY}@{source_revision}",
136
- "pinned_target_base": f"hf://datasets/{TARGET_REPOSITORY}@{target_revision}",
137
- }
138
 
139
 
140
  def datasheet(stats: dict[str, Any], source_revision: str, target_revision: str) -> str:
 
41
  pass
42
 
43
 
44
+ import sejm_api_common as common
45
+ from sejm_api_common import canonical_sha256, sha256_file, write_json
 
 
 
 
 
46
 
 
 
 
 
 
47
 
48
 
 
 
 
 
 
49
 
50
 
51
  def stable_id(row: dict[str, Any]) -> str:
52
+ return common.stable_document_id(row, SOURCE_NAME)
 
 
 
53
 
54
 
55
  def registry_entry() -> str:
 
90
 
91
 
92
  def legal_evidence(source_revision: str, target_revision: str) -> dict[str, Any]:
93
+ return common.legal_evidence(
94
+ source_revision,
95
+ target_revision,
96
+ source_name=SOURCE_NAME,
97
+ source_license=SOURCE_LICENSE,
98
+ source_repository=SOURCE_REPOSITORY,
99
+ target_repository=TARGET_REPOSITORY,
100
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
 
103
  def datasheet(stats: dict[str, Any], source_revision: str, target_revision: str) -> str:
src/sejm_api_common.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Shared helpers for the Sejm API speech fetchers.
2
+
3
+ Extracted from `fetch_sejm_api.py` and `fetch_sejm_api_2011_2022.py`, which kept
4
+ byte-identical copies of these utilities (and of `legal_evidence` /
5
+ `stable_document_id`, differing only in function signatures). Both fetchers now
6
+ wrap these; behavior is unchanged.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import hashlib
12
+ import json
13
+ import pathlib
14
+ from typing import Any
15
+
16
+ COPYRIGHT_ACT = "https://eli.gov.pl/api/acts/DU/2025/24/text/O/D20250024.pdf"
17
+ OPEN_DATA_ACT = "https://eli.gov.pl/api/acts/DU/2023/1524/text.html"
18
+ SEJM_API = "https://api.sejm.gov.pl/"
19
+ SEJM_API_DOCUMENTATION = "https://api.sejm.gov.pl/sejm.html"
20
+
21
+ EXPECTED_FIELDS = (
22
+ "id", "text", "source", "added", "created", "token_count", "license", "author"
23
+ )
24
+
25
+
26
+ def sha256_file(path: pathlib.Path) -> str:
27
+ digest = hashlib.sha256()
28
+ with path.open("rb") as stream:
29
+ for chunk in iter(lambda: stream.read(1024 * 1024), b""):
30
+ digest.update(chunk)
31
+ return digest.hexdigest()
32
+
33
+
34
+ def canonical_sha256(value: Any) -> str:
35
+ payload = json.dumps(
36
+ value, ensure_ascii=False, sort_keys=True, separators=(",", ":")
37
+ ).encode("utf-8")
38
+ return hashlib.sha256(payload).hexdigest()
39
+
40
+
41
+ def write_json(path: pathlib.Path, payload: Any) -> None:
42
+ path.write_text(
43
+ json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True) + "\n",
44
+ encoding="utf-8",
45
+ )
46
+
47
+
48
+ def stable_document_id(row: dict[str, Any], source_name: str) -> str:
49
+ material = "\x1f".join(
50
+ str(row.get(field, "")).strip() for field in ("date", "term", "speaker", "text")
51
+ )
52
+ return f"{source_name}_{hashlib.sha256(material.encode('utf-8')).hexdigest()}"
53
+
54
+
55
+ def legal_evidence(
56
+ source_revision: str,
57
+ target_revision: str,
58
+ *,
59
+ source_name: str,
60
+ source_license: str,
61
+ source_repository: str,
62
+ target_repository: str,
63
+ ) -> dict[str, Any]:
64
+ return {
65
+ "status": "reviewed-open-official-materials",
66
+ "source": source_name,
67
+ "dataset_record_license": source_license,
68
+ "hf_source_license_name": "polish-public-sector-open-statutory-reuse",
69
+ "provider": "Kancelaria Sejmu RP",
70
+ "official_source": SEJM_API,
71
+ "official_source_documentation": SEJM_API_DOCUMENTATION,
72
+ "copyright_basis": {
73
+ "act": "Polish Copyright and Related Rights Act",
74
+ "article": "4(2)",
75
+ "finding": "official documents and materials are excluded from copyright",
76
+ "url": COPYRIGHT_ACT,
77
+ },
78
+ "reuse_basis": {
79
+ "act": "Polish Act on Open Data and Reuse of Public Sector Information Act",
80
+ "articles": ["2(12)", "5", "6", "14(1)", "15", "17"],
81
+ "url": OPEN_DATA_ACT,
82
+ },
83
+ "limitations": [
84
+ "privacy and personal-data exceptions",
85
+ "third-party rights and provider-specific conditions",
86
+ "no upstream Creative Commons grant is asserted",
87
+ ],
88
+ "pinned_source": f"hf://datasets/{source_repository}@{source_revision}",
89
+ "pinned_target_base": f"hf://datasets/{target_repository}@{target_revision}",
90
+ }