1- #!/usr/bin/python
21"""
32Fetch diffs from an OSM planet server.
43
2423However, it can read cookies from a Netscape-style cookie jar file, send these
2524cookies to the server and will save received cookies to the jar file.
2625"""
26+ import sys
27+ import logging
28+ from textwrap import dedent as msgfmt
2729
2830from argparse import ArgumentParser , RawDescriptionHelpFormatter , ArgumentTypeError
2931import datetime as dt
3335from osmium .replication import newest_change_from_file
3436from osmium .replication .utils import get_replication_header
3537from osmium .version import pyosmium_release
36- from osmium import SimpleHandler , SimpleWriter
37-
38-
39- import re
40- import sys
41- import logging
42- from textwrap import dedent as msgfmt
38+ from osmium import SimpleWriter
4339
4440log = logging .getLogger ()
4541
42+
4643class ReplicationStart (object ):
4744 """ Represents the point where changeset download should begin.
4845 """
@@ -78,7 +75,8 @@ def from_date(datestr):
7875 date = dt .datetime .strptime (datestr , "%Y-%m-%dT%H:%M:%SZ" )
7976 date = date .replace (tzinfo = dt .timezone .utc )
8077 except ValueError :
81- raise ArgumentTypeError ("Date needs to be in ISO8601 format (e.g. 2015-12-24T08:08:08Z)." )
78+ raise ArgumentTypeError (
79+ "Date needs to be in ISO8601 format (e.g. 2015-12-24T08:08:08Z)." )
8280
8381 return ReplicationStart (date = date )
8482
@@ -106,6 +104,7 @@ def from_osm_file(fname, ignore_headers):
106104
107105 return ReplicationStart (seq_id = seq , date = ts , src = url )
108106
107+
109108def write_end_sequence (fname , seqid ):
110109 """Either writes out the sequence file or prints the sequence id to stdout.
111110 """
@@ -115,6 +114,7 @@ def write_end_sequence(fname, seqid):
115114 with open (fname , 'w' ) as fd :
116115 fd .write (str (seqid ))
117116
117+
118118def get_arg_parser (from_main = False ):
119119 parser = ArgumentParser (prog = 'pyosmium-get-changes' ,
120120 description = __doc__ ,
@@ -146,10 +146,10 @@ def get_arg_parser(from_main=False):
146146 group .add_argument ('-O' , '--start-osm-data' , dest = 'start_file' , metavar = 'OSMFILE' ,
147147 help = 'start at the date of the newest OSM object in the file' )
148148 parser .add_argument ('-f' , '--sequence-file' , dest = 'seq_file' ,
149- help = 'Sequence file. If the file exists, then updates '
150- 'will start after the id given in the file. At the '
151- 'end of the process, the last sequence ID contained '
152- 'in the diff is written.' )
149+ help = 'Sequence file. If the file exists, then updates '
150+ 'will start after the id given in the file. At the '
151+ 'end of the process, the last sequence ID contained '
152+ 'in the diff is written.' )
153153 parser .add_argument ('--ignore-osmosis-headers' , dest = 'ignore_headers' ,
154154 action = 'store_true' ,
155155 help = 'When determining the start from an OSM file, '
@@ -164,7 +164,7 @@ def get_arg_parser(from_main=False):
164164 return parser
165165
166166
167- def main (args ):
167+ def pyosmium_get_changes (args ):
168168 logging .basicConfig (stream = sys .stderr ,
169169 format = '%(asctime)s %(levelname)s: %(message)s' ,
170170 datefmt = '%Y-%m-%d %H:%M:%S' )
@@ -194,17 +194,16 @@ def main(args):
194194
195195 if options .server_url is not None and options .start .source is not None :
196196 if options .server_url != options .start .source :
197- log .error (msgfmt ("""
198- You asked to use server URL:
199- %s
200- but the referenced OSM file points to replication server:
201- %s
202- If you really mean to overwrite the URL, use --ignore-osmosis-headers."""
203- % (options .server_url , options .start .source )))
197+ log .error (msgfmt (f"""
198+ You asked to use server URL:
199+ { options .server_url }
200+ but the referenced OSM file points to replication server:
201+ { options .start .source }
202+ If you really mean to overwrite the URL, use --ignore-osmosis-headers.""" ))
204203 return 2
205204 url = options .server_url \
206- or options .start .source \
207- or 'https://planet.osm.org/replication/minute/'
205+ or options .start .source \
206+ or 'https://planet.osm.org/replication/minute/'
208207 logging .info ("Using replication server at %s" % url )
209208
210209 with rserv .ReplicationServer (url , diff_type = options .server_diff_type ) as svr :
@@ -248,5 +247,9 @@ def main(args):
248247 return 0
249248
250249
251- if __name__ == '__main__' :
252- exit (main (sys .argv [1 :]))
250+ def main ():
251+ logging .basicConfig (stream = sys .stderr ,
252+ format = '%(asctime)s %(levelname)s: %(message)s' ,
253+ datefmt = '%Y-%m-%d %H:%M:%S' )
254+
255+ return pyosmium_get_changes (sys .argv [1 :])
0 commit comments