civics-cdf-validator 1.68.dev15__tar.gz → 1.68.dev17__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.
Files changed (32) hide show
  1. {civics_cdf_validator-1.68.dev15/civics_cdf_validator.egg-info → civics_cdf_validator-1.68.dev17}/PKG-INFO +1 -1
  2. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17/civics_cdf_validator.egg-info}/PKG-INFO +1 -1
  3. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/gpunit_rules.py +19 -9
  4. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/loggers.py +47 -22
  5. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/rulesets/committee_rules.py +11 -10
  6. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/rulesets/common_rules.py +121 -119
  7. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/rulesets/election_dates_rules.py +7 -1
  8. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/rulesets/election_results_rules.py +2 -1
  9. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/rulesets/metadata_rules.py +43 -46
  10. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/rulesets/rule_util.py +19 -9
  11. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/rulesets/voter_information_rules.py +11 -10
  12. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/stats.py +16 -15
  13. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/version.py +1 -1
  14. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/CONTRIBUTING.md +0 -0
  15. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/LICENSE-2.0.txt +0 -0
  16. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/MANIFEST.in +0 -0
  17. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/README.md +0 -0
  18. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/__init__.py +0 -0
  19. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/base.py +0 -0
  20. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/civics_cdf_validator.egg-info/SOURCES.txt +0 -0
  21. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/civics_cdf_validator.egg-info/dependency_links.txt +0 -0
  22. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/civics_cdf_validator.egg-info/entry_points.txt +0 -0
  23. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/civics_cdf_validator.egg-info/requires.txt +0 -0
  24. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/civics_cdf_validator.egg-info/top_level.txt +0 -0
  25. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/office_utils.py +0 -0
  26. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/rules.py +0 -0
  27. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/rulesets/__init__.py +0 -0
  28. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/rulesets/election_rules.py +0 -0
  29. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/rulesets/officeholder_rules.py +0 -0
  30. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/setup.cfg +0 -0
  31. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/setup.py +0 -0
  32. {civics_cdf_validator-1.68.dev15 → civics_cdf_validator-1.68.dev17}/validator.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: civics_cdf_validator
3
- Version: 1.68.dev15
3
+ Version: 1.68.dev17
4
4
  Summary: Checks if an election feed follows best practices
5
5
  Home-page: https://github.com/google/civics_cdf_validator
6
6
  Author: Google Civics
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: civics_cdf_validator
3
- Version: 1.68.dev15
3
+ Version: 1.68.dev17
4
4
  Summary: Checks if an election feed follows best practices
5
5
  Home-page: https://github.com/google/civics_cdf_validator
6
6
  Author: Google Civics
@@ -15,6 +15,7 @@
15
15
 
16
16
  from __future__ import print_function
17
17
 
18
+ from collections.abc import Iterable
18
19
  import csv
19
20
  import datetime
20
21
  import io
@@ -28,19 +29,21 @@ import pycountry
28
29
  import requests
29
30
 
30
31
 
31
- def generate_github_raw_url(repo_name, subdir, file_name, branch="master"):
32
+ def generate_github_raw_url(
33
+ repo_name: str, subdir: str, file_name: str, branch: str = "master"
34
+ ) -> str:
32
35
  return f"https://raw.github.com/{repo_name}/{branch}/{subdir}/{file_name}"
33
36
 
34
37
 
35
- def generate_github_archive_url(repo_name, branch="master"):
38
+ def generate_github_archive_url(repo_name: str, branch: str = "master") -> str:
36
39
  return f"https://github.com/{repo_name}/archive/refs/heads/{branch}.zip"
37
40
 
38
41
 
39
- def generate_country_filename(country_code):
42
+ def generate_country_filename(country_code: str) -> str:
40
43
  return f"country-{country_code}.csv"
41
44
 
42
45
 
43
- def generate_absolute_path(path_to_directory, country_code):
46
+ def generate_absolute_path(path_to_directory: str, country_code: str) -> str:
44
47
  return f"{path_to_directory}/{generate_country_filename(country_code)}"
45
48
 
46
49
 
@@ -56,7 +59,12 @@ class GpUnitOcdIdValidator(object):
56
59
  OCD_PATTERN_ROOT = r"^ocd-division\/(country:[a-z]{2}|region:eu)$"
57
60
  OCD_MATCHER_ROOT = re.compile(OCD_PATTERN_ROOT, flags=re.U)
58
61
 
