Make MapQuest parameters configurable #258 (#308)

This commit is contained in:
Jaisen Mathai 2019-04-01 00:56:37 -07:00 committed by GitHub
parent 7fc5b5ffe0
commit 19be12b0bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 10 deletions

View File

@ -9,13 +9,12 @@ from sys import version_info
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 @@ script_directory = path.dirname(path.dirname(path.abspath(__file__)))
#: 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'

View File

@ -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)
)

View File

@ -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