civics-cdf-validator 1.67.dev20__tar.gz → 1.68.dev4__tar.gz
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.
- {civics_cdf_validator-1.67.dev20/civics_cdf_validator.egg-info → civics_cdf_validator-1.68.dev4}/PKG-INFO +1 -1
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/base.py +1 -1
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4/civics_cdf_validator.egg-info}/PKG-INFO +1 -1
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/gpunit_rules.py +94 -17
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/rulesets/common_rules.py +228 -15
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/rulesets/election_rules.py +91 -200
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/rulesets/metadata_rules.py +6 -3
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/rulesets/officeholder_rules.py +0 -19
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/rulesets/voter_information_rules.py +16 -13
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/validator.py +11 -3
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/version.py +1 -1
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/CONTRIBUTING.md +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/LICENSE-2.0.txt +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/MANIFEST.in +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/README.md +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/__init__.py +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/civics_cdf_validator.egg-info/SOURCES.txt +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/civics_cdf_validator.egg-info/dependency_links.txt +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/civics_cdf_validator.egg-info/entry_points.txt +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/civics_cdf_validator.egg-info/requires.txt +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/civics_cdf_validator.egg-info/top_level.txt +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/loggers.py +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/office_utils.py +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/rules.py +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/rulesets/__init__.py +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/rulesets/committee_rules.py +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/rulesets/election_dates_rules.py +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/rulesets/rule_util.py +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/setup.cfg +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/setup.py +0 -0
- {civics_cdf_validator-1.67.dev20 → civics_cdf_validator-1.68.dev4}/stats.py +0 -0
|
@@ -449,7 +449,7 @@ class RulesRegistry(SchemaHandler):
|
|
|
449
449
|
self.exceptions_wrapper.print_exceptions(severity, verbose)
|
|
450
450
|
|
|
451
451
|
def get_all_exceptions(self):
|
|
452
|
-
self.exceptions_wrapper.get_all_exceptions()
|
|
452
|
+
return self.exceptions_wrapper.get_all_exceptions()
|
|
453
453
|
|
|
454
454
|
def count_stats(self):
|
|
455
455
|
"""Aggregates the counts for each top level entity."""
|
|
@@ -21,6 +21,7 @@ import io
|
|
|
21
21
|
import os
|
|
22
22
|
import re
|
|
23
23
|
import shutil
|
|
24
|
+
import zipfile
|
|
24
25
|
|
|
25
26
|
import attrs
|
|
26
27
|
import pycountry
|
|
@@ -28,19 +29,19 @@ import requests
|
|
|
28
29
|
|
|
29
30
|
|
|
30
31
|
def generate_github_raw_url(repo_name, subdir, file_name, branch="master"):
|
|
31
|
-
return "https://raw.github.com/{
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
return f"https://raw.github.com/{repo_name}/{branch}/{subdir}/{file_name}"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def generate_github_archive_url(repo_name, branch="master"):
|
|
36
|
+
return f"https://github.com/{repo_name}/archive/refs/heads/{branch}.zip"
|
|
34
37
|
|
|
35
38
|
|
|
36
39
|
def generate_country_filename(country_code):
|
|
37
|
-
return "country-{
|
|
40
|
+
return f"country-{country_code}.csv"
|
|
38
41
|
|
|
39
42
|
|
|
40
43
|
def generate_absolute_path(path_to_directory, country_code):
|
|
41
|
-
return "{
|
|
42
|
-
path_to_directory, generate_country_filename(country_code)
|
|
43
|
-
)
|
|
44
|
+
return f"{path_to_directory}/{generate_country_filename(country_code)}"
|
|
44
45
|
|
|
45
46
|
|
|
46
47
|
class GpUnitOcdIdValidator(object):
|
|
@@ -55,19 +56,21 @@ class GpUnitOcdIdValidator(object):
|
|
|
55
56
|
OCD_PATTERN_ROOT = r"^ocd-division\/(country:[a-z]{2}|region:eu)$"
|
|
56
57
|
OCD_MATCHER_ROOT = re.compile(OCD_PATTERN_ROOT, flags=re.U)
|
|
57
58
|
|
|
58
|
-
def __init__(self, country_code, local_file, ocd_id_list=None):
|
|
59
|
+
def __init__(self, country_code=None, local_file=None, ocd_id_list=None):
|
|
59
60
|
"""Initialize the class.
|
|
60
61
|
|
|
61
62
|
If a list is provided, that list will be used to initialize the set of OCD
|
|
62
63
|
IDs. If a file is provided, the OCD IDs will be pulled from the local file.
|
|
63
|
-
Otherwise, the OCD IDs will be fetched from
|
|
64
|
+
Otherwise, the OCD IDs will be fetched from GitHub for the given country
|
|
65
|
+
code, or for all countries if no country code is provided.
|
|
64
66
|
|
|
65
67
|
Args:
|
|
66
|
-
country_code: the code for the country to fetch OCD IDs for
|
|
68
|
+
country_code: the code for the country to fetch OCD IDs for, or None/empty
|
|
69
|
+
to fetch OCD IDs for all countries
|
|
67
70
|
local_file: the file containing the OCD IDs
|
|
68
71
|
ocd_id_list: a list of OCD IDs
|
|
69
72
|
"""
|
|
70
|
-
if ocd_id_list:
|
|
73
|
+
if ocd_id_list is not None:
|
|
71
74
|
self.ocd_ids = frozenset(ocd_id_list)
|
|
72
75
|
else:
|
|
73
76
|
extractor = OcdIdsExtractor(country_code, local_file)
|
|
@@ -125,6 +128,11 @@ class OcdIdsExtractor(object):
|
|
|
125
128
|
CACHE_DIR = "~/.cache"
|
|
126
129
|
GITHUB_REPO = "opencivicdata/ocd-division-ids"
|
|
127
130
|
GITHUB_DIR = "identifiers"
|
|
131
|
+
GITHUB_ARCHIVE_FILENAME = "ocd-division-ids-master.zip"
|
|
132
|
+
_ARCHIVE_IDENTIFIER_PATTERN = re.compile(
|
|
133
|
+
r"^[^/]+/identifiers/country-(?P<country_code>[a-z]{2})\.csv$"
|
|
134
|
+
)
|
|
135
|
+
_DOWNLOAD_CHUNK_SIZE = 65536
|
|
128
136
|
EU_COUNTRIES = frozenset([
|
|
129
137
|
"at",
|
|
130
138
|
"be",
|
|
@@ -158,7 +166,7 @@ class OcdIdsExtractor(object):
|
|
|
158
166
|
|
|
159
167
|
@attrs.define(frozen=True)
|
|
160
168
|
class OcdIdsExtractorConfig:
|
|
161
|
-
country_code: str
|
|
169
|
+
country_code: str | None
|
|
162
170
|
github_filename: str
|
|
163
171
|
github_raw_url: str
|
|
164
172
|
local_filepath: str
|
|
@@ -189,9 +197,16 @@ class OcdIdsExtractor(object):
|
|
|
189
197
|
])
|
|
190
198
|
|
|
191
199
|
cache_directory = os.path.expanduser(self.CACHE_DIR)
|
|
200
|
+
normalized_country_code = (
|
|
201
|
+
self.country_code.strip().lower() if self.country_code else None
|
|
202
|
+
)
|
|
203
|
+
if not normalized_country_code:
|
|
204
|
+
self._create_cache(cache_directory)
|
|
205
|
+
configs = self._prepare_all_countries_cache(cache_directory)
|
|
206
|
+
return self._read_csv(configs)
|
|
192
207
|
|
|
193
208
|
# Generate configs.
|
|
194
|
-
configs = self._generate_configs(
|
|
209
|
+
configs = self._generate_configs(normalized_country_code, cache_directory)
|
|
195
210
|
|
|
196
211
|
# Create the cache directory if it doesn't exist.
|
|
197
212
|
self._create_cache(cache_directory)
|
|
@@ -202,6 +217,67 @@ class OcdIdsExtractor(object):
|
|
|
202
217
|
|
|
203
218
|
return self._read_csv(configs)
|
|
204
219
|
|
|
220
|
+
def _generate_archive_config(
|
|
221
|
+
self, cache_directory: str
|
|
222
|
+
) -> OcdIdsExtractorConfig:
|
|
223
|
+
"""Generates the extractor config for the repository archive."""
|
|
224
|
+
return self.OcdIdsExtractorConfig(
|
|
225
|
+
country_code=None,
|
|
226
|
+
github_filename=self.GITHUB_ARCHIVE_FILENAME,
|
|
227
|
+
github_raw_url=generate_github_archive_url(self.GITHUB_REPO),
|
|
228
|
+
local_filepath=f"{cache_directory}/{self.GITHUB_ARCHIVE_FILENAME}",
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
def _prepare_all_countries_cache(
|
|
232
|
+
self, cache_directory: str
|
|
233
|
+
) -> list[OcdIdsExtractorConfig]:
|
|
234
|
+
"""Downloads the repo archive if needed and extracts country CSVs."""
|
|
235
|
+
archive_config = self._generate_archive_config(cache_directory)
|
|
236
|
+
need_download = self._check_cache_freshness([archive_config])
|
|
237
|
+
if need_download:
|
|
238
|
+
self._download_data(need_download)
|
|
239
|
+
return self._extract_archive(
|
|
240
|
+
archive_config.local_filepath,
|
|
241
|
+
cache_directory,
|
|
242
|
+
overwrite=bool(need_download),
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
def _extract_archive(
|
|
246
|
+
self,
|
|
247
|
+
archive_filepath: str,
|
|
248
|
+
cache_directory: str,
|
|
249
|
+
overwrite: bool,
|
|
250
|
+
) -> list[OcdIdsExtractorConfig]:
|
|
251
|
+
"""Extracts country-*.csv files from the archive into cache_directory."""
|
|
252
|
+
configs = []
|
|
253
|
+
with zipfile.ZipFile(archive_filepath, mode="r") as archive:
|
|
254
|
+
for member_name in sorted(archive.namelist()):
|
|
255
|
+
match = self._ARCHIVE_IDENTIFIER_PATTERN.match(member_name)
|
|
256
|
+
if not match:
|
|
257
|
+
continue
|
|
258
|
+
country_code = match.group("country_code")
|
|
259
|
+
github_filename = generate_country_filename(country_code)
|
|
260
|
+
local_filepath = generate_absolute_path(cache_directory, country_code)
|
|
261
|
+
if overwrite or not self._check_for_cached_file(local_filepath):
|
|
262
|
+
local_tmp_filepath = f"{local_filepath}.tmp"
|
|
263
|
+
with archive.open(member_name, mode="r") as src, io.open(
|
|
264
|
+
local_tmp_filepath, mode="wb"
|
|
265
|
+
) as dst:
|
|
266
|
+
shutil.copyfileobj(src, dst)
|
|
267
|
+
shutil.copy(local_tmp_filepath, local_filepath)
|
|
268
|
+
os.remove(local_tmp_filepath)
|
|
269
|
+
configs.append(
|
|
270
|
+
self.OcdIdsExtractorConfig(
|
|
271
|
+
country_code=country_code,
|
|
272
|
+
github_filename=github_filename,
|
|
273
|
+
github_raw_url=generate_github_raw_url(
|
|
274
|
+
self.GITHUB_REPO, self.GITHUB_DIR, github_filename
|
|
275
|
+
),
|
|
276
|
+
local_filepath=local_filepath,
|
|
277
|
+
)
|
|
278
|
+
)
|
|
279
|
+
return configs
|
|
280
|
+
|
|
205
281
|
def _create_cache(self, cache_directory: str) -> None:
|
|
206
282
|
"""Creates a cache directory if it doesn't exist."""
|
|
207
283
|
if not os.path.exists(cache_directory):
|
|
@@ -284,14 +360,15 @@ class OcdIdsExtractor(object):
|
|
|
284
360
|
return configs
|
|
285
361
|
|
|
286
362
|
def _download_data(self, configs: list[OcdIdsExtractorConfig]) -> None:
|
|
287
|
-
"""Download OCD ID files from
|
|
363
|
+
"""Download OCD ID files from GitHub."""
|
|
288
364
|
|
|
289
|
-
# Download the file(s) from
|
|
365
|
+
# Download the file(s) from GitHub.
|
|
290
366
|
for config in configs:
|
|
291
|
-
local_tmp_filepath = "{
|
|
367
|
+
local_tmp_filepath = f"{config.local_filepath}.tmp"
|
|
292
368
|
r = requests.get(config.github_raw_url)
|
|
369
|
+
r.raise_for_status()
|
|
293
370
|
with io.open(local_tmp_filepath, mode="wb") as fd:
|
|
294
|
-
for chunk in r.iter_content():
|
|
371
|
+
for chunk in r.iter_content(chunk_size=self._DOWNLOAD_CHUNK_SIZE):
|
|
295
372
|
fd.write(chunk)
|
|
296
373
|
shutil.copy(local_tmp_filepath, config.local_filepath)
|
|
297
374
|
os.remove(local_tmp_filepath)
|
|
@@ -19,6 +19,7 @@ import collections
|
|
|
19
19
|
import re
|
|
20
20
|
|
|
21
21
|
from civics_cdf_validator import base
|
|
22
|
+
from civics_cdf_validator import gpunit_rules
|
|
22
23
|
from civics_cdf_validator import loggers
|
|
23
24
|
from civics_cdf_validator import office_utils
|
|
24
25
|
from civics_cdf_validator.rulesets import rule_util
|
|
@@ -143,7 +144,7 @@ class Schema(base.TreeRule):
|
|
|
143
144
|
try:
|
|
144
145
|
schema = etree.XMLSchema(etree=self.schema_tree)
|
|
145
146
|
except etree.XMLSchemaParseError as e:
|
|
146
|
-
raise loggers.
|
|
147
|
+
raise loggers.ElectionFatal.from_message(
|
|
147
148
|
"The schema file could not be parsed correctly %s" % str(e)
|
|
148
149
|
)
|
|
149
150
|
valid_xml = True
|
|
@@ -161,9 +162,21 @@ class Schema(base.TreeRule):
|
|
|
161
162
|
"The election file didn't validate "
|
|
162
163
|
"against schema : {0}".format(error.message.encode("utf-8"))
|
|
163
164
|
),
|
|
165
|
+
elements=self._get_error_elements(error),
|
|
164
166
|
)
|
|
165
167
|
)
|
|
166
|
-
raise loggers.
|
|
168
|
+
raise loggers.ElectionFatal(errors)
|
|
169
|
+
|
|
170
|
+
def _get_error_elements(self, error):
|
|
171
|
+
"""Returns the elements a schema error points to, or the root element."""
|
|
172
|
+
if error.path:
|
|
173
|
+
try:
|
|
174
|
+
elements = self.election_tree.xpath(error.path)
|
|
175
|
+
except etree.XPathError:
|
|
176
|
+
elements = []
|
|
177
|
+
if elements:
|
|
178
|
+
return elements
|
|
179
|
+
return [self.election_tree.getroot()]
|
|
167
180
|
|
|
168
181
|
|
|
169
182
|
class OptionalAndEmpty(base.BaseRule):
|
|
@@ -198,7 +211,9 @@ class Encoding(base.TreeRule):
|
|
|
198
211
|
def check(self):
|
|
199
212
|
docinfo = self.election_tree.docinfo
|
|
200
213
|
if docinfo.encoding != "UTF-8":
|
|
201
|
-
raise loggers.ElectionError.from_message(
|
|
214
|
+
raise loggers.ElectionError.from_message(
|
|
215
|
+
"Encoding on file is not UTF-8", [self.election_tree.getroot()]
|
|
216
|
+
)
|
|
202
217
|
|
|
203
218
|
|
|
204
219
|
class LanguageCode(base.BaseRule):
|
|
@@ -471,16 +486,18 @@ class DuplicateGpUnits(base.BaseRule):
|
|
|
471
486
|
if composing_gpunits is None or not composing_gpunits.text:
|
|
472
487
|
continue
|
|
473
488
|
composing_ids = frozenset(composing_gpunits.text.split())
|
|
474
|
-
if children.get(composing_ids):
|
|
489
|
+
if children.get(composing_ids) is not None:
|
|
490
|
+
duplicate_gpunit = children[composing_ids]
|
|
475
491
|
error_log.append(
|
|
476
492
|
loggers.LogEntry(
|
|
477
493
|
"GpUnits {} are duplicates".format(
|
|
478
|
-
str((
|
|
479
|
-
)
|
|
494
|
+
str((duplicate_gpunit.get("objectId"), object_id))
|
|
495
|
+
),
|
|
496
|
+
[duplicate_gpunit, gpunit],
|
|
480
497
|
)
|
|
481
498
|
)
|
|
482
499
|
continue
|
|
483
|
-
children[composing_ids] =
|
|
500
|
+
children[composing_ids] = gpunit
|
|
484
501
|
if error_log:
|
|
485
502
|
raise loggers.ElectionError(error_log)
|
|
486
503
|
|
|
@@ -496,13 +513,17 @@ class GpUnitsCyclesRefsValidation(base.TreeRule):
|
|
|
496
513
|
self.visited = {} # Used to store status of the nodes as visited or not.
|
|
497
514
|
self.error_log = []
|
|
498
515
|
self.bad_nodes = []
|
|
516
|
+
self.gpunit_by_object_id = {}
|
|
499
517
|
|
|
500
518
|
def build_tree(self, gpunit):
|
|
501
519
|
# Check if the node is already visited
|
|
502
520
|
if gpunit in self.visited:
|
|
503
521
|
if gpunit not in self.bad_nodes:
|
|
504
522
|
self.error_log.append(
|
|
505
|
-
loggers.LogEntry(
|
|
523
|
+
loggers.LogEntry(
|
|
524
|
+
"Cycle detected at node {0}".format(gpunit),
|
|
525
|
+
[self.gpunit_by_object_id[gpunit]],
|
|
526
|
+
)
|
|
506
527
|
)
|
|
507
528
|
self.bad_nodes.append(gpunit)
|
|
508
529
|
return
|
|
@@ -515,7 +536,8 @@ class GpUnitsCyclesRefsValidation(base.TreeRule):
|
|
|
515
536
|
self.error_log.append(
|
|
516
537
|
loggers.LogEntry(
|
|
517
538
|
"Node {0} is not present in the file as a GpUnit element."
|
|
518
|
-
.format(child_unit)
|
|
539
|
+
.format(child_unit),
|
|
540
|
+
[self.gpunit_by_object_id[gpunit]],
|
|
519
541
|
)
|
|
520
542
|
)
|
|
521
543
|
|
|
@@ -524,6 +546,7 @@ class GpUnitsCyclesRefsValidation(base.TreeRule):
|
|
|
524
546
|
object_id = element.get("objectId")
|
|
525
547
|
if object_id is None:
|
|
526
548
|
continue
|
|
549
|
+
self.gpunit_by_object_id[object_id] = element
|
|
527
550
|
self.edges[object_id] = []
|
|
528
551
|
composing_gp_unit = element.find("ComposingGpUnitIds")
|
|
529
552
|
if composing_gp_unit is None or composing_gp_unit.text is None:
|
|
@@ -702,7 +725,8 @@ class IndependentPartyName(base.BaseRule):
|
|
|
702
725
|
raise loggers.ElectionWarning.from_message(
|
|
703
726
|
f"Party name '{party_name.text}' indicates an independent party."
|
|
704
727
|
" Please use the IsIndependent attribute on the party element to"
|
|
705
|
-
" specify this."
|
|
728
|
+
" specify this.",
|
|
729
|
+
[party],
|
|
706
730
|
)
|
|
707
731
|
|
|
708
732
|
|
|
@@ -715,6 +739,7 @@ class UniqueStableID(base.TreeRule):
|
|
|
715
739
|
def check(self):
|
|
716
740
|
error_log = []
|
|
717
741
|
stable_obj_dict = dict()
|
|
742
|
+
stable_elements_dict = dict()
|
|
718
743
|
for _, element in etree.iterwalk(self.election_tree, events=("end",)):
|
|
719
744
|
stable_ids = rule_util.get_external_id_values(
|
|
720
745
|
element, "stable", search_subelements=False
|
|
@@ -723,6 +748,7 @@ class UniqueStableID(base.TreeRule):
|
|
|
723
748
|
continue
|
|
724
749
|
elem_id = self._get_object_id_or_name(element)
|
|
725
750
|
for stable_id in stable_ids:
|
|
751
|
+
stable_elements_dict.setdefault(stable_id, []).append(element)
|
|
726
752
|
if stable_id in stable_obj_dict.keys():
|
|
727
753
|
stable_obj_dict.get(stable_id).append(elem_id)
|
|
728
754
|
else:
|
|
@@ -734,7 +760,9 @@ class UniqueStableID(base.TreeRule):
|
|
|
734
760
|
error_message = (
|
|
735
761
|
"Stable ID {} is not unique as it is mapped in {}".format(k, v)
|
|
736
762
|
)
|
|
737
|
-
error_log.append(
|
|
763
|
+
error_log.append(
|
|
764
|
+
loggers.LogEntry(error_message, stable_elements_dict[k])
|
|
765
|
+
)
|
|
738
766
|
if error_log:
|
|
739
767
|
raise loggers.ElectionError(error_log)
|
|
740
768
|
|
|
@@ -1760,7 +1788,8 @@ class DeprecatedPartyLeadershipSchema(base.BaseRule):
|
|
|
1760
1788
|
):
|
|
1761
1789
|
raise loggers.ElectionError.from_message(
|
|
1762
1790
|
"Specifying party leadership via external identifiers is deprecated."
|
|
1763
|
-
" Please use the PartyLeadership element instead."
|
|
1791
|
+
" Please use the PartyLeadership element instead.",
|
|
1792
|
+
[element],
|
|
1764
1793
|
)
|
|
1765
1794
|
|
|
1766
1795
|
|
|
@@ -1779,7 +1808,8 @@ class ProhibitElectionData(base.TreeRule):
|
|
|
1779
1808
|
root = self.election_tree.getroot()
|
|
1780
1809
|
if root is not None and root.find("Election") is not None:
|
|
1781
1810
|
raise loggers.ElectionError.from_message(
|
|
1782
|
-
"Election data is prohibited in officeholder feeds."
|
|
1811
|
+
"Election data is prohibited in officeholder feeds.",
|
|
1812
|
+
[root.find("Election")],
|
|
1783
1813
|
)
|
|
1784
1814
|
|
|
1785
1815
|
|
|
@@ -1838,7 +1868,9 @@ class UnreferencedEntitiesBase(base.TreeRule):
|
|
|
1838
1868
|
raise loggers.ElectionWarning.from_message(
|
|
1839
1869
|
f"Element of type {element_name} with object id {obj_id} is not"
|
|
1840
1870
|
" referenced by anything else in the feed. This is only ok if"
|
|
1841
|
-
" there are explicit instructions to include this entity
|
|
1871
|
+
" there are explicit instructions to include this entity"
|
|
1872
|
+
" anyways.",
|
|
1873
|
+
[element],
|
|
1842
1874
|
)
|
|
1843
1875
|
elif (
|
|
1844
1876
|
element_name == "GpUnit"
|
|
@@ -1853,7 +1885,8 @@ class UnreferencedEntitiesBase(base.TreeRule):
|
|
|
1853
1885
|
else:
|
|
1854
1886
|
raise loggers.ElectionError.from_message(
|
|
1855
1887
|
f"Element of type {element_name} with object id {obj_id} is not"
|
|
1856
|
-
" referenced by anything else in the feed."
|
|
1888
|
+
" referenced by anything else in the feed.",
|
|
1889
|
+
[element],
|
|
1857
1890
|
)
|
|
1858
1891
|
|
|
1859
1892
|
|
|
@@ -1977,6 +2010,182 @@ class DuplicatedPartyName(ValidatePartyCollection):
|
|
|
1977
2010
|
return info_log
|
|
1978
2011
|
|
|
1979
2012
|
|
|
2013
|
+
class MissingPartyAbbreviationTranslation(ValidatePartyCollection):
|
|
2014
|
+
"""Every party's abbreviation should be translated to the same languages.
|
|
2015
|
+
|
|
2016
|
+
If a party is missing a name translation, it should be picked up within this
|
|
2017
|
+
class and returned to the user as an Info message.
|
|
2018
|
+
"""
|
|
2019
|
+
|
|
2020
|
+
def check_specific(self, parties):
|
|
2021
|
+
info_log = []
|
|
2022
|
+
feed_languages = set()
|
|
2023
|
+
feed_party_ids = set()
|
|
2024
|
+
for party in parties:
|
|
2025
|
+
party_object_id = party.get("objectId")
|
|
2026
|
+
abbreviation_element = party.find("InternationalizedAbbreviation")
|
|
2027
|
+
if abbreviation_element is None:
|
|
2028
|
+
info_log.append(
|
|
2029
|
+
loggers.LogEntry(
|
|
2030
|
+
"<Party> does not have <InternationalizedAbbreviation> objects",
|
|
2031
|
+
[party],
|
|
2032
|
+
)
|
|
2033
|
+
)
|
|
2034
|
+
continue
|
|
2035
|
+
party_abbreviations = abbreviation_element.findall("Text")
|
|
2036
|
+
party_languages = set()
|
|
2037
|
+
for party_abbreviation in party_abbreviations:
|
|
2038
|
+
language = party_abbreviation.get("language")
|
|
2039
|
+
if language not in feed_languages:
|
|
2040
|
+
feed_languages.add(language)
|
|
2041
|
+
if feed_party_ids:
|
|
2042
|
+
info_log.append(
|
|
2043
|
+
loggers.LogEntry(
|
|
2044
|
+
"The feed is missing abbreviation translation to"
|
|
2045
|
+
f" {language} for parties : {feed_party_ids}."
|
|
2046
|
+
)
|
|
2047
|
+
)
|
|
2048
|
+
party_languages.add(language)
|
|
2049
|
+
feed_party_ids.add(party_object_id)
|
|
2050
|
+
if len(party_languages) != len(feed_languages):
|
|
2051
|
+
info_log.append(
|
|
2052
|
+
loggers.LogEntry(
|
|
2053
|
+
"The party abbreviation is not translated to all feed"
|
|
2054
|
+
f" languages {feed_languages}. You only did it for the"
|
|
2055
|
+
f" following languages : {party_languages}.",
|
|
2056
|
+
[party],
|
|
2057
|
+
)
|
|
2058
|
+
)
|
|
2059
|
+
return info_log
|
|
2060
|
+
|
|
2061
|
+
|
|
2062
|
+
class MissingPartyNameTranslation(ValidatePartyCollection):
|
|
2063
|
+
"""All Parties should have their name translated to the same languages.
|
|
2064
|
+
|
|
2065
|
+
If there is a party name that is not translated to all the feed languages,
|
|
2066
|
+
the party should be picked up within this class and returned to the user as
|
|
2067
|
+
an Info message.
|
|
2068
|
+
"""
|
|
2069
|
+
|
|
2070
|
+
def check_specific(self, parties):
|
|
2071
|
+
info_log = []
|
|
2072
|
+
feed_languages = set()
|
|
2073
|
+
feed_party_ids = set()
|
|
2074
|
+
for party in parties:
|
|
2075
|
+
party_object_id = party.get("objectId")
|
|
2076
|
+
name_element = party.find("Name")
|
|
2077
|
+
if name_element is None:
|
|
2078
|
+
info_log.append(
|
|
2079
|
+
loggers.LogEntry("<Party> does not have <Name> objects", [party])
|
|
2080
|
+
)
|
|
2081
|
+
continue
|
|
2082
|
+
party_names = name_element.findall("Text")
|
|
2083
|
+
party_languages = set()
|
|
2084
|
+
for party_name in party_names:
|
|
2085
|
+
language = party_name.get("language")
|
|
2086
|
+
if language not in feed_languages:
|
|
2087
|
+
feed_languages.add(language)
|
|
2088
|
+
if feed_party_ids:
|
|
2089
|
+
info_log.append(
|
|
2090
|
+
loggers.LogEntry(
|
|
2091
|
+
f"The feed is missing names translation to {language} for"
|
|
2092
|
+
f" parties : {feed_party_ids}."
|
|
2093
|
+
)
|
|
2094
|
+
)
|
|
2095
|
+
party_languages.add(language)
|
|
2096
|
+
feed_party_ids.add(party_object_id)
|
|
2097
|
+
if len(party_languages) != len(feed_languages):
|
|
2098
|
+
info_log.append(
|
|
2099
|
+
loggers.LogEntry(
|
|
2100
|
+
"The party name is not translated to all feed languages"
|
|
2101
|
+
f" {feed_languages}. You did it only for the following"
|
|
2102
|
+
f" languages : {party_languages}.",
|
|
2103
|
+
[party],
|
|
2104
|
+
)
|
|
2105
|
+
)
|
|
2106
|
+
return info_log
|
|
2107
|
+
|
|
2108
|
+
|
|
2109
|
+
class DateOfBirthIsInPast(base.DateRule):
|
|
2110
|
+
"""Date of Birth should not be in the future."""
|
|
2111
|
+
|
|
2112
|
+
def elements(self):
|
|
2113
|
+
return ["Person"]
|
|
2114
|
+
|
|
2115
|
+
def check(self, element):
|
|
2116
|
+
self.reset_instance_vars()
|
|
2117
|
+
date_of_birth_element = element.find("DateOfBirth")
|
|
2118
|
+
if date_of_birth_element is not None and date_of_birth_element.text:
|
|
2119
|
+
date_of_birth = base.PartialDate.init_partial_date(
|
|
2120
|
+
date_of_birth_element.text
|
|
2121
|
+
)
|
|
2122
|
+
self.check_for_date_in_past(date_of_birth, element)
|
|
2123
|
+
if self.error_log:
|
|
2124
|
+
raise loggers.ElectionError(self.error_log)
|
|
2125
|
+
|
|
2126
|
+
|
|
2127
|
+
class GpUnitsHaveSingleRoot(base.TreeRule):
|
|
2128
|
+
"""Ensure that GpUnits form a single-rooted tree."""
|
|
2129
|
+
|
|
2130
|
+
def __init__(self, election_tree, schema_tree, **kwargs):
|
|
2131
|
+
super(GpUnitsHaveSingleRoot, self).__init__(
|
|
2132
|
+
election_tree, schema_tree, **kwargs
|
|
2133
|
+
)
|
|
2134
|
+
self.error_log = []
|
|
2135
|
+
|
|
2136
|
+
def check(self):
|
|
2137
|
+
# Make sure there's at most one GpUnit as a root.
|
|
2138
|
+
# The root is defined as having ComposingGpUnitIds but
|
|
2139
|
+
# is not in the ComposingGpUnitIds of any other GpUnit.
|
|
2140
|
+
|
|
2141
|
+
gpunit_ids = dict()
|
|
2142
|
+
composing_gpunits = set()
|
|
2143
|
+
for element in self.get_elements_by_class(self.election_tree, "GpUnit"):
|
|
2144
|
+
object_id = element.get("objectId")
|
|
2145
|
+
if object_id is not None:
|
|
2146
|
+
gpunit_ids[object_id] = element
|
|
2147
|
+
composing_gpunit = element.find("ComposingGpUnitIds")
|
|
2148
|
+
if composing_gpunit is not None and composing_gpunit.text is not None:
|
|
2149
|
+
composing_gpunits.update(composing_gpunit.text.split())
|
|
2150
|
+
|
|
2151
|
+
roots = gpunit_ids.keys() - composing_gpunits
|
|
2152
|
+
|
|
2153
|
+
if not roots:
|
|
2154
|
+
self.error_log.append(
|
|
2155
|
+
loggers.LogEntry(
|
|
2156
|
+
"GpUnits have no geo district root. "
|
|
2157
|
+
"There should be one or more root geo district.",
|
|
2158
|
+
[self._get_gpunit_collection()],
|
|
2159
|
+
)
|
|
2160
|
+
)
|
|
2161
|
+
else:
|
|
2162
|
+
for object_id in roots:
|
|
2163
|
+
element = gpunit_ids.get(object_id)
|
|
2164
|
+
ocd_ids = rule_util.get_external_id_values(element, "ocd-id")
|
|
2165
|
+
for ocd_id in ocd_ids:
|
|
2166
|
+
if not gpunit_rules.GpUnitOcdIdValidator.is_country_or_region_ocd_id(
|
|
2167
|
+
ocd_id
|
|
2168
|
+
):
|
|
2169
|
+
self.error_log.append(
|
|
2170
|
+
loggers.LogEntry(
|
|
2171
|
+
"GpUnits tree roots needs to be either a country or the EU"
|
|
2172
|
+
f" region, please check the value {ocd_id}.",
|
|
2173
|
+
[element],
|
|
2174
|
+
)
|
|
2175
|
+
)
|
|
2176
|
+
|
|
2177
|
+
if self.error_log:
|
|
2178
|
+
raise loggers.ElectionError(self.error_log)
|
|
2179
|
+
|
|
2180
|
+
def _get_gpunit_collection(self):
|
|
2181
|
+
"""Returns the GpUnitCollection element, or the root if there is none."""
|
|
2182
|
+
root = self.election_tree.getroot()
|
|
2183
|
+
gpunit_collection = root.find("GpUnitCollection")
|
|
2184
|
+
if gpunit_collection is None:
|
|
2185
|
+
return root
|
|
2186
|
+
return gpunit_collection
|
|
2187
|
+
|
|
2188
|
+
|
|
1980
2189
|
COMMON_RULES = (
|
|
1981
2190
|
# go/keep-sorted start
|
|
1982
2191
|
AllCaps,
|
|
@@ -1985,6 +2194,7 @@ COMMON_RULES = (
|
|
|
1985
2194
|
BadCharactersInPersonFullName,
|
|
1986
2195
|
CandidateImageInContactInformation,
|
|
1987
2196
|
CoalitionParties,
|
|
2197
|
+
DateOfBirthIsInPast,
|
|
1988
2198
|
DeprecatedPartyLeadershipSchema,
|
|
1989
2199
|
DiplomaticOfficesHaveCorrespondingGpUnitsSet,
|
|
1990
2200
|
DuplicateGpUnits,
|
|
@@ -1999,12 +2209,15 @@ COMMON_RULES = (
|
|
|
1999
2209
|
GpUnitOcdId,
|
|
2000
2210
|
GpUnitsCyclesRefsValidation,
|
|
2001
2211
|
GpUnitsHaveInternationalizedName,
|
|
2212
|
+
GpUnitsHaveSingleRoot,
|
|
2002
2213
|
IndependentPartyName,
|
|
2003
2214
|
LanguageCode,
|
|
2004
2215
|
MissingFieldsError,
|
|
2005
2216
|
MissingFieldsInfo,
|
|
2006
2217
|
MissingFieldsWarning,
|
|
2007
2218
|
MissingOfficeSelectionMethod,
|
|
2219
|
+
MissingPartyAbbreviationTranslation,
|
|
2220
|
+
MissingPartyNameTranslation,
|
|
2008
2221
|
MissingStableIds,
|
|
2009
2222
|
MultipleInternationalizedTextWithSameLanguageCode,
|
|
2010
2223
|
NonExecutiveOfficeShouldHaveGovernmentBody,
|