File: changeset.py

package info (click to toggle)
python-osmapi 5.0.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 848 kB
  • sloc: python: 4,113; xml: 1,599; makefile: 46; sh: 14
file content (396 lines) | stat: -rw-r--r-- 13,819 bytes parent folder | download
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""
Changeset operations for the OpenStreetMap API.
"""

import re
import urllib.parse
import xml.dom.minidom
import xml.parsers.expat
from contextlib import contextmanager
from typing import Any, Optional, TYPE_CHECKING, Generator, cast
from xml.dom.minidom import Element

from . import dom, errors, xmlbuilder, parser

if TYPE_CHECKING:
    from .OsmApi import OsmApi


class ChangesetMixin:
    """Mixin providing changeset-related operations with pythonic method names."""

    @contextmanager
    def changeset(
        self: "OsmApi", changeset_tags: Optional[dict[str, str]] = None
    ) -> Generator[int, None, None]:
        """
        Context manager for a Changeset.

        It opens a Changeset, uploads the changes and closes the changeset
        when used with the `with` statement:

            #!python
            import osmapi

            with api.changeset({"comment": "Import script XYZ"}) as changeset_id:
                print(f"Part of changeset {changeset_id}")
                api.node_create({"lon":1, "lat":1, "tag": {}})

        If `changeset_tags` are given, this tags are applied (key/value).

        Returns `changeset_id`

        If no authentication information are provided,
        `OsmApi.UsernamePasswordMissingError` is raised.

        If there is already an open changeset,
        `OsmApi.ChangesetAlreadyOpenError` is raised.
        """
        if changeset_tags is None:
            changeset_tags = {}
        # Create a new changeset
        changeset_id = self.changeset_create(changeset_tags)
        yield changeset_id
        self.changeset_close()

    def changeset_get(
        self: "OsmApi", changeset_id: int, include_discussion: bool = False
    ) -> dict[str, Any]:
        """
        Returns changeset with `changeset_id` as a dict.

        `changeset_id` is the unique identifier of a changeset.

        If `include_discussion` is set to `True` the changeset discussion
        will be available in the result.
        """
        path = f"/api/0.6/changeset/{changeset_id}"
        if include_discussion:
            path = f"{path}?include_discussion=true"
        data = self._session._get(path)
        changeset = cast(
            Element, dom.OsmResponseToDom(data, tag="changeset", single=True)
        )
        return dom.dom_parse_changeset(changeset, include_discussion=include_discussion)

    def changeset_update(
        self: "OsmApi", changeset_tags: Optional[dict[str, str]] = None
    ) -> int:
        """
        Updates current changeset with `changeset_tags`.

        If no authentication information are provided,
        `OsmApi.UsernamePasswordMissingError` is raised.

        If there is no open changeset,
        `OsmApi.NoChangesetOpenError` is raised.

        If the changeset is already closed,
        `OsmApi.ChangesetClosedApiError` is raised.
        """
        if changeset_tags is None:
            changeset_tags = {}
        if not self._current_changeset_id:
            raise errors.NoChangesetOpenError("No changeset currently opened")
        if "created_by" not in changeset_tags:
            changeset_tags["created_by"] = self._created_by
        try:
            self._session._put(
                f"/api/0.6/changeset/{self._current_changeset_id}",
                xmlbuilder._xml_build("changeset", {"tag": changeset_tags}, data=self),
                return_value=False,
            )
        except errors.ApiError as e:
            if e.status == 409:
                raise errors.ChangesetClosedApiError(
                    e.status, e.reason, e.payload
                ) from e
            else:
                raise
        return self._current_changeset_id

    def changeset_create(
        self: "OsmApi", changeset_tags: Optional[dict[str, str]] = None
    ) -> int:
        """
        Opens a changeset.

        If `changeset_tags` are given, this tags are applied (key/value).

        Returns `changeset_id`

        If no authentication information are provided,
        `OsmApi.UsernamePasswordMissingError` is raised.

        If there is already an open changeset,
        `OsmApi.ChangesetAlreadyOpenError` is raised.
        """
        if changeset_tags is None:
            changeset_tags = {}
        if self._current_changeset_id:
            raise errors.ChangesetAlreadyOpenError("Changeset already opened")
        if "created_by" not in changeset_tags:
            changeset_tags["created_by"] = self._created_by

        # check if someone tries to create a test changeset to PROD
        if (
            self._api == "https://www.openstreetmap.org"
            and changeset_tags.get("comment") == "My first test"
        ):
            raise errors.OsmApiError(
                "DO NOT CREATE test changesets on the production server"
            )

        result = self._session._put(
            "/api/0.6/changeset/create",
            xmlbuilder._xml_build("changeset", {"tag": changeset_tags}, data=self),
        )
        self._current_changeset_id = int(result)
        return self._current_changeset_id

    def changeset_close(self: "OsmApi") -> int:
        """
        Closes current changeset.

        Returns `changeset_id`.

        If no authentication information are provided,
        `OsmApi.UsernamePasswordMissingError` is raised.

        If there is no open changeset,
        `OsmApi.NoChangesetOpenError` is raised.

        If the changeset is already closed,
        `OsmApi.ChangesetClosedApiError` is raised.
        """
        if not self._current_changeset_id:
            raise errors.NoChangesetOpenError("No changeset currently opened")
        try:
            self._session._put(
                f"/api/0.6/changeset/{self._current_changeset_id}/close",
                None,
                return_value=False,
            )
            current_changeset_id = self._current_changeset_id
            self._current_changeset_id = 0
        except errors.ApiError as e:
            if e.status == 409:
                raise errors.ChangesetClosedApiError(
                    e.status, e.reason, e.payload
                ) from e
            else:
                raise
        return current_changeset_id

    def changeset_upload(
        self: "OsmApi", changes_data: list[dict[str, Any]]
    ) -> list[dict[str, Any]]:
        """
        Upload data with the `changes_data` list of dicts.

        Returns list with updated ids.

        If no authentication information are provided,
        `OsmApi.UsernamePasswordMissingError` is raised.

        If the changeset is already closed,
        `OsmApi.ChangesetClosedApiError` is raised.
        """
        data = ""
        data += '<?xml version="1.0" encoding="UTF-8"?>\n'
        data += '<osmChange version="0.6" generator="'
        data += self._created_by + '">\n'
        for change in changes_data:
            data += "<" + change["action"] + ">\n"
            change_data = change["data"]
            data += self._add_changeset_data(change_data, change["type"])
            data += "</" + change["action"] + ">\n"
        data += "</osmChange>"
        try:
            response_data = self._session._post(
                f"/api/0.6/changeset/{self._current_changeset_id}/upload",
                data.encode("utf-8"),
                forceAuth=True,
            )
        except errors.ApiError as e:
            if e.status == 409 and re.search(
                r"The changeset .* was closed at .*", e.payload
            ):
                raise errors.ChangesetClosedApiError(
                    e.status, e.reason, e.payload
                ) from e
            else:
                raise
        try:
            result_dom = xml.dom.minidom.parseString(response_data)
            diff_result = result_dom.getElementsByTagName("diffResult")[0]
            result_elements = [
                x for x in diff_result.childNodes if x.nodeType == x.ELEMENT_NODE
            ]
        except (xml.parsers.expat.ExpatError, IndexError) as e:
            raise errors.XmlResponseInvalidError(
                f"The XML response from the OSM API is invalid: {e!r}"
            ) from e

        for change in changes_data:
            if change["action"] == "delete":
                for change_element in change["data"]:
                    change_element.pop("version")
            else:
                self._assign_id_and_version(result_elements, change["data"])

        return changes_data

    def changeset_download(self: "OsmApi", changeset_id: int) -> list[dict[str, Any]]:
        """
        Download data from changeset `changeset_id`.

        Returns list of dict with type, action, and data.
        """
        uri = f"/api/0.6/changeset/{changeset_id}/download"
        data = self._session._get(uri)
        return parser.parse_osc(data)

    def changesets_get(  # noqa: C901
        self: "OsmApi",
        min_lon: Optional[float] = None,
        min_lat: Optional[float] = None,
        max_lon: Optional[float] = None,
        max_lat: Optional[float] = None,
        userid: Optional[int] = None,
        username: Optional[str] = None,
        closed_after: Optional[str] = None,
        created_before: Optional[str] = None,
        only_open: bool = False,
        only_closed: bool = False,
    ) -> dict[int, dict[str, Any]]:
        """
        Returns a dict with the id of the changeset as key matching all criteria.

        All parameters are optional.
        """
        uri = "/api/0.6/changesets"
        params: dict[str, Any] = {}
        if min_lon or min_lat or max_lon or max_lat:
            params["bbox"] = f"{min_lon},{min_lat},{max_lon},{max_lat}"
        if userid:
            params["user"] = userid
        if username:
            params["display_name"] = username
        if closed_after and not created_before:
            params["time"] = closed_after
        if created_before:
            if not closed_after:
                closed_after = "1970-01-01T00:00:00Z"
            params["time"] = f"{closed_after},{created_before}"
        if only_open:
            params["open"] = 1
        if only_closed:
            params["closed"] = 1

        if params:
            uri += "?" + urllib.parse.urlencode(params)

        data = self._session._get(uri)
        changesets = cast(list[Element], dom.OsmResponseToDom(data, tag="changeset"))
        result: dict[int, dict[str, Any]] = {}
        for cur_changeset in changesets:
            tmp_cs = dom.dom_parse_changeset(cur_changeset)
            result[tmp_cs["id"]] = tmp_cs
        return result

    def changeset_comment(
        self: "OsmApi", changeset_id: int, comment: str
    ) -> dict[str, Any]:
        """
        Adds a comment to the changeset `changeset_id`.

        `comment` should be a string.

        Returns the updated changeset data dict.

        If no authentication information are provided,
        `OsmApi.UsernamePasswordMissingError` is raised.

        If the changeset is already closed,
        `OsmApi.ChangesetClosedApiError` is raised.
        """
        params = urllib.parse.urlencode({"text": comment})
        try:
            data = self._session._post(
                f"/api/0.6/changeset/{changeset_id}/comment",
                params,
                forceAuth=True,
            )
        except errors.ApiError as e:
            if e.status == 409:
                raise errors.ChangesetClosedApiError(
                    e.status, e.reason, e.payload
                ) from e
            else:
                raise
        changeset = cast(
            Element,
            dom.OsmResponseToDom(data, tag="changeset", single=True),
        )
        return dom.dom_parse_changeset(changeset, include_discussion=False)

    def changeset_subscribe(self: "OsmApi", changeset_id: int) -> dict[str, Any]:
        """
        Subscribe to the changeset `changeset_id`.

        Returns the updated changeset data dict.

        If no authentication information are provided,
        `OsmApi.UsernamePasswordMissingError` is raised.

        If already subscribed to this changeset,
        `OsmApi.AlreadySubscribedApiError` is raised.
        """
        try:
            data = self._session._post(
                f"/api/0.6/changeset/{changeset_id}/subscribe",
                None,
                forceAuth=True,
            )
        except errors.ApiError as e:
            if e.status == 409:
                raise errors.AlreadySubscribedApiError(
                    e.status, e.reason, e.payload
                ) from e
            else:
                raise
        changeset = cast(
            Element,
            dom.OsmResponseToDom(data, tag="changeset", single=True),
        )
        return dom.dom_parse_changeset(changeset, include_discussion=False)

    def changeset_unsubscribe(self: "OsmApi", changeset_id: int) -> dict[str, Any]:
        """
        Unsubscribe from the changeset `changeset_id`.

        Returns the updated changeset data dict.

        If no authentication information are provided,
        `OsmApi.UsernamePasswordMissingError` is raised.

        If not subscribed to this changeset,
        `OsmApi.NotSubscribedApiError` is raised.
        """
        try:
            data = self._session._post(
                f"/api/0.6/changeset/{changeset_id}/unsubscribe",
                None,
                forceAuth=True,
            )
        except errors.ApiError as e:
            if e.status == 404:
                raise errors.NotSubscribedApiError(e.status, e.reason, e.payload) from e
            else:
                raise
        changeset = cast(
            Element,
            dom.OsmResponseToDom(data, tag="changeset", single=True),
        )
        return dom.dom_parse_changeset(changeset, include_discussion=False)