59
- def __init__(self, country_code=None, local_file=None, ocd_id_list=None):
62
+ def __init__(
63
+ self,
64
+ country_code: str | None = None,
65
+ local_file: str | None = None,
66
+ ocd_id_list: Iterable[str] | None = None,
67
+ ) -> None:
60
68
  """Initialize the class.
61
69
 
62
70
  If a list is provided, that list will be used to initialize the set of OCD
@@ -76,7 +84,7 @@ class GpUnitOcdIdValidator(object):
76
84
  extractor = OcdIdsExtractor(country_code, local_file)
77
85
  self.ocd_ids = extractor.extract()
78
86
 
79
- def is_valid_ocd_id(self, ocd_id):
87
+ def is_valid_ocd_id(self, ocd_id: str) -> bool | None:
80
88
  """Check whether the given OCD ID is valid.
81
89
 
82
90
  An OCD ID is valid if:
@@ -98,7 +106,7 @@ class GpUnitOcdIdValidator(object):
98
106
  )
99
107
 
100
108
  @classmethod
101
- def is_valid_country_code(cls, ocd_id):
109
+ def is_valid_country_code(cls, ocd_id: str) -> bool:
102
110
  """Check whether country code in the given OCD ID is valid."""
103
111
  match_object = re.match(cls.OCD_PATTERN, ocd_id)
104
112
  if match_object is None:
@@ -113,7 +121,7 @@ class GpUnitOcdIdValidator(object):
113
121
  return False
114
122
 
115
123
  @classmethod
116
- def is_country_or_region_ocd_id(cls, ocd_id):
124
+ def is_country_or_region_ocd_id(cls, ocd_id: str) -> re.Match[str] | None:
117
125
  """Check whether the given OCD ID represents a country or region."""
118
126
  return cls.OCD_MATCHER_ROOT.match(str(ocd_id))
119
127
 
@@ -171,7 +179,9 @@ class OcdIdsExtractor(object):
171
179
  github_raw_url: str
172
180
  local_filepath: str
173
181
 
174
- def __init__(self, country_code=None, local_file=None):
182
+ def __init__(
183
+ self, country_code: str | None = None, local_file: str | None = None
184
+ ) -> None:
175
185
  self.country_code = country_code
176
186
  self.local_file = local_file
177
187
 
