From dba6ed4b0b35165ac00bede57808b72fafa2abcb Mon Sep 17 00:00:00 2001 From: fordprefect Date: Fri, 27 Mar 2020 20:24:38 +0100 Subject: [PATCH] more saving options (stdout, return) --- osmsearch.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/osmsearch.py b/osmsearch.py index 23d3d46..1a2bfcc 100644 --- a/osmsearch.py +++ b/osmsearch.py @@ -16,9 +16,15 @@ assert sys.version_info >= (3, 6), "At least Python 3.8 required due to fStrings class OSMSearch(): def __init__(self, args): + """ + TODO: write docstring + """ self.url = args.pop("url") self.maxsearchnums = args.pop("maxsearchnums") - self.gpxfilename = args.pop("outputfilename") + self.save_to_file = True if args["output"] == "disk" else False + if self.save_to_file: + self.gpxfilename = args.pop("outputfilename") + self.print_xml = True if args["output"] == "print" else False self.use_boundingbox = args.pop("use_boundingbox") self.ignore_ids = args.pop("ignore_ids") if self.use_boundingbox: @@ -32,10 +38,17 @@ class OSMSearch(): self.initialize_gpx_file() self.search() + + if self.save_to_file: + self.save_to_disk() + if self.print_xml: + self.print_to_stdout() - self.save_to_disk() - def initialize_gpx_file(self): + if not self.save_to_file: + self.gpxfile = gpxpy.gpx.GPX() + return + if os.path.isfile(self.gpxfilename): raise NotImplementedError("We don't know how to deal with existing files yet, sry") # TODO: file name collision? either: @@ -85,6 +98,9 @@ class OSMSearch(): with open(self.gpxfilename, "w") as f: f.write(self.gpxfile.to_xml()) + def print_to_stdout(self): + print(self.gpxfile.to_xml()) + default_args = { "q": "Camping", "format": "json", @@ -95,6 +111,7 @@ default_args = { "ignore_ids": [], "maxsearchnums": 10, "url": "https://nominatim.openstreetmap.org/search/", + "output": "print", } if __name__ == "__main__":