|
29 | 29 | However, it can read cookies from a Netscape-style cookie jar file, send these |
30 | 30 | cookies to the server and will save received cookies to the jar file. |
31 | 31 | """ |
32 | | -from typing import Optional, List |
| 32 | +from typing import List |
33 | 33 | import sys |
34 | 34 | import logging |
35 | 35 | from textwrap import dedent as msgfmt |
36 | | - |
37 | | -from argparse import ArgumentParser, RawDescriptionHelpFormatter, ArgumentTypeError |
38 | | -import datetime as dt |
39 | | -from dataclasses import dataclass |
| 36 | +from argparse import ArgumentParser, RawDescriptionHelpFormatter |
40 | 37 | import http.cookiejar |
41 | 38 |
|
42 | | -from osmium.replication import server as rserv |
43 | | -from osmium.replication import newest_change_from_file |
44 | | -from osmium.replication.utils import get_replication_header |
45 | | -from osmium.version import pyosmium_release |
46 | | -from osmium import SimpleWriter |
47 | | - |
48 | | -log = logging.getLogger() |
| 39 | +from ..replication import server as rserv |
| 40 | +from ..version import pyosmium_release |
| 41 | +from .. import SimpleWriter |
| 42 | +from .common import ReplicationStart |
49 | 43 |
|
50 | 44 |
|
51 | | -@dataclass |
52 | | -class ReplicationStart: |
53 | | - """ Represents the point where changeset download should begin. |
54 | | - """ |
55 | | - date: Optional[dt.datetime] = None |
56 | | - seq_id: Optional[int] = None |
57 | | - source: Optional[str] = None |
58 | | - |
59 | | - def get_sequence(self, svr: rserv.ReplicationServer) -> Optional[int]: |
60 | | - if self.seq_id is not None: |
61 | | - log.debug("Using given sequence ID %d" % self.seq_id) |
62 | | - return self.seq_id + 1 |
63 | | - |
64 | | - assert self.date is not None |
65 | | - log.debug("Looking up sequence ID for timestamp %s" % self.date) |
66 | | - return svr.timestamp_to_sequence(self.date) |
67 | | - |
68 | | - def get_end_sequence(self, svr: rserv.ReplicationServer) -> Optional[int]: |
69 | | - if self.seq_id is not None: |
70 | | - log.debug("Using end sequence ID %d" % self.seq_id) |
71 | | - return self.seq_id |
72 | | - |
73 | | - assert self.date is not None |
74 | | - log.debug("Looking up end sequence ID for timestamp %s" % self.date) |
75 | | - return svr.timestamp_to_sequence(self.date) |
76 | | - |
77 | | - @staticmethod |
78 | | - def from_id(idstr: str) -> 'ReplicationStart': |
79 | | - try: |
80 | | - seq_id = int(idstr) |
81 | | - except ValueError: |
82 | | - raise ArgumentTypeError("Sequence id '%s' is not a number" % idstr) |
83 | | - |
84 | | - if seq_id < -1: |
85 | | - raise ArgumentTypeError("Sequence id '%s' is negative" % idstr) |
86 | | - |
87 | | - return ReplicationStart(seq_id=seq_id) |
88 | | - |
89 | | - @staticmethod |
90 | | - def from_date(datestr: str) -> 'ReplicationStart': |
91 | | - try: |
92 | | - date = dt.datetime.strptime(datestr, "%Y-%m-%dT%H:%M:%SZ") |
93 | | - date = date.replace(tzinfo=dt.timezone.utc) |
94 | | - except ValueError: |
95 | | - raise ArgumentTypeError( |
96 | | - "Date needs to be in ISO8601 format (e.g. 2015-12-24T08:08:08Z).") |
97 | | - |
98 | | - return ReplicationStart(date=date) |
99 | | - |
100 | | - @staticmethod |
101 | | - def from_osm_file(fname: str, ignore_headers: bool) -> 'ReplicationStart': |
102 | | - if ignore_headers: |
103 | | - ts = None |
104 | | - seq = None |
105 | | - url = None |
106 | | - else: |
107 | | - try: |
108 | | - (url, seq, ts) = get_replication_header(fname) |
109 | | - except RuntimeError as e: |
110 | | - raise ArgumentTypeError(e) |
111 | | - |
112 | | - if ts is None and seq is None: |
113 | | - log.debug("OSM file has no replication headers. Looking for newest OSM object.") |
114 | | - try: |
115 | | - ts = newest_change_from_file(fname) |
116 | | - except RuntimeError as e: |
117 | | - raise ArgumentTypeError(e) |
118 | | - |
119 | | - if ts is None: |
120 | | - raise ArgumentTypeError("OSM file does not seem to contain valid data.") |
121 | | - |
122 | | - return ReplicationStart(seq_id=seq, date=ts, source=url) |
| 45 | +log = logging.getLogger() |
123 | 46 |
|
124 | 47 |
|
125 | 48 | def write_end_sequence(fname: str, seqid: int) -> None: |
|
0 commit comments