1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Some helper functions |
5
|
|
|
""" |
6
|
|
|
from dataclasses import dataclass, field |
7
|
|
|
|
8
|
|
|
from .constants import GERMAN_PARTIES # type: ignore # noqa |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class NotInRange(Exception): |
12
|
|
|
|
13
|
|
|
""" |
14
|
|
|
Until term 13 the available PDF files are scanned in and need OCR to be |
15
|
|
|
scraped. Therefore only terms 14 to currently term 17 are accepted. |
16
|
|
|
""" |
17
|
|
|
|
18
|
|
|
pass |
|
|
|
|
19
|
|
|
|
20
|
|
|
|
21
|
|
|
class NotGermanParty(Exception): |
22
|
|
|
|
23
|
|
|
""" |
24
|
|
|
Since this project focusses on Germany the current range of parties that |
25
|
|
|
will be accepted is reduced to German parties. |
26
|
|
|
""" |
27
|
|
|
|
28
|
|
|
pass |
|
|
|
|
29
|
|
|
|
30
|
|
|
|
31
|
|
|
class TooManyFirstNames(Exception): |
32
|
|
|
|
33
|
|
|
""" |
34
|
|
|
Currently only one first name and two middle names are supported. |
35
|
|
|
Example: Tom H. Paul last_name |
36
|
|
|
""" |
37
|
|
|
|
38
|
|
|
def __init__(self, message): |
39
|
|
|
"""use like: raise TooManyFirstNames ("message")""" |
40
|
|
|
Exception.__init__(self) |
41
|
|
|
print(message) |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
class AttrDisplay: |
45
|
|
|
|
46
|
|
|
""" |
47
|
|
|
Mark Lutz, Programming Python |
48
|
|
|
Provides an inheritable display overload method that shows instances |
49
|
|
|
with their class names and a name=value pair for each attribute stored |
50
|
|
|
on the instance itself (but not attrs inherited from its classes). Can |
51
|
|
|
be mixed into any class, and will work on any instance. |
52
|
|
|
""" |
53
|
|
|
|
54
|
|
|
def gather_attrs(self) -> list: |
55
|
|
|
""" |
56
|
|
|
Check if attributes have content and add them to a list called attrs. |
57
|
|
|
""" |
58
|
|
|
attrs = [] |
59
|
|
|
for key in sorted(self.__dict__): |
60
|
|
|
if self.__dict__[key] and self.__dict__[key] not in [ |
61
|
|
|
"unknown", |
|
|
|
|
62
|
|
|
"ew", |
|
|
|
|
63
|
|
|
None, |
|
|
|
|
64
|
|
|
]: |
65
|
|
|
attrs.append(f"{key}={getattr(self, key)}") |
66
|
|
|
return attrs |
67
|
|
|
|
68
|
|
|
def __str__(self) -> str: |
69
|
|
|
""" |
70
|
|
|
Instances will printed like this: |
71
|
|
|
class name |
72
|
|
|
attr1=value1 |
73
|
|
|
attr2=value2 |
74
|
|
|
... |
75
|
|
|
""" |
76
|
|
|
comp_repr = ( |
77
|
|
|
f"{self.__class__.__name__}:\n" |
78
|
|
|
+ "\n".join(str(attr) for attr in self.gather_attrs()) |
79
|
|
|
+ "\n" |
80
|
|
|
) |
81
|
|
|
return comp_repr |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
@dataclass |
|
|
|
|
85
|
|
|
class _Party_base: |
86
|
|
|
party_name: str # type: ignore # noqa |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
@dataclass |
|
|
|
|
90
|
|
|
class _Party_default: |
91
|
|
|
party_entry: str = field(default="unknown") |
92
|
|
|
party_exit: str = field(default="unknown") |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
@dataclass |
|
|
|
|
96
|
|
|
class Party(_Party_default, _Party_base, AttrDisplay): |
97
|
|
|
def __post_init__(self): |
98
|
|
|
if self.party_name not in GERMAN_PARTIES: |
99
|
|
|
raise NotGermanParty |
100
|
|
|
|