Skip to content
Permalink
Browse files

Make MapQuest parameters configurable #258 (#308)

  • Loading branch information...
jmathai committed Apr 1, 2019
1 parent 7fc5b5f commit 19be12b0bbea5f3c35824a4b342f23ab6da5d853
Showing with 66 additions and 10 deletions.
  1. +17 −2 elodie/constants.py
  2. +6 −1 elodie/geolocation.py
  3. +43 −7 elodie/tests/constants_test.py
@@ -9,13 +9,12 @@
debug = False

#: Directory in which to store Elodie settings.
application_directory = '{}/.elodie'.format(path.expanduser('~'))
if (
'ELODIE_APPLICATION_DIRECTORY' in environ and
path.isdir(environ['ELODIE_APPLICATION_DIRECTORY'])
):
application_directory = environ['ELODIE_APPLICATION_DIRECTORY']
else:
application_directory = '{}/.elodie'.format(path.expanduser('~'))

#: File in which to store details about media Elodie has seen.
hash_db = '{}/hash.json'.format(application_directory)
@@ -29,6 +28,22 @@
#: Path to Elodie's ExifTool config file.
exiftool_config = path.join(script_directory, 'configs', 'ExifTool_config')

#: Path to MapQuest base URL
mapquest_base_url = 'https://open.mapquestapi.com'
if (
'ELODIE_MAPQUEST_BASE_URL' in environ and
environ['ELODIE_MAPQUEST_BASE_URL'] != ''
):
mapquest_base_url = environ['ELODIE_MAPQUEST_BASE_URL']

#: MapQuest key from environment
mapquest_key = None
if (
'ELODIE_MAPQUEST_KEY' in environ and
environ['ELODIE_MAPQUEST_KEY'] != ''
):
mapquest_key = environ['ELODIE_MAPQUEST_KEY']

#: Accepted language in responses from MapQuest
accepted_language = 'en'

@@ -104,6 +104,10 @@ def get_key():
if __KEY__ is not None:
return __KEY__

if constants.mapquest_key is not None:
__KEY__ = constants.mapquest_key
return __KEY__

config_file = '%s/config.ini' % constants.application_directory
if not path.exists(config_file):
return None
@@ -197,7 +201,8 @@ def lookup(**kwargs):
path = '/geocoding/v1/address'
if('lat' in kwargs and 'lon' in kwargs):
path = '/nominatim/v1/reverse.php'
url = 'https://open.mapquestapi.com%s?%s' % (
url = '%s%s?%s' % (
constants.mapquest_base_url,
path,
urllib.parse.urlencode(params)
)
@@ -26,28 +26,36 @@ def test_debug():
assert constants.debug == constants.debug, constants.debug

def test_application_directory_default():
reload(constants)
expected_path = '{}/.elodie'.format(os.path.expanduser('~'))
assert constants.application_directory == expected_path, constants.application_directory

def test_application_directory_override_invalid():
os.environ['ELODIE_APPLICATION_DIRECTORY'] = '/foo/bar'
reload(constants)
directory_to_check = constants.application_directory

# reset
os.environ['ELODIE_APPLICATION_DIRECTORY'] = ''
reload(constants)

expected_path = '{}/.elodie'.format(os.path.expanduser('~'))
assert constants.application_directory == expected_path, constants.application_directory
assert directory_to_check == expected_path, constants.application_directory

def test_application_directory_override_valid():
cwd = os.getcwd()
os.environ['ELODIE_APPLICATION_DIRECTORY'] = cwd
reload(constants)
directory_to_check = constants.application_directory
hash_db_to_check = constants.hash_db

assert constants.application_directory == cwd, constants.application_directory
assert cwd in constants.hash_db, constants.hash_db

# must come after test_application_directory_override_valid due to env var reset
def test_hash_db():
# reset
os.environ['ELODIE_APPLICATION_DIRECTORY'] = ''
reload(constants)

assert directory_to_check == cwd, constants.application_directory
assert cwd in hash_db_to_check, constants.hash_db

def test_hash_db():
assert constants.hash_db == '{}/hash.json'.format(constants.application_directory), constants.hash_db

def test_location_db():
@@ -61,6 +69,34 @@ def test_exiftool_config():
path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
assert '{}/configs/ExifTool_config'.format(path) == constants.exiftool_config, constants.exiftool_config

def test_mapquest_base_url_default():
assert constants.mapquest_base_url == 'https://open.mapquestapi.com', constants.mapquest_base_url

def test_mapquest_base_url_override():
os.environ['ELODIE_MAPQUEST_BASE_URL'] = 'foobar'
reload(constants)
url_to_check = constants.mapquest_base_url

# reset
os.environ['ELODIE_MAPQUEST_BASE_URL'] = ''
reload(constants)

assert url_to_check == 'foobar', constants.mapquest_base_url

def test_mapquest_Key():
assert constants.mapquest_key == None, constants.mapquest_key

def test_mapquest_key_override():
os.environ['ELODIE_MAPQUEST_KEY'] = 'foobar'
reload(constants)
key_to_check = constants.mapquest_key

# reset
os.environ['ELODIE_MAPQUEST_KEY'] = ''
reload(constants)

assert key_to_check == 'foobar', constants.mapquest_key

def test_accepted_language():
assert constants.accepted_language == 'en', constants.accepted_language

0 comments on commit 19be12b

Please sign in to comment.
You can’t perform that action at this time.