-
Notifications
You must be signed in to change notification settings - Fork 5
/
abstimmungsparser.py
220 lines (136 loc) · 4.66 KB
/
abstimmungsparser.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
#!/usr/bin/env python3
import sys
import yaml
import os
from yaml.representer import SafeRepresenter
#def unicode_representer (dumper, uni):
#node = yaml.ScalarNode (tag = u'tag:yaml.org,2002:str', value = uni)
#return node
#yaml.add_representer(unicode, unicode_representer)
yaml.add_representer(str, SafeRepresenter.represent_str)
class Abstimmung:
data = {}
def parse_abstimmung (self, abstimmungsfile):
with open(abstimmungsfile, 'r') as f:
for data in yaml.load_all (f):
if data:
self.data = data
break
def __init__(self):
self.data = {}
def get_title (self):
return self.data["title"]
def set_title (self, title):
self.data["title"] = title
def get_legislaturperiode (self):
return self.data["abstimmung"]["legislaturperiode"]
def set_legislaturperiode (self, legislaturperiode):
if not "abstimmung" in self.data:
self.data["abstimmung"] = {}
self.data["abstimmung"]["legislaturperiode"] = legislaturperiode
def get_bundestagssitzung (self):
return self.data["abstimmung"]["bundestagssitzung"]
def set_bundestagssitzung (self, bundestagssitzung):
if not "abstimmung" in self.data:
self.data["abstimmung"] = {}
self.data["abstimmung"]["bundestagssitzung"] = bundestagssitzung
def get_abstimmung (self):
return self.data["abstimmung"]["abstimmung"]
def set_abstimmung (self, abstimmung):
if not "abstimmung" in self.data:
self.data["abstimmung"] = {}
self.data["abstimmung"]["abstimmung"] = abstimmung
def get_datum (self):
return self.data["abstimmung"]["datum"]
def set_datum (self, datum):
if not "abstimmung" in self.data:
self.data["abstimmung"] = {}
self.data["abstimmung"]["datum"] = datum
def get_categories (self):
return self.data["categories"]
def set_categories (self, categories):
self.data["categories"] = categories
def add_category (self, category):
if not "categories" in self.data:
self.data["categories"] = []
if category not in self.data["categories"]:
self.data["categories"].append (category)
def get_tags (self):
return self.data["tags"]
def set_tags (self, tags):
self.data["tags"] = tags
def add_tag (self, tag):
if not "tags" in self.data:
self.data["tags"] = []
if tag not in self.data["tags"]:
self.data["tags"].append (tag)
def get_abstimmungs_ergebnisse (self):
return self.data["ergebnis"]
def set_abstimmungs_ergebnisse (self, tag):
self.data["ergebnis"] = (tag)
#######################################################################
#
# DATA FILES ...
#
# sind Dateien, die Daten zu einer Bestimmten Abstimmung enthalten.
# Jedes "data_file" Objekt ist ein dict und sollte die folgenden
# keys enthalten:
# - titel: Menschenlesbare Beschreibung
# - url: Link zur Datei
#
# get_data_files returns alle bekannten data_files.
def get_data_files (self):
if not "data" in self.data:
self.data["data"] = []
return self.data["data"]
#
# set_data_files ueberschreibt alle data_files mit einem neuen array
def set_data_files (self, data):
self.data["data"] = data
#
# add_data_file fuegt ein neues "data_file" Objekt hinzu
def add_data_file (self, data_file):
if not "data" in self.data:
self.data["data"] = []
if data_file not in self.data["data"]:
self.data["data"].append (data_file)
#
#######################################################################
def get_documents (self):
if not "documents" in self.data:
self.data["documents"] = []
return self.data["documents"]
def set_documents (self, documents):
self.data["documents"] = documents
def add_document (self, document):
if not "documents" in self.data:
self.data["documents"] = []
if document not in self.data["documents"]:
self.data["documents"].append (document)
def get_links (self):
if not "links" in self.data:
self.data["links"] = []
return self.data["links"]
def set_links (self, links):
self.data["links"] = links
def add_link (self, links):
if not "links" in self.data:
self.data["links"] = []
if links not in self.data["links"]:
self.data["links"].append (links)
def get_preview (self):
return self.data["preview"]
def set_preview (self, preview):
self.data["preview"] = (preview)
def write_abstimmung (self, abstimmungsfile):
self.data["layout"] = "abstimmung"
if not os.path.exists (os.path.dirname (abstimmungsfile)):
os.makedirs (os.path.dirname (abstimmungsfile))
with open(abstimmungsfile, 'w') as f:
yaml.dump (self.data, f, explicit_start=True, default_flow_style=False, encoding='utf-8', allow_unicode=True)
f.write ("---")
if __name__ == "__main__":
curfile = sys.argv[1]
a = Abstimmung ()
neu = a.parse_abstimmung (curfile)
print (a.get_title ())