-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPUB_files_place.py
93 lines (48 loc) · 2.35 KB
/
SPUB_files_place.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
import xml.etree.cElementTree as ET
from datetime import datetime
#%% classes
class Place:
def __init__(self, id_, lat, lon, geonames='', name='', annotation='', **kwargs):
self.id = f"http://www.wikidata.org/entity/Q{id_}"
self.lat = lat
self.lon = lon
self.geonames = geonames
self.periods = [self.PlacePeriod(name=name)]
self.annotation = annotation
class PlacePeriod:
def __init__(self, date_from='', date_to='', name='', country='', lang='pl'):
self.date_from = date_from
self.date_to = date_to
self.name = name
self.country = country
self.lang = lang
def __repr__(self):
return "PlacePeriod('{}', '{}', '{}', '{}', '{}')".format(self.date_from, self.date_to, self.name, self.country, self.lang)
def to_xml(self):
period_xml = ET.Element('period', {'date-from': self.date_from, 'date-to': self.date_to})
for attr_tag, value in zip(['name', 'country'], [self.name, self.country]):
if value:
ET.SubElement(period_xml, attr_tag, {'lang': self.lang}).text = value
return period_xml
@classmethod
def from_dict(cls, place_dict):
id_ = place_dict.get('wiki')
lat, lon = place_dict.get('coordinates').split(',') if place_dict.get('coordinates') else ['', '']
name = place_dict.get('name')
return cls(id_, lat, lon, name=name)
def to_xml(self):
place_xml = ET.Element('place', {k:v for k,v in self.__dict__.items() if k not in ('periods', 'annotation') and v})
for period in self.periods:
place_xml.append(period.to_xml())
if self.annotation:
annotation_xml = ET.Element('annotation')
annotation_xml.text = self.annotation
place_xml.append(annotation_xml)
return place_xml
# # schemat XML
# <place geonames="3088171" lon="16.92993" lat="52.40692" id="https://www.wikidata.org/wiki/Q268">
# <period date-from="" date-to="">
# <name lang="pl">Poznań</name>
# <country lang="pl">Polska</country>
# </period>
# </place>