From 051961db70c39c2c8582ee41c5ecd8a0e1187282 Mon Sep 17 00:00:00 2001 From: Jaisen Mathai Date: Sat, 8 Oct 2016 10:02:16 -0700 Subject: [PATCH] gh-136 Skip geolocation lookup if no key is found --- elodie.py | 4 ++-- elodie/geolocation.py | 6 ++++++ elodie/tests/geolocation_test.py | 13 +++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/elodie.py b/elodie.py index 022141a..50ba8d2 100755 --- a/elodie.py +++ b/elodie.py @@ -79,7 +79,7 @@ def import_file(_file, destination, album_from_folder, trash, allow_duplicates): help='Import the file even if it\'s already been imported.') @click.argument('paths', nargs=-1, type=click.Path()) def _import(destination, source, file, album_from_folder, trash, paths, allow_duplicates): - """Import files or directories. + """Import files or directories by reading their EXIF and organizing them accordingly. """ destination = os.path.expanduser(destination) @@ -148,7 +148,7 @@ def update_time(media, file_path, time_string): @click.argument('files', nargs=-1, type=click.Path(dir_okay=False), required=True) def _update(album, location, time, title, files): - """Update files. + """Update a file's EXIF. Automatically modifies the file's location and file name accordingly. """ for file_path in files: if not os.path.exists(file_path): diff --git a/elodie/geolocation.py b/elodie/geolocation.py index 478de08..a12dd23 100644 --- a/elodie/geolocation.py +++ b/elodie/geolocation.py @@ -153,6 +153,9 @@ def reverse_lookup(lat, lon): key = get_key() + if(key is None): + return None + try: params = {'format': 'json', 'key': key, 'lat': lat, 'lon': lon} headers = {"Accept-Language": constants.accepted_language} @@ -178,6 +181,9 @@ def lookup(name): key = get_key() + if(key is None): + return None + try: params = {'format': 'json', 'key': key, 'location': name} if(constants.debug is True): diff --git a/elodie/tests/geolocation_test.py b/elodie/tests/geolocation_test.py index 22c64a9..f6c323e 100644 --- a/elodie/tests/geolocation_test.py +++ b/elodie/tests/geolocation_test.py @@ -7,6 +7,7 @@ import os import random import re import sys +from mock import patch sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) @@ -81,7 +82,19 @@ def test_reverse_lookup_with_invalid_key(): res = geolocation.reverse_lookup(123.45, 123.45) assert res is None, res +@patch('elodie.geolocation.constants') +def test_reverse_lookup_with_no_key(mock_constants): + mock_constants.application_directory = 'invalid path' + res = geolocation.reverse_lookup(123.45, 123.45) + assert res is None, res + def test_lookup_with_invalid_key(): geolocation.__KEY__ = 'invalid_key' res = geolocation.lookup('foo') assert res is None, res + +@patch('elodie.geolocation.constants') +def test_lookup_with_no_key(mock_constants): + mock_constants.application_directory = 'invalid path' + res = geolocation.lookup('foo') + assert res is None, res