forked from google/civics_cdf_validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrules.py
executable file
·1572 lines (1323 loc) · 55.9 KB
/
rules.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Validation rules for the NIST CDF XML validator."""
from __future__ import print_function
import csv
from datetime import datetime
import hashlib
import io
import os
import re
import shutil
import sys
from election_results_xml_validator import base
import enum
import github
import language_tags
from lxml import etree
import requests
from six.moves.urllib.parse import urlparse
_PARTY_LEADERSHIP_TYPES = ["party-leader-id", "party-chair-id"]
def sourceline_prefix(element):
if hasattr(element, "sourceline") and element.sourceline is not None:
return "Line %d. " % element.sourceline
else:
return ""
class Schema(base.TreeRule):
"""Checks if election file validates against the provided schema."""
def check(self):
schema_tree = etree.parse(self.schema_file)
try:
schema = etree.XMLSchema(etree=schema_tree)
except etree.XMLSchemaParseError as e:
raise base.ElectionError(
"The schema file could not be parsed correctly %s" % str(e))
valid_xml = True
try:
schema.assertValid(self.election_tree)
except etree.DocumentInvalid as e:
valid_xml = False
if not valid_xml:
errors = []
for error in schema.error_log:
errors.append(
base.ErrorLogEntry(error.line, error.message.encode("utf-8")))
raise base.ElectionTreeError(
"The election file didn't validate against schema.", errors)
class OptionalAndEmpty(base.BaseRule):
"""Checks for optional and empty fields."""
def __init__(self, election_tree, schema_file):
super(OptionalAndEmpty, self).__init__(election_tree, schema_file)
self.previous = None
def elements(self):
schema_tree = etree.parse(self.schema_file)
eligible_elements = []
for event, element in etree.iterwalk(schema_tree):
tag = self.strip_schema_ns(element)
if tag and tag == "element" and element.get("minOccurs") == "0":
eligible_elements.append(element.get("name"))
return eligible_elements
def check(self, element):
if element == self.previous:
return
self.previous = element
if ((element.text is None or element.text.strip() == "") and
not len(element)):
raise base.ElectionWarning(
"Line %d. %s optional element included although it "
"is empty." % (element.sourceline, element.tag))
class Encoding(base.TreeRule):
"""Checks that the file provided uses UTF-8 encoding."""
def check(self):
docinfo = self.election_tree.docinfo
if docinfo.encoding != "UTF-8":
raise base.ElectionError("Encoding on file is not UTF-8")
class HungarianStyleNotation(base.BaseRule):
"""Check that element identifiers use Hungarian style notation.
Hungarian style notation is used to maintain uniqueness and provide context
for the identifiers.
"""
# Add a prefix when there is a specific entity in the xml.
elements_prefix = {
"BallotMeasureContest": "bmc",
"BallotMeasureSelection": "bms",
"BallotStyle": "bs",
"Candidate": "can",
"CandidateContest": "cc",
"CandidateSelection": "cs",
"Coalition": "coa",
"ContactInformation": "ci",
"Hours": "hours",
"Office": "off",
"OfficeGroup": "og",
"Party": "par",
"PartyContest": "pc",
"PartySelection": "ps",
"Person": "per",
"ReportingDevice": "rd",
"ReportingUnit": "ru",
"RetentionContest": "rc",
"Schedule": "sched",
}
def elements(self):
return self.elements_prefix.keys()
def check(self, element):
object_id = element.get("objectId", None)
tag = self.get_element_class(element)
if object_id:
if not object_id.startswith(self.elements_prefix[tag]):
raise base.ElectionInfo(
"Line %d. %s ID %s is not in Hungarian Style Notation. "
"Should start with %s" %
(element.sourceline, tag, object_id, self.elements_prefix[tag]))
class LanguageCode(base.BaseRule):
"""Check that Text elements have a valid language code."""
def elements(self):
return ["Text"]
def check(self, element):
if "language" not in element.attrib:
return
elem_lang = element.get("language")
if (not elem_lang.strip() or not language_tags.tags.check(elem_lang)):
raise base.ElectionError("Line %d. %s is not a valid language code" %
(element.sourceline, elem_lang))
class PercentSum(base.BaseRule):
"""Check that Contest elements have percents summing to 0 or 100."""
def elements(self):
return ["Contest"]
@staticmethod
def fuzzy_equals(a, b, epsilon=1e-6):
return abs(a - b) < epsilon
def check(self, element):
sum_percents = 0.0
for ballot_selection in element.findall("BallotSelection"):
for vote_counts in (
ballot_selection.find("VoteCountsCollection").findall("VoteCounts")):
other_type = vote_counts.find("OtherType")
if other_type is not None and other_type.text == "total-percent":
sum_percents += float(vote_counts.find("Count").text)
if (not PercentSum.fuzzy_equals(sum_percents, 0) and
not PercentSum.fuzzy_equals(sum_percents, 100)):
raise base.ElectionError(
sourceline_prefix(element) +
"Contest percents do not sum to 0 or 100: %f" % sum_percents)
class OnlyOneElection(base.BaseRule):
"""Check that there is only one Election in the ElectionReport."""
def elements(self):
return ["ElectionReport"]
def check(self, element):
if len(element.findall("Election")) > 1:
raise base.ElectionError(
sourceline_prefix(element) +
"ElectionReport has more than one Election.")
class EmptyText(base.BaseRule):
"""Check that Text elements are not strictly whitespace."""
def elements(self):
return ["Text"]
def check(self, element):
if element.text is not None and element.text.strip() == "":
raise base.ElectionWarning("Line %d. %s is empty" %
(element.sourceline, element.tag))
class DuplicateID(base.TreeRule):
"""Check that the file does not contain duplicate object IDs."""
def check(self):
all_object_ids = set()
error_log = []
for event, element in etree.iterwalk(self.election_tree, events=("end",)):
if "objectId" not in element.attrib:
continue
else:
obj_id = element.get("objectId")
if not obj_id:
continue
if obj_id in all_object_ids:
error_line = element.sourceline
error_message = "{0} is a duplicate object ID".format(obj_id)
error_log.append(base.ErrorLogEntry(error_line, error_message))
else:
all_object_ids.add(obj_id)
if error_log:
raise base.ElectionTreeError(
"The Election File contains duplicate object IDs", error_log)
# TODO(kaminer): Refactor this rule to extend ValidReferenceRule
class ValidIDREF(base.BaseRule):
"""Check that IDREFs are valid.
Every field of type IDREF should actually reference a value that exists in a
field of type ID.
"""
def __init__(self, election_tree, schema_file):
super(ValidIDREF, self).__init__(election_tree, schema_file)
self.all_object_ids = set()
for event, element in etree.iterwalk(self.election_tree, events=("end",)):
if "objectId" not in element.attrib:
continue
else:
obj_id = element.get("objectId")
if not obj_id:
continue
else:
self.all_object_ids.add(obj_id)
def elements(self):
schema_tree = etree.parse(self.schema_file)
eligible_elements = []
for event, element in etree.iterwalk(schema_tree):
tag = self.strip_schema_ns(element)
if (tag and tag == "element" and
element.get("type") in ("xs:IDREF", "xs:IDREFS")):
eligible_elements.append(element.get("name"))
return eligible_elements
def check(self, element):
if element.text:
id_references = element.text.split()
for id_ref in id_references:
if id_ref not in self.all_object_ids:
raise base.ElectionError("Line %d. %s is not a valid IDREF." %
(element.sourceline, id_ref))
class ElectoralDistrictOcdId(base.BaseRule):
"""GpUnit referred to by Contest.ElectoralDistrictId MUST have a valid OCD-ID."""
CACHE_DIR = "~/.cache"
GITHUB_REPO = "opencivicdata/ocd-division-ids"
GITHUB_DIR = "identifiers"
OCD_PATTERN_MATCHER = re.compile(r"^ocd-division\/country:(\w|-|_|\.|~)+(\/(\w|-|_)+:(\w|-|_|\.|~)+)*$")
def __init__(self, election_tree, schema_file):
super(ElectoralDistrictOcdId, self).__init__(election_tree, schema_file)
self.gpunits = []
self.check_github = True
self.country_code = None
self.github_file = None
self.github_repo = None
self.local_file = None
for gpunit in self.get_elements_by_class(self.election_tree, "GpUnit"):
self.gpunits.append(gpunit)
def setup(self):
if self.local_file is None:
self.github_file = "country-%s.csv" % self.country_code
self.ocds = self._get_ocd_data()
def _read_csv(self, reader, ocd_id_codes):
"""Reads in OCD IDs from CSV file."""
for row in reader:
if "id" in row and row["id"]:
ocd_id_codes.add(row["id"])
def _get_ocd_data(self):
"""Returns a list of OCD-ID codes.
This list is populated using either a local file or a downloaded file
from GitHub.
"""
# Value `local_file` is not provided by default, only by cmd line arg.
if self.local_file:
countries_file = self.local_file
else:
cache_directory = os.path.expanduser(self.CACHE_DIR)
countries_file = "{0}/{1}".format(cache_directory, self.github_file)
if not os.path.exists(countries_file):
# Only initialize `github_repo` if there's no cached file.
github_api = github.Github()
self.github_repo = github_api.get_repo(self.GITHUB_REPO)
if not os.path.exists(cache_directory):
os.makedirs(cache_directory)
self._download_data(countries_file)
else:
if self.check_github:
last_mod_date = datetime.fromtimestamp(
os.path.getmtime(countries_file))
seconds_since_mod = (datetime.now() - last_mod_date).total_seconds()
# If 1 hour has elapsed, check GitHub for the last file update.
if (seconds_since_mod/3600) > 1:
github_api = github.Github()
self.github_repo = github_api.get_repo(self.GITHUB_REPO)
# Re-download the file if the file on GitHub was updated.
if last_mod_date < self._get_latest_commit_date():
self._download_data(countries_file)
# Update the timestamp to reflect last GitHub check.
os.utime(countries_file, None)
ocd_id_codes = set()
# CSVs read in Python3 must be specified as UTF-8.
if sys.version_info.major < 3:
with open(countries_file) as csvfile:
csv_reader = csv.DictReader(csvfile)
self._read_csv(csv_reader, ocd_id_codes)
else:
with open(countries_file, encoding="utf-8") as csvfile:
csv_reader = csv.DictReader(csvfile)
self._read_csv(csv_reader, ocd_id_codes)
return ocd_id_codes
def _get_latest_commit_date(self):
"""Returns the latest commit date to country-*.csv."""
latest_commit_date = None
latest_commit = self.github_repo.get_commits(
path="{0}/{1}".format(self.GITHUB_DIR, self.github_file))[0]
latest_commit_date = latest_commit.commit.committer.date
return latest_commit_date
def _download_data(self, file_path):
"""Makes a request to Github to download the file."""
ocdid_url = "https://raw.github.com/{0}/master/{1}/{2}".format(
self.GITHUB_REPO, self.GITHUB_DIR, self.github_file)
r = requests.get(ocdid_url)
with io.open("{0}.tmp".format(file_path), "wb") as fd:
for chunk in r.iter_content():
fd.write(chunk)
valid = self._verify_data("{0}.tmp".format(file_path))
if not valid:
raise base.ElectionError(
"Could not successfully download OCD ID data files. "
"Please try downloading the file manually and "
"place it in ~/.cache")
else:
shutil.copy("{0}.tmp".format(file_path), file_path)
def _verify_data(self, file_path):
"""Validates a file's SHA."""
file_sha1 = hashlib.sha1()
file_info = os.stat(file_path)
# GitHub calculates the blob SHA like this:
# sha1("blob "+filesize+"\0"+data)
file_sha1.update(b"blob %d\0" % file_info.st_size)
with io.open(file_path, mode="rb") as fd:
for line in fd:
file_sha1.update(line)
latest_file_sha = self._get_latest_file_blob_sha()
return latest_file_sha == file_sha1.hexdigest()
def _get_latest_file_blob_sha(self):
"""Returns the GitHub blob SHA of country-*.csv."""
blob_sha = None
dir_contents = self.github_repo.get_dir_contents(self.GITHUB_DIR)
for content_file in dir_contents:
if content_file.name == self.github_file:
blob_sha = content_file.sha
break
return blob_sha
def _encode_ocdid_value(self, ocdid):
if sys.version_info.major < 3:
if isinstance(ocdid, unicode):
return ocdid.encode("utf-8")
if isinstance(ocdid, str):
return ocdid
else:
return ""
def elements(self):
return ["ElectoralDistrictId"]
def check(self, element):
if element.getparent().tag != "Contest":
return
contest_id = element.getparent().get("objectId")
if not contest_id:
return
valid_ocd_id = False
referenced_gpunit = None
external_ids = []
for gpunit in self.gpunits:
if gpunit.get("objectId", None) == element.text:
referenced_gpunit = gpunit
external_ids = gpunit.findall(".//ExternalIdentifier")
for extern_id in external_ids:
id_type = extern_id.find("Type")
if id_type is not None and id_type.text == "ocd-id":
value = extern_id.find("Value")
if value is None or not hasattr(value, "text"):
continue
ocd_id = self._encode_ocdid_value(value.text)
valid_ocd_id = ocd_id in self.ocds and self.OCD_PATTERN_MATCHER.fullmatch(ocd_id)
if (id_type is not None and id_type.text != "ocd-id" and
id_type.text.lower() == "ocd-id"):
raise base.ElectionError(
"Line %d. The External Identifier case is incorrect"
". Should be ocd-id and not %s" %
(id_type.sourceline, id_type.text))
if referenced_gpunit is None:
raise base.ElectionError(
"Line %d. The ElectoralDistrictId element for contest %s does "
"not refer to a GpUnit. Every ElectoralDistrictId MUST "
"reference a GpUnit" % (element.sourceline, contest_id))
if referenced_gpunit is not None and not external_ids:
raise base.ElectionError(
"Line %d. The GpUnit %s on line %d referenced by contest %s "
"does not have any external identifiers" %
(element.sourceline, element.text, referenced_gpunit.sourceline,
contest_id))
if not valid_ocd_id and referenced_gpunit is not None:
raise base.ElectionError(
"Line %d. The ElectoralDistrictId element for contest %s "
"refers to GpUnit %s on line %d that does not have a valid OCD "
"ID" % (element.sourceline, contest_id, element.text,
referenced_gpunit.sourceline))
class GpUnitOcdId(ElectoralDistrictOcdId):
"""Any GpUnit that is a geographic district SHOULD have a valid OCD-ID."""
districts = [
"borough", "city", "county", "municipality", "state", "town", "township",
"village"
]
validate_ocd_file = True
def __init__(self, election_tree, schema_file):
super(GpUnitOcdId, self).__init__(election_tree, schema_file)
def elements(self):
return ["ReportingUnit"]
def check(self, element):
gpunit_id = element.get("objectId")
if not gpunit_id:
return
gpunit_type = element.find("Type")
if gpunit_type is not None and gpunit_type.text in self.districts:
for extern_id in element.iter("ExternalIdentifier"):
id_type = extern_id.find("Type")
if id_type is not None and id_type.text == "ocd-id":
value = extern_id.find("Value")
if value is None or not hasattr(value, "text"):
continue
ocd_id = self._encode_ocdid_value(value.text)
if ocd_id not in self.ocds:
raise base.ElectionWarning(
"The OCD ID %s in GpUnit %s defined on line %d is "
"not valid" % (ocd_id, gpunit_id, value.sourceline))
class DuplicateGpUnits(base.TreeRule):
"""Detect GpUnits which are effectively duplicates of each other."""
def __init__(self, election_tree, schema_file):
super(DuplicateGpUnits, self).__init__(election_tree, schema_file)
self.leaf_nodes = set()
self.children = dict()
self.defined_gpunits = set()
def check(self):
root = self.election_tree.getroot()
if root is None:
return
collection = root.find("GpUnitCollection")
if collection is None:
return
self.process_gpunit_collection(collection)
self.find_duplicates()
def process_gpunit_collection(self, collection):
for gpunit in collection:
if "objectId" not in gpunit.attrib:
continue
object_id = gpunit.attrib["objectId"]
self.defined_gpunits.add(object_id)
composing_ids = self.get_composing_gpunits(gpunit)
if composing_ids is None:
self.leaf_nodes.add(object_id)
else:
self.children[object_id] = composing_ids
for gpunit in collection:
self.process_one_gpunit(gpunit)
def find_duplicates(self):
tags = dict()
for object_id in self.children:
if not self.children[object_id]:
continue
sorted_children = " ".join(sorted(self.children[object_id]))
if sorted_children in tags:
tags[sorted_children].append(object_id)
else:
tags[sorted_children] = [object_id]
for tag in tags:
if len(tags[tag]) == 1:
continue
raise base.ElectionError("GpUnits [%s] are duplicates" %
(", ".join(tags[tag])))
def process_one_gpunit(self, gpunit):
"""Define each GpUnit in terms of only nodes with no children."""
if "objectId" not in gpunit.attrib:
return
object_id = gpunit.attrib["objectId"]
if object_id in self.leaf_nodes:
return
composing_ids = self.get_composing_gpunits(gpunit)
visited = set()
while True:
# Iterate over the set of GpUnits which compose this particular
# GpUnit. If any of the children of this node have children
# themselves, replace the child of this node with the set of
# grandchildren. Repeat until the only children of this GpUnit are
# leaf nodes.
non_leaf_nodes = set()
are_leaf_nodes = set()
for composing_id in composing_ids:
if (composing_id in self.leaf_nodes or
composing_id not in self.defined_gpunits):
are_leaf_nodes.add(composing_id)
elif composing_id in self.children:
non_leaf_nodes.add(composing_id)
# If we get here then it means that the composing ID (i.e., the
# GpUnit referenced by the current GpUnit) is not actually
# present in the doc. Since everything is handled by IDREFS this
# means that the schema validation should catch this, and we can
# skip this error.
if not non_leaf_nodes:
self.children[object_id] = are_leaf_nodes
return
for middle_node in non_leaf_nodes:
if middle_node not in self.children:
# TODO: Figure out error
# TODO(kaminer): Confirm whether or not it's possible to remove
# this block. non_leafe_nodes consist exclusively of nodes in
# self.children (see line 565). Checking for them not to be there
# seems like something that will never happen.
print("Non-leaf node {} has no children".format(middle_node))
continue
visited.add(middle_node)
for node in self.children[middle_node]:
if node in visited:
continue
composing_ids.add(node)
composing_ids.remove(middle_node)
def get_composing_gpunits(self, gpunit):
composing = gpunit.find("ComposingGpUnitIds")
if composing is None or composing.text is None:
return None
composing_ids = composing.text.split()
if not composing_ids:
return None
return set(composing_ids)
class GpUnitsTree(base.TreeRule):
"""Ensure that GpUnits form a tree and no cycles are present."""
def __init__(self, election_tree, schema_file):
super(GpUnitsTree, self).__init__(election_tree, schema_file)
self.edges = dict() # Used to maintain the record of connected edges
self.visited = {} # Used to store status of the nodes as - visited ot not.
self.error_log = []
self.bad_nodes = []
def build_tree(self, gpunit):
# Check if the node is already visited
if gpunit in self.visited:
if gpunit not in self.bad_nodes:
error_message = ("Cycle detected at node {0}".format(gpunit))
self.error_log.append(base.ErrorLogEntry(None, error_message))
self.bad_nodes.append(gpunit)
return
self.visited[gpunit] = 1
# Check each composing_gpunit and its edges if any.
for child_unit in self.edges[gpunit]:
if child_unit in self.edges:
self.build_tree(child_unit)
else:
error_message = ("Node {0} is not present in the"
" file as a GpUnit element.".format(child_unit))
self.error_log.append(base.ErrorLogEntry(None, error_message))
def check(self):
for element in self.get_elements_by_class(self.election_tree, "GpUnit"):
object_id = element.get("objectId", None)
if object_id is None:
continue
self.edges[object_id] = []
composing_gp_unit = element.find("ComposingGpUnitIds")
if composing_gp_unit is None or composing_gp_unit.text is None:
continue
composing_gp_unit_ids = composing_gp_unit.text.split()
self.edges[object_id] = composing_gp_unit_ids
for gpunit in self.edges:
self.build_tree(gpunit)
self.visited.clear()
if self.error_log:
raise base.ElectionTreeError("The GpUnits have a cycle.", self.error_log)
class OtherType(base.BaseRule):
"""Elements with an "other" enum should set OtherType.
Elements that have enumerations which include a value named other should
-- when that enumeration value is other -- set the corresponding field
OtherType within the containing element.
"""
def elements(self):
schema_tree = etree.parse(self.schema_file)
eligible_elements = []
for element in schema_tree.iterfind("{%s}complexType" %
self._XSCHEMA_NAMESPACE):
for elem in element.iter():
tag = self.strip_schema_ns(elem)
if tag == "element":
elem_name = elem.get("name", None)
if elem_name and elem_name == "OtherType":
eligible_elements.append(element.get("name"))
return eligible_elements
def check(self, element):
type_element = element.find("Type")
if type_element is not None and type_element.text == "other":
other_type_element = element.find("OtherType")
if other_type_element is None:
raise base.ElectionError(
"Line %d. Type on element %s is set to 'other' but "
"OtherType element is not defined" %
(element.sourceline, element.tag))
class PartisanPrimary(base.BaseRule):
"""Partisan elections should link to the correct political party.
For an Election element of Election type primary, partisan-primary-open,
or partisan-primary-closed, the Contests in that ContestCollection should
have a PrimartyPartyIds that is present and non-empty.
"""
election_type = None
def __init__(self, election_tree, schema_file):
super(PartisanPrimary, self).__init__(election_tree, schema_file)
# There can only be one election element in a file.
election_elem = self.election_tree.find("Election")
if election_elem is not None:
election_type_elem = election_elem.find("Type")
if election_type_elem is not None:
self.election_type = election_type_elem.text.strip()
def elements(self):
# Only check contest elements if this is a partisan election.
if self.election_type and self.election_type in ("primary",
"partisan-primary-open",
"partisan-primary-closed"):
return ["CandidateContest"]
else:
return []
def check(self, element):
primary_party_ids = element.find("PrimaryPartyIds")
if (primary_party_ids is None or not primary_party_ids.text or
not primary_party_ids.text.strip()):
election_elem = self.election_tree.find("Election")
raise base.ElectionError(
"Line %d. Election is of ElectionType %s but PrimaryPartyIds "
"is not present or is empty" %
(election_elem.sourceline, self.election_type))
class PartisanPrimaryHeuristic(PartisanPrimary):
"""Attempts to identify partisan primaries not marked up as such."""
# Add other strings that imply this is a primary contest.
party_text = ["(dem)", "(rep)", "(lib)"]
def elements(self):
if not self.election_type or self.election_type not in (
"primary", "partisan-primary-open", "partisan-primary-closed"):
return ["CandidateContest"]
else:
return []
def check(self, element):
contest_name = element.find("Name")
if contest_name is not None and contest_name.text is not None:
c_name = contest_name.text.replace(" ", "").lower()
for p_text in self.party_text:
if p_text in c_name:
raise base.ElectionWarning(
"Line %d. Name of contest - %s, "
"contains text that implies it is a partisan primary "
"but is not marked up as such." %
(element.sourceline, contest_name.text))
class CoalitionParties(base.TreeRule):
"""Coalitions should always define the Party IDs."""
def check(self):
coalitions = self.get_elements_by_class(self.election_tree, "Coalition")
for coalition in coalitions:
party_id = coalition.find("PartyIds")
if (party_id is None or not party_id.text or not party_id.text.strip()):
raise base.ElectionError(
"Line %d. Coalition %s must define PartyIDs" %
(coalition.sourceline, coalition.get("objectId", None)))
class UniqueLabel(base.BaseRule):
"""Labels should be unique within a file."""
def __init__(self, election_tree, schema_file):
super(UniqueLabel, self).__init__(election_tree, schema_file)
self.labels = set()
def elements(self):
schema_tree = etree.parse(self.schema_file)
eligible_elements = []
for event, element in etree.iterwalk(schema_tree):
tag = self.strip_schema_ns(element)
if tag == "element":
elem_type = element.get("type", None)
if elem_type == "InternationalizedText":
if element.get("name") not in eligible_elements:
eligible_elements.append(element.get("name"))
return eligible_elements
def check(self, element):
element_label = element.get("label", None)
if element_label:
if element_label in self.labels:
raise base.ElectionError(
"Line %d. Duplicate label '%s'. Label already defined" %
(element.sourceline, element_label))
else:
self.labels.add(element_label)
class ReusedCandidate(base.TreeRule):
"""Candidate should be referred to by only one contest.
A Candidate object should only ever be referenced from one contest. If a
Person is running in multiple Contests, then that Person is a Candidate
several times over, but a Candida(te|cy) can't span contests.
"""
def __init__(self, election_tree, schema_file):
super(ReusedCandidate, self).__init__(election_tree, schema_file)
# Mapping of candidates and candidate selections.
self.seen_candidates = {}
def _register_candidates(self):
candidate_selections = self.get_elements_by_class(self.election_tree,
"CandidateSelection")
for candidate_selection in candidate_selections:
candidate_selection_id = candidate_selection.get("objectId", None)
candidate_ids = candidate_selection.find("CandidateIds")
if candidate_ids is None:
continue
for candidate_id in candidate_ids.text.split():
if candidate_selection_id:
self.seen_candidates.setdefault(candidate_id,
[]).append(candidate_selection_id)
def check(self):
self._register_candidates()
error_log = []
for cand_id, cand_select_ids in self.seen_candidates.items():
if len(cand_select_ids) > 1:
error_message = "A Candidate object should only ever be " \
"referenced from one CandidateSelection. Candidate %s is " \
"referenced by the following CandidateSelections :- %s" % (
cand_id, ", ".join(cand_select_ids))
error_log.append(base.ErrorLogEntry(None, error_message))
if error_log:
raise base.ElectionTreeError(
"The Election File contains reused Candidates", error_log)
class ProperBallotSelection(base.BaseRule):
"""BallotSelections should be correct for that type of contest.
Ensure that the BallotSelection elements in a CandidateContest are
CandidateSelections, PartyContests have PartySelections, etc, etc.
"""
con_sel_mapping = {
"BallotMeasureContest": "BallotMeasureSelection",
"CandidateContest": "CandidateSelection",
"PartyContest": "PartySelection",
"RetentionContest": "BallotMeasureSelection"
}
def elements(self):
return self.con_sel_mapping.keys()
def check(self, element):
tag = self.get_element_class(element)
selections = []
for c in self.con_sel_mapping.keys():
selections += self.get_elements_by_class(element, self.con_sel_mapping[c])
for selection in selections:
selection_tag = self.get_element_class(selection)
if (selection_tag != self.con_sel_mapping[tag]):
contest_id = element.get("objectId", None)
selection_id = selection.get("objectId", None)
raise base.ElectionError(
"Line %d. The Contest %s does not contain the right "
"BallotSelection. %s must have a %s but contains a "
"%s, %s" % (element.sourceline, contest_id, tag,
self.con_sel_mapping[tag], selection_tag, selection_id))
# TODO(kaminer): Refactor this rule to extend ValidReferenceRule
class MissingPartyAffiliation(base.TreeRule):
"""Each Person/Candidate PartyId must have an associated Party.
A PartyId that has no Party that references them should be picked up
within this class and returned to the user as an error.
"""
def check(self):
root = self.election_tree.getroot()
if not root.getchildren():
return
check_party_ids = set()
candidate_collection = root.find("CandidateCollection")
if candidate_collection is not None:
for cand in candidate_collection.findall("Candidate"):
party_id = cand.find("PartyId")
if party_id is not None and party_id.text and party_id.text.strip():
check_party_ids.add(party_id.text.strip())
person_collection = root.find("PersonCollection")
if person_collection is not None:
for person in person_collection.findall("Person"):
party_id = person.find("PartyId")
if party_id is not None and party_id.text and party_id.text.strip():
check_party_ids.add(party_id.text.strip())
all_parties = set()
party_collection = root.find("PartyCollection")
if party_collection is not None:
all_parties = set(party.attrib["objectId"] for party
in party_collection if party is not None
and party.get("objectId", None))
missing_parties = check_party_ids - all_parties
if check_party_ids and missing_parties:
raise base.ElectionError("Party elements not found "
"for {}".format(",".join(missing_parties)))
class CandidateNotReferenced(base.TreeRule):
"""Candidate should have AT LEAST one contest they are referred to.
A Candidate object that has no contests attached to them should be picked up
within this class and returned to the user as an error.
"""
def __init__(self, election_tree, schema_file):
super(CandidateNotReferenced, self).__init__(election_tree, schema_file)
self.cand_to_cand_selection = {}
def check(self):
error_log = []
candidates = self.get_elements_by_class(self.election_tree, "Candidate")
for candidate in candidates:
cand_id = candidate.get("objectId", None)
self.cand_to_cand_selection[cand_id] = []
candidate_selections = self.get_elements_by_class(self.election_tree,
"CandidateSelection")
for candidate_selection in candidate_selections:
candidate_selection_id = candidate_selection.get("objectId", None)
candidate_ids = candidate_selection.find("CandidateIds")
# if no candidate ids, skip to the next one
if candidate_ids is None or candidate_selection_id is None:
continue
for candidate_id in candidate_ids.text.split():
self.cand_to_cand_selection.setdefault(
candidate_id, []).append(candidate_selection_id)
for cand_id, cand_select_ids in self.cand_to_cand_selection.items():
if not cand_select_ids:
error_message = "A Candidate object should be referenced from one" \
" CandidateSelection. Candidate {0} is not referenced by any" \
" CandidateSelections".format(cand_id)
error_log.append(base.ErrorLogEntry(None, error_message))
if error_log:
raise base.ElectionTreeError(
"The Election File contains unreferenced Candidates", error_log)
class DuplicateContestNames(base.TreeRule):
"""Check that the file contains unique ContestNames.
Add Warning if duplicate ContestName found.
"""
def check(self):
# Mapping for <Name> and its Contest ObjectId.
name_contest_id = {}
error_log = []
# TODO(kaminer) Avoid the iterwalk by extending BaseRule
# and only checking ContestCollection. Verify with Justin
# Get the object ids associated with each contest name.
for event, element in etree.iterwalk(self.election_tree):
tag = self.strip_schema_ns(element)
if tag != "Contest":
continue
object_id = element.get("objectId", None)
name = element.find("Name")
if name is None or not name.text:
error_message = "Contest {0} is missing a <Name> ".format(object_id)
error_log.append(base.ErrorLogEntry(element.sourceline, error_message))
continue
name_contest_id.setdefault(name.text, []).append(object_id)
for name, contests in name_contest_id.items():
if len(contests) > 1:
error_message = (
"Contest name '{0}' appears in following {1} contests: {2}".format(
name, len(contests), ", ".join(contests)))
error_log.append(base.ErrorLogEntry(None, error_message))
if error_log:
raise base.ElectionTreeError(
"The Election File contains duplicate contest names.", error_log)
class CheckIdentifiers(base.TreeRule):
"""Check that the NIST objects in the feed have an <ExternalIdentifier> block.
Add an error message if the block is missing.
"""
_SKIPPABLE_TYPES = ["contest-stage"]
def check(self):
identifier_values = {}
error_log = []
# TODO(kaminer) Turn this rule into a BaseRule by returning
# this list from elements function.
nist_objects = ("Candidate", "Contest", "Party")
for event, element in etree.iterwalk(self.election_tree):
nist_obj = self.strip_schema_ns(element)