From 5cd9f289e81162ba7aae845e86ba86766b1b1b75 Mon Sep 17 00:00:00 2001 From: James Fawcus-Robinson Date: Tue, 21 Nov 2017 21:05:00 +1000 Subject: [PATCH] MAJOR: allow not saving output files For all of the output generators, the 'output_file' argument now defaults to being an empty string, this in turn disables the saved output. This also changes the allowable strings for disabling the output to include an empty one so that you can leave a line blank in the configuration file to disable a generator. --- archmap.conf | 2 +- archmap.py | 69 +++++++++++++++++++++++++++----------------------- tests/tests.py | 15 ++++------- 3 files changed, 44 insertions(+), 42 deletions(-) diff --git a/archmap.conf b/archmap.conf index 6dc3c70..baea33f 100644 --- a/archmap.conf +++ b/archmap.conf @@ -5,7 +5,7 @@ url = https://wiki.archlinux.org/index.php/ArchMap/List file = # The location to save the list of users and the GIS files, -# use 'no' to disable output. +# use 'no' or leave blank to disable output. users = /tmp/archmap_users.txt geojson = /tmp/archmap.geojson kml = /tmp/archmap.kml diff --git a/archmap.py b/archmap.py index f5ca9ec..b17bbc8 100755 --- a/archmap.py +++ b/archmap.py @@ -141,13 +141,13 @@ def parse_users(users): return parsed -def make_users(parsed_users, output_file, pretty=False): +def make_users(parsed_users, output_file='', pretty=False): """This function reads the raw text supplied by ``users``, it then writes it to ``output_file``. Args: parsed_users (:obj:`list` of :obj:`list` [:obj:`decimal.Decimal`, :obj:`decimal.Decimal`, :obj:`str`, :obj:`str`])\ : A list of lists, each sub-list should have 4 elements: ``[latitude, longitude, name, comment]`` - output_file (str): Location to save the text output + output_file (str): Location to save the text output. If left empty, nothing will be output pretty (bool): If set to True, the output "columns" will be aligned and expanded to match the longest element Returns: @@ -198,29 +198,30 @@ def make_users(parsed_users, output_file, pretty=False): # and strip the trailing whitespace then replace the newline (prevents editor errors) users = users.strip('\n').strip() + '\n' - log.info('Writing raw user list to ' + output_file) - # Write the text to 'output_file'. - with open(output_file, 'w') as output: - output.write(users) + if output_file != '': + log.info('Writing raw user list to ' + output_file) + # Write the text to 'output_file'. + with open(output_file, 'w') as output: + output.write(users) return users -def make_geojson(parsed_users, output_file): +def make_geojson(parsed_users, output_file=''): """This function reads the user data supplied by ``parsed_users``, it then generates GeoJSON output and writes it to ``output_file``. Args: parsed_users (:obj:`list` of :obj:`list` [:obj:`decimal.Decimal`, :obj:`decimal.Decimal`, :obj:`str`, :obj:`str`])\ : A list of lists, each sub-list should have 4 elements: ``[latitude, longitude, name, comment]`` - output_file (str): Location to save the GeoJSON output + output_file (str): Location to save the GeoJSON output. If left empty, nothing will be output Returns: str: The text written to the output file """ geojson = [] - log.info('Making and writing GeoJSON to ' + output_file) + log.debug('Making GeoJSON') for id, user in enumerate(parsed_users): # Generate a GeoJSON point feature for the user and add it to 'geojson'. point = Point((float(user[1]), float(user[0]))) @@ -230,32 +231,36 @@ def make_geojson(parsed_users, output_file): # Make 'geojson_str' for output. geojson_str = (dumps(FeatureCollection(geojson), sort_keys=True, indent=4)) + '\n' - with open(output_file, 'w') as output: - output.write(geojson_str) + if output_file != '': + log.info('Writing GeoJSON to ' + output_file) + with open(output_file, 'w') as output: + output.write(geojson_str) return geojson_str -def make_kml(parsed_users, output_file): +def make_kml(parsed_users, output_file=''): """This function reads the user data supplied by ``parsed_users``, it then generates KML output and writes it to ``output_file``. Args: parsed_users (:obj:`list` of :obj:`list` [:obj:`decimal.Decimal`, :obj:`decimal.Decimal`, :obj:`str`, :obj:`str`])\ : A list of lists, each sub-list should have 4 elements: ``[latitude, longitude, name, comment]`` - output_file (str): Location to save the KML output + output_file (str): Location to save the KML output. If left empty, nothing will be output Returns: str: The text written to the output file """ kml = Kml() - log.info('Making and writing KML to ' + output_file) + log.debug('Making KML') for user in parsed_users: # Generate a KML point for the user. kml.newpoint(name=user[2], coords=[(user[1], user[0])], description=user[3]) - kml.save(output_file) + if output_file != '': + log.info('Writing KML to ' + output_file) + kml.save(output_file) # Reset the ID counters featgeom.Feature._id = 0 @@ -264,25 +269,26 @@ def make_kml(parsed_users, output_file): return kml.kml() -def make_csv(parsed_users, output_file): +def make_csv(parsed_users, output_file=''): """This function reads the user data supplied by ``parsed_users``, it then generates CSV output and writes it to ``output_file``. Args: parsed_users (:obj:`list` of :obj:`list` [:obj:`decimal.Decimal`, :obj:`decimal.Decimal`, :obj:`str`, :obj:`str`])\ : A list of lists, each sub-list should have 4 elements: ``[latitude, longitude, name, comment]`` - output_file (str): Location to save the CSV output + output_file (str): Location to save the CSV output. If left empty, nothing will be output Returns: str: The text written to the output file """ - with open(output_file, 'w', newline='') as output: - csv_file_writer = csv.writer(output, quoting=csv.QUOTE_MINIMAL) + if output_file != '': + with open(output_file, 'w', newline='') as output: + csv_file_writer = csv.writer(output, quoting=csv.QUOTE_MINIMAL) - log.info('Making and writing CSV to ' + output_file) - csv_file_writer.writerow(['Latitude', 'Longitude', 'Name', 'Comment']) - for user in parsed_users: - csv_file_writer.writerow(user) + log.info('Making and writing CSV to ' + output_file) + csv_file_writer.writerow(['Latitude', 'Longitude', 'Name', 'Comment']) + for user in parsed_users: + csv_file_writer.writerow(user) csv_string = StringIO() csv_string_writer = csv.writer(csv_string, quoting=csv.QUOTE_MINIMAL, dialect='unix') @@ -374,10 +380,11 @@ def main(): output_file_csv = args.csv # Do what's needed. - if output_file_users == 'no' and \ - output_file_geojson == 'no' and \ - output_file_kml == 'no' and \ - output_file_csv == 'no': + dont_run = ['', 'no'] + if output_file_users in dont_run and \ + output_file_geojson in dont_run and \ + output_file_kml in dont_run and \ + output_file_csv in dont_run: log.warning('There is nothing to do') else: users = get_users(url=input_url, local=input_file) @@ -385,13 +392,13 @@ def main(): return parsed_users = parse_users(users) - if output_file_users != 'no': + if output_file_users not in dont_run: make_users(parsed_users, output_file_users) - if output_file_geojson != 'no': + if output_file_geojson not in dont_run: make_geojson(parsed_users, output_file_geojson) - if output_file_kml != 'no': + if output_file_kml not in dont_run: make_kml(parsed_users, output_file_kml) - if output_file_csv != 'no': + if output_file_csv not in dont_run: make_csv(parsed_users, output_file_csv) diff --git a/tests/tests.py b/tests/tests.py index 8fb9b08..754baf2 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -195,49 +195,44 @@ class ReturnedTestCase(unittest.TestCase): def setUp(self): self.sample_users = 'tests/sample-archmap_users.txt' - self.output_users = 'tests/output-archmap_users.txt' self.sample_pretty_users = 'tests/sample-archmap_pretty_users.txt' - self.output_pretty_users = 'tests/output-archmap_pretty_users.txt' self.sample_geojson = 'tests/sample-archmap.geojson' - self.output_geojson = 'tests/output-archmap.geojson' self.sample_kml = 'tests/sample-archmap.kml' - self.output_kml = 'tests/output-archmap.kml' self.sample_csv = 'tests/sample-archmap.csv' - self.output_csv = 'tests/output-archmap.csv' # Set 'maxDiff' to 'None' to be able to see long diffs when something goes wrong. self.maxDiff = None def test_users(self): - returned_users = archmap.make_users(self.parsed_users, self.output_users) + returned_users = archmap.make_users(self.parsed_users) with open(self.sample_users, 'r') as file: sample_users = file.read() self.assertEqual(sample_users, returned_users) def test_pretty_users(self): - returned_pretty_users = archmap.make_users(self.parsed_users, self.output_pretty_users, pretty=True) + returned_pretty_users = archmap.make_users(self.parsed_users, pretty=True) with open(self.sample_pretty_users, 'r') as file: sample_pretty_users = file.read() self.assertEqual(sample_pretty_users, returned_pretty_users) def test_geojson(self): - returned_geojson = archmap.make_geojson(self.parsed_users, self.output_geojson) + returned_geojson = archmap.make_geojson(self.parsed_users) with open(self.sample_geojson, 'r') as file: sample_geojson = file.read() self.assertEqual(sample_geojson, returned_geojson) def test_kml(self): - returned_kml = archmap.make_kml(self.parsed_users, self.output_kml) + returned_kml = archmap.make_kml(self.parsed_users) with open(self.sample_kml, 'r') as file: sample_kml = file.read() self.assertEqual(sample_kml, returned_kml) def test_csv(self): - returned_csv = archmap.make_csv(self.parsed_users, self.output_csv) + returned_csv = archmap.make_csv(self.parsed_users) with open(self.sample_csv, 'r') as file: sample_csv = file.read()