@@ -14,13 +14,22 @@ See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  """
16
16
 
17
+ from collections.abc import Sequence
17
18
  import copy
19
+ from typing import Self
20
+
21
+ from lxml import etree
18
22
 
19
23
 
20
24
  class LogEntry(object):
21
25
  """This class contains needed information for user output."""
22
26
 
23
- def __init__(self, message, elements=None, lines=None):
27
+ def __init__(
28
+ self,
29
+ message: str,
30
+ elements: Sequence[etree._Element | None] | None = None,
31
+ lines: Sequence[int] | None = None,
32
+ ) -> None:
24
33
  self.message = message
25
34
  self.elements = elements
26
35
  if lines is not None:
@@ -40,7 +49,7 @@ class ElectionException(Exception):
40
49
  """Base class for all the exceptions in this script."""
41
50
  description = None
42
51
 
43
- def __init__(self, log_entry):
52
+ def __init__(self, log_entry: LogEntry | list[LogEntry]) -> None:
44
53
  super(ElectionException, self).__init__()
45
54
  if isinstance(log_entry, list):
46
55
  self.log_entry = log_entry
@@ -48,7 +57,12 @@ class ElectionException(Exception):
48
57
  self.log_entry = [log_entry]
49
58
 
50
59
  @classmethod
51
- def from_message(cls, message, elements=None, lines=None):
60
+ def from_message(
61
+ cls,
62
+ message: str,
63
+ elements: Sequence[etree._Element | None] | None = None,
64
+ lines: Sequence[int] | None = None,
65
+ ) -> Self:
52
66
  return cls(LogEntry(message, elements, lines))
53
67
 
54
68
 
@@ -82,7 +96,7 @@ class ElectionInfo(ElectionException):
82
96
  description = "Info"
83
97
 
84
98
 
85
- def get_parent_hierarchy_object_id_str(elt):
99
+ def get_parent_hierarchy_object_id_str(elt: etree._Element | None) -> str:
86
100
  """Get the elt path from the 1st parent with an objectId / ElectionReport."""
87
101
  elt_hierarchy = []
88
102
  current_elt = elt
@@ -112,12 +126,14 @@ class ExceptionListWrapper:
112
126
  ElectionError,
113
127
  ElectionFatal)
114
128
 
115
- def __init__(self):
129
+ def __init__(self) -> None:
116
130
  self._rules_exc_logs = dict()
117
131
  for e_type in self.SUPPORTED_SEVERITIES:
118
132
  self._rules_exc_logs[e_type] = dict()
119
133
 
120
- def exception_handler(self, exception, rule_name="No rule"):
134
+ def exception_handler(
135
+ self, exception: ElectionException, rule_name: str = "No rule"
136
+ ) -> None:
121
137
  """Gather log entry counts by type, class, and total."""
122
138
  for e_type in self._rules_exc_logs:
123
139
  if issubclass(exception.__class__, e_type):
@@ -125,47 +141,56 @@ class ExceptionListWrapper:
125
141
  self._rules_exc_logs[e_type][rule_name] = []
126
142
  self._rules_exc_logs[e_type][rule_name].extend(exception.log_entry)
127
143
 
128
- def _select_exception_types(self, severity):
144
+ def _select_exception_types(
145
+ self, severity: int | None
146
+ ) -> tuple[type[ElectionException], ...]:
129
147
  if not severity:
130
148
  severity = 0
131
149
  elif severity > len(self.SUPPORTED_SEVERITIES):
132
150
  severity = len(self.SUPPORTED_SEVERITIES) - 1
133
151
  return self.SUPPORTED_SEVERITIES[severity:]
134
152
 
135
- def count_logs_with_exception_type(self, e_type):
153
+ def count_logs_with_exception_type(
154
+ self, e_type: type[ElectionException]
155
+ ) -> int:
136
156
  exception_count = 0
137
157
  for log_list in list(self._rules_exc_logs[e_type].values()):
138
158
  exception_count += len(log_list)
139
159
  return exception_count
140
160
 
141
- def _print_exception_type_summary(self, e_type, count_exception):
161
+ def _print_exception_type_summary(
162
+ self, e_type: type[ElectionException], count_exception: int
163
+ ) -> None:
142
164
  suffix = ""
143
165
  if count_exception > 1:
144
166
  suffix = "s"
145
- print("{0:6d} {1} message{2} found".format(count_exception,
146
- e_type.description, suffix))
167
+ print(f"{count_exception:6d} {e_type.description} message{suffix} found")
147
168
 
148
- def _print_rule_exception_summary(self, e_type, rule_name):
169
+ def _print_rule_exception_summary(
170
+ self, e_type: type[ElectionException], rule_name: str
171
+ ) -> None:
149
172
  rule_count = len(self._rules_exc_logs[e_type][rule_name])
150
173
  rule_suffix = ""
151
174
  if rule_count > 1:
152
175
  rule_suffix = "s"
153
- print("{0:10d} {1} {2} message{3}".format(rule_count, rule_name,
154
- e_type.description, rule_suffix))
176
+ print(f"{rule_count:10d} {rule_name} {e_type.description} "
177
+ f"message{rule_suffix}")
155
178
 
156
- def _print_exception_log(self, log_entry):
179
+ def _print_exception_log(self, log_entry: LogEntry) -> None:
157
180
  if log_entry.lines is not None:
158
- print(" " * 14 + "Lines %s :" % log_entry.lines)
181
+ print(" " * 14 + f"Lines {log_entry.lines} :")
159
182
  if log_entry.elements is not None:
160
183
  print(" " * 15 + "Affected elements :")
161
184
  for element in log_entry.elements:
162
185
  print(" " * 16 + get_parent_hierarchy_object_id_str(element))
163
- print(" " * 18 + "* {0}".format(log_entry.message))
186
+ print(" " * 18 + f"* {log_entry.message}")
164
187
 
165
- def get_all_exceptions(self):
188
+ def get_all_exceptions(
189
+ self,
190
+ ) -> dict[type[Exception], dict[str, list[LogEntry]]]:
166
191
  return copy.deepcopy(self._rules_exc_logs)
167
192
 
168
- def print_exceptions(self, severity, verbose):
193
+ def print_exceptions(self, severity: int | None, verbose: bool) -> None:
169
194
  """Print exceptions in decreasing order of severity."""
170
195
  exception_types = self._select_exception_types(severity)
171
196
  for e_type in reversed(exception_types):
@@ -185,11 +210,11 @@ class ExceptionListWrapper:
185
210
  self._print_exception_log(log_entry)
186
211
 
187
212
 
188
- def handled_severities():
213
+ def handled_severities() -> tuple[type[ElectionException], ...]:
189
214
  return ExceptionListWrapper.SUPPORTED_SEVERITIES
190
215
 
191
216
 
192
- def supported_severities_mapping():
217
+ def supported_severities_mapping() -> dict[str, int]:
193
218
  exp_severity_map = {}
194
219
  for n in range(len(ExceptionListWrapper.SUPPORTED_SEVERITIES)):
195
220
  exp_severity_map[
@@ -197,7 +222,7 @@ def supported_severities_mapping():
197
222
  return exp_severity_map
198
223
 
199
224
 
200
- def severities_names():
225
+ def severities_names() -> list[str]:
201
226
  names = []
202
227
  for exp in ExceptionListWrapper.SUPPORTED_SEVERITIES:
203
228
  names.append(exp.description.lower())
@@ -21,15 +21,16 @@ from civics_cdf_validator import base
21
21
  from civics_cdf_validator import loggers
22
22
  from civics_cdf_validator.rulesets import common_rules
23
23
  from civics_cdf_validator.rulesets import rule_util
24
+ from lxml import etree
24
25
 
25
26
 
26
27
  class CommitteeClassificationEndDateOccursAfterStartDate(base.DateRule):
27
28
  """CommitteeClassification elements should have end dates that occur after the start dates."""
28
29
 
29
- def elements(self):
30
+ def elements(self) -> list[str]:
30
31
  return ["CommitteeClassification"]
31
32
 
32
- def check(self, element):
33
+ def check(self, element: etree._Element) -> None:
33
34
  self.reset_instance_vars()
34
35
  self.gather_dates(element)
35
36
 
@@ -42,10 +43,10 @@ class CommitteeClassificationEndDateOccursAfterStartDate(base.DateRule):
42
43
  class AffiliationEndDateOccursAfterStartDate(base.DateRule):
43
44
  """Affiliation elements should have end dates that occur after the start dates."""
44
45
 
45
- def elements(self):
46
+ def elements(self) -> list[str]:
46
47
  return ["Affiliation"]
47
48
 
48
- def check(self, element):
49
+ def check(self, element: etree._Element) -> None:
49
50
  self.reset_instance_vars()
50
51
  self.gather_dates(element)
51
52
 
@@ -58,15 +59,15 @@ class AffiliationEndDateOccursAfterStartDate(base.DateRule):
58
59
  class EinMatchesFormat(base.BaseRule):
59
60
  """EIN id should be in the following format: XX-XXXXXXX."""
60
61
 
61
- def __init__(self, *args, **kwargs):
62
+ def __init__(self, *args, **kwargs) -> None:
62
63
  super(EinMatchesFormat, self).__init__(*args, **kwargs)
63
64
  regex = r"\d{2}(-\d{7})?"
64
65
  self.ein_id_matcher = re.compile(regex, flags=re.U)
65
66
 
66
- def elements(self):
67
+ def elements(self) -> list[str]:
67
68
  return ["Committee"]
68
69
 
69
- def check(self, element):
70
+ def check(self, element: etree._Element) -> None:
70
71
  external_identifiers = element.find("ExternalIdentifiers")
71
72
  if external_identifiers is not None:
72
73
  ein_ids = rule_util.get_external_id_values(external_identifiers, "ein")
@@ -74,7 +75,7 @@ class EinMatchesFormat(base.BaseRule):
74
75
  e_id = ein_ids.pop()
75
76
  if not self.ein_id_matcher.match(e_id):
76
77
  raise loggers.ElectionError.from_message(
77
- "EIN id '{}' is not in the correct format.".format(e_id),
78
+ f"EIN id '{e_id}' is not in the correct format.",
78
79
  [element],
79
80
  )
80
81
 
@@ -82,10 +83,10 @@ class EinMatchesFormat(base.BaseRule):
82
83
  class AffiliationHasEitherPartyOrPerson(base.BaseRule):
83
84
  """Affiliation should contain either a Party or a Person objectId."""
84
85
 
85
- def elements(self):
86
+ def elements(self) -> list[str]:
86
87
  return ["Affiliation"]
87
88
 
88
- def check(self, element):
89
+ def check(self, element: etree._Element) -> None:
89
90
  party_id = element.find("PartyId")
90
91
  person_id = element.find("PersonId")
91
92
  if not ((party_id is None) ^ (person_id is None)):