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
|
from datetime import datetime
import xml.dom.minidom
import xml.parsers.expat
import logging
from . import errors
from . import xmlbuilder
logger = logging.getLogger(__name__)
def OsmResponseToDom(response, tag, single=False, allow_empty=False):
"""
Returns the (sub-) DOM parsed from an OSM response
"""
try:
dom = xml.dom.minidom.parseString(response)
osm_dom = dom.getElementsByTagName("osm")[0]
all_data = osm_dom.getElementsByTagName(tag)
first_element = all_data[0]
except (IndexError) as e:
if allow_empty:
return []
raise errors.XmlResponseInvalidError(
"The XML response from the OSM API is invalid: %r" % e
)
except (xml.parsers.expat.ExpatError) as e:
raise errors.XmlResponseInvalidError(
"The XML response from the OSM API is invalid: %r" % e
)
if single:
return first_element
return all_data
def DomParseNode(DomElement):
"""
Returns NodeData for the node.
"""
result = _DomGetAttributes(DomElement)
result["tag"] = _DomGetTag(DomElement)
return result
def DomParseWay(DomElement):
"""
Returns WayData for the way.
"""
result = _DomGetAttributes(DomElement)
result["tag"] = _DomGetTag(DomElement)
result["nd"] = _DomGetNd(DomElement)
return result
def DomParseRelation(DomElement):
"""
Returns RelationData for the relation.
"""
result = _DomGetAttributes(DomElement)
result["tag"] = _DomGetTag(DomElement)
result["member"] = _DomGetMember(DomElement)
return result
def DomParseChangeset(DomElement):
"""
Returns ChangesetData for the changeset.
"""
result = _DomGetAttributes(DomElement)
result["tag"] = _DomGetTag(DomElement)
result["discussion"] = _DomGetDiscussion(DomElement)
return result
def DomParseNote(DomElement):
"""
Returns NoteData for the note.
"""
result = _DomGetAttributes(DomElement)
result["id"] = xmlbuilder._GetXmlValue(DomElement, "id")
result["status"] = xmlbuilder._GetXmlValue(DomElement, "status")
result["date_created"] = _ParseDate(
xmlbuilder._GetXmlValue(DomElement, "date_created")
)
result["date_closed"] = _ParseDate(
xmlbuilder._GetXmlValue(DomElement, "date_closed")
)
result["comments"] = _DomGetComments(DomElement)
return result
def _DomGetAttributes(DomElement):
"""
Returns a formated dictionnary of attributes of a DomElement.
"""
def is_true(v):
return (v == "true")
attribute_mapping = {
'uid': int,
'changeset': int,
'version': int,
'id': int,
'lat': float,
'lon': float,
'open': is_true,
'visible': is_true,
'ref': int,
'comments_count': int,
'timestamp': _ParseDate,
'created_at': _ParseDate,
'closed_at': _ParseDate,
'date': _ParseDate,
}
result = {}
for k, v in DomElement.attributes.items():
try:
result[k] = attribute_mapping[k](v)
except KeyError:
result[k] = v
return result
def _DomGetTag(DomElement):
"""
Returns the dictionnary of tags of a DomElement.
"""
result = {}
for t in DomElement.getElementsByTagName("tag"):
k = t.attributes["k"].value
v = t.attributes["v"].value
result[k] = v
return result
def _DomGetNd(DomElement):
"""
Returns the list of nodes of a DomElement.
"""
result = []
for t in DomElement.getElementsByTagName("nd"):
result.append(int(int(t.attributes["ref"].value)))
return result
def _DomGetDiscussion(DomElement):
"""
Returns the dictionnary of comments of a DomElement.
"""
result = []
try:
discussion = DomElement.getElementsByTagName("discussion")[0]
for t in discussion.getElementsByTagName("comment"):
comment = _DomGetAttributes(t)
comment['text'] = xmlbuilder._GetXmlValue(t, "text")
result.append(comment)
except IndexError:
pass
return result
def _DomGetComments(DomElement):
"""
Returns the list of comments of a DomElement.
"""
result = []
for t in DomElement.getElementsByTagName("comment"):
comment = {}
comment['date'] = _ParseDate(xmlbuilder._GetXmlValue(t, "date"))
comment['action'] = xmlbuilder._GetXmlValue(t, "action")
comment['text'] = xmlbuilder._GetXmlValue(t, "text")
comment['html'] = xmlbuilder._GetXmlValue(t, "html")
comment['uid'] = xmlbuilder._GetXmlValue(t, "uid")
comment['user'] = xmlbuilder._GetXmlValue(t, "user")
result.append(comment)
return result
def _DomGetMember(DomElement):
"""
Returns a list of relation members.
"""
result = []
for m in DomElement.getElementsByTagName("member"):
result.append(_DomGetAttributes(m))
return result
def _ParseDate(DateString):
date_formats = ["%Y-%m-%d %H:%M:%S UTC", "%Y-%m-%dT%H:%M:%SZ"]
for date_format in date_formats:
try:
result = datetime.strptime(DateString, date_format)
return result
except (ValueError, TypeError):
logger.debug(f"{DateString} does not match {date_format}")
return DateString
|