load config as global variable

This commit is contained in:
Cédric Leporcq 2021-04-16 20:02:14 +02:00
parent 33649319a4
commit 42b1f4ecd4
14 changed files with 89 additions and 97 deletions

View File

@ -1,3 +1,4 @@
[MapQuest]
key=your-api-key-goes-here
prefer_english_names=False
[Geolocation]
geocoder=Nominatim
mapquest_key=m5aGo8xGe4LLhxeKZYpHr2MPXGN2aDhe
prefer_english_names=False

View File

@ -122,7 +122,7 @@ def _import(destination, source, file, album_from_folder, trash, allow_duplicate
# if no exclude list was passed in we check if there's a config
if len(exclude_regex) == 0:
config = load_config()
config = load_config(constants.CONFIG_FILE)
if 'Exclusions' in config:
exclude_regex = [value for key, value in config.items('Exclusions')]

View File

@ -2,24 +2,20 @@
from configparser import RawConfigParser
from os import path
from elodie import constants
config_file = '%s/config.ini' % constants.application_directory
def load_config():
def load_config(file):
if hasattr(load_config, "config"):
return load_config.config
if not path.exists(config_file):
if not path.exists(file):
return {}
load_config.config = RawConfigParser()
load_config.config.read(config_file)
load_config.config.read(file)
return load_config.config
def load_plugin_config():
config = load_config()
def load_plugin_config(file):
config = load_config(file)
# If plugins are defined in the config we return them as a list
# Else we return an empty list
@ -28,10 +24,10 @@ def load_plugin_config():
return []
def load_config_for_plugin(name):
def load_config_for_plugin(name, file):
# Plugins store data using Plugin%PluginName% format.
key = 'Plugin{}'.format(name)
config = load_config()
config = load_config(file)
if key in config:
return config[key]

View File

@ -49,3 +49,5 @@ accepted_language = 'en'
# check python version, required in filesystem.py to trigger appropriate method
python_version = version_info.major
CONFIG_FILE = '%s/config.ini' % application_directory

View File

@ -15,6 +15,8 @@ from elodie import compatability
from elodie import geolocation
from elodie import log
from elodie.config import load_config
from elodie import constants
from elodie.localstorage import Db
from elodie.media.base import Base, get_all_subclasses
from elodie.plugins.plugins import Plugins
@ -218,7 +220,7 @@ class FileSystem(object):
name,
)
config = load_config()
config = load_config(constants.CONFIG_FILE)
if('File' in config and 'capitalization' in config['File'] and config['File']['capitalization'] == 'upper'):
return name.upper()
@ -247,7 +249,7 @@ class FileSystem(object):
if self.cached_file_name_definition is not None:
return self.cached_file_name_definition
config = load_config()
config = load_config(constants.CONFIG_FILE)
# If File is in the config we assume name and its
# corresponding values are also present
@ -307,7 +309,7 @@ class FileSystem(object):
if self.cached_folder_path_definition is not None:
return self.cached_folder_path_definition
config = load_config()
config = load_config(constants.CONFIG_FILE)
# If Directory is in the config we assume full_path and its
# corresponding values (date, location) are also present
@ -390,7 +392,7 @@ class FileSystem(object):
)
return folder
elif part in ('date'):
config = load_config()
config = load_config(constants.CONFIG_FILE)
# If Directory is in the config we assume full_path and its
# corresponding values (date, location) are also present
config_directory = self.default_folder_path_definition

View File

@ -108,15 +108,11 @@ def get_key():
__KEY__ = constants.mapquest_key
return __KEY__
config_file = '%s/config.ini' % constants.application_directory
if not path.exists(config_file):
config = load_config(constants.CONFIG_FILE)
if('Geolocation' not in config):
return None
config = load_config()
if('MapQuest' not in config):
return None
__KEY__ = config['MapQuest']['key']
__KEY__ = config['Geolocation']['mapquest_key']
return __KEY__
def get_prefer_english_names():
@ -124,18 +120,11 @@ def get_prefer_english_names():
if __PREFER_ENGLISH_NAMES__ is not None:
return __PREFER_ENGLISH_NAMES__
config_file = '%s/config.ini' % constants.application_directory
if not path.exists(config_file):
config = load_config(constants.CONFIG_FILE)
if('prefer_english_names' not in config['Geolocation']):
return False
config = load_config()
if('MapQuest' not in config):
return False
if('prefer_english_names' not in config['MapQuest']):
return False
__PREFER_ENGLISH_NAMES__ = bool(config['MapQuest']['prefer_english_names'])
__PREFER_ENGLISH_NAMES__ = bool(config['Geolocation']['prefer_english_names'])
return __PREFER_ENGLISH_NAMES__
def place_name(lat, lon):

View File

@ -17,7 +17,7 @@ from traceback import format_exc
from elodie.compatability import _bytes
from elodie.config import load_config_for_plugin, load_plugin_config
from elodie.constants import application_directory
from elodie import constants as c
from elodie import log
@ -35,7 +35,7 @@ class PluginBase(object):
def __init__(self):
# Loads the config for the plugin from config.ini
self.config_for_plugin = load_config_for_plugin(self.__name__)
self.config_for_plugin = load_config_for_plugin(self.__name__, c.CONFIG_FILE)
self.db = PluginDb(self.__name__)
def after(self, file_path, destination_folder, final_file_path, metadata):
@ -65,7 +65,7 @@ class PluginDb(object):
"""
def __init__(self, plugin_name):
self.db_file = '{}/plugins/{}.json'.format(
application_directory,
c.application_directory,
plugin_name.lower()
)
@ -131,7 +131,7 @@ class Plugins(object):
if self.loaded == True:
return
plugin_list = load_plugin_config()
plugin_list = load_plugin_config(c.CONFIG_FILE)
for plugin in plugin_list:
plugin_lower = plugin.lower()
try:

14
elodie/tests/config_test.py Normal file → Executable file
View File

@ -14,7 +14,7 @@ sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirna
from elodie import constants
from elodie.config import load_config, load_plugin_config
@patch('elodie.config.config_file', '%s/config.ini-singleton-success' % gettempdir())
@patch('elodie.constants.CONFIG_FILE', '%s/config.ini-singleton-success' % gettempdir())
def test_load_config_singleton_success():
with open('%s/config.ini-singleton-success' % gettempdir(), 'w') as f:
f.write("""
@ -36,7 +36,7 @@ prefer_english_names=False
assert config['MapQuest']['key'] == 'new-value', config.get('MapQuest', 'key')
@patch('elodie.config.config_file', '%s/config.ini-does-not-exist' % gettempdir())
@patch('elodie.constants.CONFIG_FILE', '%s/config.ini-does-not-exist' % gettempdir())
def test_load_config_singleton_no_file():
if hasattr(load_config, 'config'):
del load_config.config
@ -48,7 +48,7 @@ def test_load_config_singleton_no_file():
assert config == {}, config
@patch('elodie.config.config_file', '%s/config.ini-load-plugin-config-unset-backwards-compat' % gettempdir())
@patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugin-config-unset-backwards-compat' % gettempdir())
def test_load_plugin_config_unset_backwards_compat():
with open('%s/config.ini-load-plugin-config-unset-backwards-compat' % gettempdir(), 'w') as f:
f.write("""
@ -63,7 +63,7 @@ def test_load_plugin_config_unset_backwards_compat():
assert plugins == [], plugins
@patch('elodie.config.config_file', '%s/config.ini-load-plugin-config-exists-not-set' % gettempdir())
@patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugin-config-exists-not-set' % gettempdir())
def test_load_plugin_config_exists_not_set():
with open('%s/config.ini-load-plugin-config-exists-not-set' % gettempdir(), 'w') as f:
f.write("""
@ -79,7 +79,7 @@ def test_load_plugin_config_exists_not_set():
assert plugins == [], plugins
@patch('elodie.config.config_file', '%s/config.ini-load-plugin-config-one' % gettempdir())
@patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugin-config-one' % gettempdir())
def test_load_plugin_config_one():
with open('%s/config.ini-load-plugin-config-one' % gettempdir(), 'w') as f:
f.write("""
@ -96,7 +96,7 @@ plugins=Dummy
assert plugins == ['Dummy'], plugins
@patch('elodie.config.config_file', '%s/config.ini-load-plugin-config-one-with-invalid' % gettempdir())
@patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugin-config-one-with-invalid' % gettempdir())
def test_load_plugin_config_one_with_invalid():
with open('%s/config.ini-load-plugin-config-one' % gettempdir(), 'w') as f:
f.write("""
@ -113,7 +113,7 @@ plugins=DNE
assert plugins == [], plugins
@patch('elodie.config.config_file', '%s/config.ini-load-plugin-config-many' % gettempdir())
@patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugin-config-many' % gettempdir())
def test_load_plugin_config_many():
with open('%s/config.ini-load-plugin-config-many' % gettempdir(), 'w') as f:
f.write("""

View File

@ -355,7 +355,7 @@ def test_import_directory_with_non_matching_exclude():
assert 'Success 1' in result.output, result.output
assert 'Error 0' in result.output, result.output
@mock.patch('elodie.config.config_file', '%s/config.ini-import-file-with-single-config-exclude' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-import-file-with-single-config-exclude' % gettempdir())
def test_import_file_with_single_config_exclude():
config_string = """
[Exclusions]
@ -382,7 +382,7 @@ def test_import_file_with_single_config_exclude():
assert 'Success 0' in result.output, result.output
assert 'Error 0' in result.output, result.output
@mock.patch('elodie.config.config_file', '%s/config.ini-import-file-with-multiple-config-exclude' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-import-file-with-multiple-config-exclude' % gettempdir())
def test_import_file_with_multiple_config_exclude():
config_string = """
[Exclusions]
@ -727,7 +727,7 @@ def test_verify_error():
assert origin in result.output, result.output
assert 'Error 1' in result.output, result.output
@mock.patch('elodie.config.config_file', '%s/config.ini-cli-batch-plugin-googlephotos' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-cli-batch-plugin-googlephotos' % gettempdir())
def test_cli_batch_plugin_googlephotos():
auth_file = helper.get_file('plugins/googlephotos/auth_file.json')
secrets_file = helper.get_file('plugins/googlephotos/secrets_file.json')

View File

@ -196,7 +196,7 @@ def test_get_file_name_definition_default():
assert name_template == '%date-%original_name-%title.%extension', name_template
assert definition == [[('date', '%Y-%m-%d_%H-%M-%S')], [('original_name', '')], [('title', '')], [('extension', '')]], definition #noqa
@mock.patch('elodie.config.config_file', '%s/config.ini-custom-filename' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-custom-filename' % gettempdir())
def test_get_file_name_definition_custom():
with open('%s/config.ini-custom-filename' % gettempdir(), 'w') as f:
f.write("""
@ -251,7 +251,7 @@ def test_get_file_name_with_uppercase_and_spaces():
assert file_name == helper.path_tz_fix('2015-12-05_00-59-26-plain-with-spaces-and-uppercase-123.jpg'), file_name
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-filename-custom' % gettempdir())
def test_get_file_name_custom():
with open('%s/config.ini-filename-custom' % gettempdir(), 'w') as f:
f.write("""
@ -271,7 +271,7 @@ name=%date-%original_name.%extension
assert file_name == helper.path_tz_fix('2015-12-dec-plain.jpg'), file_name
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-title' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-filename-custom-with-title' % gettempdir())
def test_get_file_name_custom_with_title():
with open('%s/config.ini-filename-custom-with-title' % gettempdir(), 'w') as f:
f.write("""
@ -291,7 +291,7 @@ name=%date-%original_name-%title.%extension
assert file_name == helper.path_tz_fix('2015-12-05-with-title-some-title.jpg'), file_name
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-empty-value' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-filename-custom-with-empty-value' % gettempdir())
def test_get_file_name_custom_with_empty_value():
with open('%s/config.ini-filename-custom-with-empty-value' % gettempdir(), 'w') as f:
f.write("""
@ -311,7 +311,7 @@ name=%date-%original_name-%title.%extension
assert file_name == helper.path_tz_fix('2015-12-05-plain.jpg'), file_name
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-lowercase' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-filename-custom-with-lowercase' % gettempdir())
def test_get_file_name_custom_with_lower_capitalization():
with open('%s/config.ini-filename-custom-with-lowercase' % gettempdir(), 'w') as f:
f.write("""
@ -332,7 +332,7 @@ capitalization=lower
assert file_name == helper.path_tz_fix('2015-12-05-plain.jpg'), file_name
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-invalidcase' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-filename-custom-with-invalidcase' % gettempdir())
def test_get_file_name_custom_with_invalid_capitalization():
with open('%s/config.ini-filename-custom-with-invalidcase' % gettempdir(), 'w') as f:
f.write("""
@ -353,7 +353,7 @@ capitalization=garabage
assert file_name == helper.path_tz_fix('2015-12-05-plain.jpg'), file_name
@mock.patch('elodie.config.config_file', '%s/config.ini-filename-custom-with-uppercase' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-filename-custom-with-uppercase' % gettempdir())
def test_get_file_name_custom_with_upper_capitalization():
with open('%s/config.ini-filename-custom-with-uppercase' % gettempdir(), 'w') as f:
f.write("""
@ -395,7 +395,7 @@ def test_get_folder_path_with_location():
assert path == os.path.join('2015-12-Dec','Sunnyvale'), path
@mock.patch('elodie.config.config_file', '%s/config.ini-original-with-camera-make-and-model' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-original-with-camera-make-and-model' % gettempdir())
def test_get_folder_path_with_camera_make_and_model():
with open('%s/config.ini-original-with-camera-make-and-model' % gettempdir(), 'w') as f:
f.write("""
@ -412,7 +412,7 @@ full_path=%camera_make/%camera_model
assert path == os.path.join('Canon', 'Canon EOS REBEL T2i'), path
@mock.patch('elodie.config.config_file', '%s/config.ini-original-with-camera-make-and-model-fallback' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-original-with-camera-make-and-model-fallback' % gettempdir())
def test_get_folder_path_with_camera_make_and_model_fallback():
with open('%s/config.ini-original-with-camera-make-and-model-fallback' % gettempdir(), 'w') as f:
f.write("""
@ -429,7 +429,7 @@ full_path=%camera_make|"nomake"/%camera_model|"nomodel"
assert path == os.path.join('nomake', 'nomodel'), path
@mock.patch('elodie.config.config_file', '%s/config.ini-int-in-component-path' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-int-in-component-path' % gettempdir())
def test_get_folder_path_with_int_in_config_component():
# gh-239
with open('%s/config.ini-int-in-component-path' % gettempdir(), 'w') as f:
@ -448,7 +448,7 @@ full_path=%date
assert path == os.path.join('2015'), path
@mock.patch('elodie.config.config_file', '%s/config.ini-combined-date-and-album' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-combined-date-and-album' % gettempdir())
def test_get_folder_path_with_combined_date_and_album():
# gh-239
with open('%s/config.ini-combined-date-and-album' % gettempdir(), 'w') as f:
@ -468,7 +468,7 @@ full_path=%custom
assert path == '2015-12-Dec Test Album', path
@mock.patch('elodie.config.config_file', '%s/config.ini-combined-date-album-location-fallback' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-combined-date-album-location-fallback' % gettempdir())
def test_get_folder_path_with_album_and_location_fallback():
# gh-279
with open('%s/config.ini-combined-date-album-location-fallback' % gettempdir(), 'w') as f:
@ -509,7 +509,7 @@ def test_get_folder_path_with_int_in_source_path():
assert path == os.path.join('2015-12-Dec','Unknown Location'), path
@mock.patch('elodie.config.config_file', '%s/config.ini-original-default-unknown-location' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-original-default-unknown-location' % gettempdir())
def test_get_folder_path_with_original_default_unknown_location():
with open('%s/config.ini-original-default-with-unknown-location' % gettempdir(), 'w') as f:
f.write('')
@ -523,7 +523,7 @@ def test_get_folder_path_with_original_default_unknown_location():
assert path == os.path.join('2015-12-Dec','Unknown Location'), path
@mock.patch('elodie.config.config_file', '%s/config.ini-custom-path' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-custom-path' % gettempdir())
def test_get_folder_path_with_custom_path():
with open('%s/config.ini-custom-path' % gettempdir(), 'w') as f:
f.write("""
@ -545,7 +545,7 @@ full_path=%date/%location
assert path == os.path.join('2015-12-05','United States of America-California-Sunnyvale'), path
@mock.patch('elodie.config.config_file', '%s/config.ini-fallback' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-fallback' % gettempdir())
def test_get_folder_path_with_fallback_folder():
with open('%s/config.ini-fallback' % gettempdir(), 'w') as f:
f.write("""
@ -565,7 +565,7 @@ full_path=%year/%month/%album|%"No Album Fool"/%month
assert path == os.path.join('2015','12','No Album Fool','12'), path
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-location-date' % gettempdir())
def test_get_folder_path_with_with_more_than_two_levels():
with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
f.write("""
@ -590,7 +590,7 @@ full_path=%year/%month/%location
assert path == os.path.join('2015','12','Sunnyvale, California'), path
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-location-date' % gettempdir())
def test_get_folder_path_with_with_only_one_level():
with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
f.write("""
@ -941,7 +941,7 @@ def test_process_video_with_album_then_title():
assert origin_checksum_preprocess == origin_checksum
assert helper.path_tz_fix(os.path.join('2015-01-Jan','test_album','2015-01-19_12-45-11-movie-test_title.mov')) in destination, destination
@mock.patch('elodie.config.config_file', '%s/config.ini-fallback-folder' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-fallback-folder' % gettempdir())
def test_process_file_fallback_folder():
with open('%s/config.ini-fallback-folder' % gettempdir(), 'w') as f:
f.write("""
@ -968,7 +968,7 @@ full_path=%date/%album|"fallback"
shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
@mock.patch('elodie.config.config_file', '%s/config.ini-multiple-directories' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-multiple-directories' % gettempdir())
def test_process_twice_more_than_two_levels_of_directories():
with open('%s/config.ini-multiple-directories' % gettempdir(), 'w') as f:
f.write("""
@ -1031,7 +1031,7 @@ def test_process_existing_file_without_changes():
shutil.rmtree(folder)
shutil.rmtree(os.path.dirname(os.path.dirname(destination)))
@mock.patch('elodie.config.config_file', '%s/config.ini-plugin-throw-error' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-plugin-throw-error' % gettempdir())
def test_process_file_with_plugin_throw_error():
with open('%s/config.ini-plugin-throw-error' % gettempdir(), 'w') as f:
f.write("""
@ -1056,7 +1056,7 @@ plugins=ThrowError
assert destination is None, destination
@mock.patch('elodie.config.config_file', '%s/config.ini-plugin-runtime-error' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-plugin-runtime-error' % gettempdir())
def test_process_file_with_plugin_runtime_error():
with open('%s/config.ini-plugin-runtime-error' % gettempdir(), 'w') as f:
f.write("""
@ -1168,7 +1168,7 @@ def test_should_exclude_with_complex_matching_regex():
result = filesystem.should_exclude('/var/folders/j9/h192v5v95gd_fhpv63qzyd1400d9ct/T/T497XPQH2R/UATR2GZZTX/2016-04-Apr/London/2016-04-07_11-15-26-valid-sample-title.txt', {re.compile('London.*\.txt$')})
assert result == True, result
@mock.patch('elodie.config.config_file', '%s/config.ini-does-not-exist' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-does-not-exist' % gettempdir())
def test_get_folder_path_definition_default():
if hasattr(load_config, 'config'):
del load_config.config
@ -1179,7 +1179,7 @@ def test_get_folder_path_definition_default():
assert path_definition == [[('date', '%Y-%m-%b')], [('album', ''), ('location', '%city'), ('"Unknown Location"', '')]], path_definition
@mock.patch('elodie.config.config_file', '%s/config.ini-date-location' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-date-location' % gettempdir())
def test_get_folder_path_definition_date_location():
with open('%s/config.ini-date-location' % gettempdir(), 'w') as f:
f.write("""
@ -1201,7 +1201,7 @@ full_path=%date/%location
assert path_definition == expected, path_definition
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-location-date' % gettempdir())
def test_get_folder_path_definition_location_date():
with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
f.write("""
@ -1223,7 +1223,7 @@ full_path=%location/%date
assert path_definition == expected, path_definition
@mock.patch('elodie.config.config_file', '%s/config.ini-cached' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-cached' % gettempdir())
def test_get_folder_path_definition_cached():
with open('%s/config.ini-cached' % gettempdir(), 'w') as f:
f.write("""
@ -1260,7 +1260,7 @@ full_path=%date/%location
if hasattr(load_config, 'config'):
del load_config.config
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-location-date' % gettempdir())
def test_get_folder_path_definition_with_more_than_two_levels():
with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
f.write("""
@ -1283,7 +1283,7 @@ full_path=%year/%month/%day
assert path_definition == expected, path_definition
@mock.patch('elodie.config.config_file', '%s/config.ini-location-date' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-location-date' % gettempdir())
def test_get_folder_path_definition_with_only_one_level():
with open('%s/config.ini-location-date' % gettempdir(), 'w') as f:
f.write("""
@ -1304,7 +1304,7 @@ full_path=%year
assert path_definition == expected, path_definition
@mock.patch('elodie.config.config_file', '%s/config.ini-multi-level-custom' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-multi-level-custom' % gettempdir())
def test_get_folder_path_definition_multi_level_custom():
with open('%s/config.ini-multi-level-custom' % gettempdir(), 'w') as f:
f.write("""

View File

@ -32,7 +32,8 @@ def checksum(file_path, blocksize=65536):
def create_working_folder(format=None):
temporary_folder = tempfile.gettempdir()
folder = os.path.join(temporary_folder, random_string(10, format), random_string(10, format))
folder = tempfile.TemporaryDirectory(prefix='elodie-').name
folder = os.path.join(folder, random_string(10, format))
os.makedirs(folder)
return (temporary_folder, folder)

View File

@ -33,7 +33,7 @@ config_string_fmt = config_string.format(
setup_module = helper.setup_module
teardown_module = helper.teardown_module
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-set-session' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-googlephotos-set-session' % gettempdir())
def test_googlephotos_set_session():
with open('%s/config.ini-googlephotos-set-session' % gettempdir(), 'w') as f:
f.write(config_string_fmt)
@ -49,7 +49,7 @@ def test_googlephotos_set_session():
gp.set_session()
assert gp.session is not None, gp.session
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-after-supported' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-googlephotos-after-supported' % gettempdir())
def test_googlephotos_after_supported():
with open('%s/config.ini-googlephotos-after-supported' % gettempdir(), 'w') as f:
f.write(config_string_fmt)
@ -69,7 +69,7 @@ def test_googlephotos_after_supported():
assert db_row == 'foobar', db_row
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-after-unsupported' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-googlephotos-after-unsupported' % gettempdir())
def test_googlephotos_after_unsupported():
with open('%s/config.ini-googlephotos-after-unsupported' % gettempdir(), 'w') as f:
f.write(config_string_fmt)
@ -89,7 +89,7 @@ def test_googlephotos_after_unsupported():
assert db_row == None, db_row
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-googlephotos-upload' % gettempdir())
def test_googlephotos_upload():
with open('%s/config.ini-googlephotos-upload' % gettempdir(), 'w') as f:
f.write(config_string_fmt)
@ -106,7 +106,7 @@ def test_googlephotos_upload():
assert status is not None, status
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload-session-fail' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-googlephotos-upload-session-fail' % gettempdir())
def test_googlephotos_upload_session_fail():
with open('%s/config.ini-googlephotos-upload-session-fail' % gettempdir(), 'w') as f:
f.write(config_string)
@ -123,7 +123,7 @@ def test_googlephotos_upload_session_fail():
assert status is None, status
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload-invalid-empty' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-googlephotos-upload-invalid-empty' % gettempdir())
def test_googlephotos_upload_invalid_empty():
with open('%s/config.ini-googlephotos-upload-invalid-empty' % gettempdir(), 'w') as f:
f.write(config_string_fmt)
@ -140,7 +140,7 @@ def test_googlephotos_upload_invalid_empty():
assert status is None, status
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-upload-dne' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-googlephotos-upload-dne' % gettempdir())
def test_googlephotos_upload_dne():
with open('%s/config.ini-googlephotos-upload-dne' % gettempdir(), 'w') as f:
f.write(config_string_fmt)
@ -157,7 +157,7 @@ def test_googlephotos_upload_dne():
assert status is None, status
@mock.patch('elodie.config.config_file', '%s/config.ini-googlephotos-batch' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-googlephotos-batch' % gettempdir())
def test_googlephotos_batch():
with open('%s/config.ini-googlephotos-batch' % gettempdir(), 'w') as f:
f.write(config_string_fmt)

View File

@ -11,7 +11,7 @@ from . import helper
from elodie.config import load_config
from elodie.plugins.plugins import Plugins, PluginBase, PluginDb
@mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-unset-backwards-compat' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugins-unset-backwards-compat' % gettempdir())
def test_load_plugins_unset_backwards_compat():
with open('%s/config.ini-load-plugins-unset-backwards-compat' % gettempdir(), 'w') as f:
f.write("""
@ -27,7 +27,7 @@ def test_load_plugins_unset_backwards_compat():
assert plugins.plugins == [], plugins.plugins
@mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-exists-not-set' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugins-exists-not-set' % gettempdir())
def test_load_plugins_exists_not_set():
with open('%s/config.ini-load-plugins-exists-not-set' % gettempdir(), 'w') as f:
f.write("""
@ -44,7 +44,7 @@ def test_load_plugins_exists_not_set():
assert plugins.plugins == [], plugins.plugins
@mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-one' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugins-one' % gettempdir())
def test_load_plugins_one():
with open('%s/config.ini-load-plugins-one' % gettempdir(), 'w') as f:
f.write("""
@ -63,7 +63,7 @@ plugins=Dummy
assert plugins.plugins == ['Dummy'], plugins.plugins
assert len(plugins.classes) == 1, len(plugins.classes)
@mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-one-with-invalid' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugins-one-with-invalid' % gettempdir())
def test_load_plugins_one_with_invalid():
with open('%s/config.ini-load-plugins-one' % gettempdir(), 'w') as f:
f.write("""
@ -82,7 +82,7 @@ plugins=DNE
assert plugins.plugins == [], plugins.plugins
assert len(plugins.classes) == 0, len(plugins.classes)
@mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-many' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugins-many' % gettempdir())
def test_load_plugins_many():
with open('%s/config.ini-load-plugins-many' % gettempdir(), 'w') as f:
f.write("""
@ -103,7 +103,7 @@ plugins=ThrowError,Dummy
assert plugins.classes['Dummy'].__name__ == 'Dummy', plugins.classes['Dummy'].__name__
assert len(plugins.classes) == 2, len(plugins.classes)
@mock.patch('elodie.config.config_file', '%s/config.ini-load-plugins-many-with-invalid' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-load-plugins-many-with-invalid' % gettempdir())
def test_load_plugins_set_many_with_invalid():
with open('%s/config.ini-load-plugins-many-with-invalid' % gettempdir(), 'w') as f:
f.write("""
@ -121,7 +121,7 @@ plugins=ThrowError,Dummy,DNE
assert plugins.plugins == ['ThrowError','Dummy'], plugins.plugins
@mock.patch('elodie.config.config_file', '%s/config.ini-run-before' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-run-before' % gettempdir())
def test_run_before():
with open('%s/config.ini-run-before' % gettempdir(), 'w') as f:
f.write("""
@ -143,7 +143,7 @@ plugins=Dummy
assert before_ran_1 == False, before_ran_1
assert before_ran_2 == True, before_ran_2
@mock.patch('elodie.config.config_file', '%s/config.ini-throw-error' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-throw-error' % gettempdir())
def test_throw_error():
with open('%s/config.ini-throw-error' % gettempdir(), 'w') as f:
f.write("""
@ -166,7 +166,7 @@ plugins=ThrowError
assert status_batch == False, status_batch
assert status_before == False, status_before
@mock.patch('elodie.config.config_file', '%s/config.ini-throw-error-one-of-many' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-throw-error-one-of-many' % gettempdir())
def test_throw_error_one_of_many():
with open('%s/config.ini-throw-error-one-of-many' % gettempdir(), 'w') as f:
f.write("""
@ -189,7 +189,7 @@ plugins=Dummy,ThrowError
assert status_batch == False, status_batch
assert status_before == False, status_before
@mock.patch('elodie.config.config_file', '%s/config.ini-throw-runtime-error' % gettempdir())
@mock.patch('elodie.constants.CONFIG_FILE', '%s/config.ini-throw-runtime-error' % gettempdir())
def test_throw_error_runtime_error():
with open('%s/config.ini-throw-runtime-error' % gettempdir(), 'w') as f:
f.write("""

View File

@ -33,6 +33,7 @@ if __name__ == "__main__":
test_argv = sys.argv
test_argv.append('--verbosity=2')
test_argv.append('-s')
result = nose.run(argv=test_argv)
if(result):
sys.exit(0)