Skip to content

Commit ba509ad

Browse files
authored
Fix. Load config file from env (#92)
If you set a config file from env, but exists a default file. PyMS load the default file first instead the env file
1 parent 31fc58e commit ba509ad

File tree

6 files changed

+15
-16
lines changed

6 files changed

+15
-16
lines changed

pyms/cmd/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def get_input(msg): # pragma: no cover
7272
def run(self):
7373
crypt = Crypt()
7474
if self.create_key:
75-
path = crypt._loader.get_or_setpath() # pylint: disable=protected-access
75+
path = crypt._loader.get_path_from_env() # pylint: disable=protected-access
7676
pwd = self.get_input('Type a password to generate the key file: ')
7777
generate_file = self.get_input('Do you want to generate a file in {}? [Y/n]'.format(path))
7878
generate_file = generate_file.lower() != "n"

pyms/config/confile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def __init__(self, *args, **kwargs):
4444
if self._empty_init:
4545
config = {}
4646
else:
47-
raise ConfigDoesNotFoundException("Configuration file not found")
47+
path = self._loader.path if self._loader.path else ""
48+
raise ConfigDoesNotFoundException("Configuration file {}not found".format(path + " "))
4849

4950
config = self.set_config(config)
5051

pyms/utils/crypt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ def decrypt(self, encrypted):
5454
return str(decrypted, encoding="utf-8")
5555

5656
def delete_key(self):
57-
os.remove(self._loader.get_or_setpath())
57+
os.remove(self._loader.get_path_from_env())

pyms/utils/files.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,24 @@ def __init__(self, path, env_var, default_filename):
1919
self.default_file = default_filename
2020

2121
def get_file(self, fn=None):
22-
return self._get_conf_from_file(fn) or self._get_conf_from_env(fn)
22+
return self._get_conf_from_env(fn) or self._get_conf_from_file(self.path, fn)
2323

2424
def put_file(self, content, mode="w"):
25-
self.get_or_setpath()
26-
file_to_write = open(self.path, mode)
25+
path = self.get_path_from_env()
26+
file_to_write = open(path, mode)
2727
file_to_write.write(content) # The key is type bytes still
2828
file_to_write.close()
2929

30-
def get_or_setpath(self):
30+
def get_path_from_env(self):
3131
config_file = os.environ.get(self.file_env_location, self.default_file)
3232
logger.debug("Searching file in ENV[{}]: {}...".format(self.file_env_location, config_file))
33-
self.path = config_file
34-
return self.path
33+
return config_file
3534

3635
def _get_conf_from_env(self, fn=None):
37-
self.get_or_setpath()
38-
return self._get_conf_from_file(fn)
39-
40-
def _get_conf_from_file(self, fn=None):
41-
path = self.path
36+
path = self.get_path_from_env()
37+
return self._get_conf_from_file(path, fn)
4238

39+
def _get_conf_from_file(self, path, fn=None):
4340
if path and os.path.isdir(path):
4441
path = os.path.join(path, self.default_file)
4542

@@ -48,6 +45,7 @@ def _get_conf_from_file(self, fn=None):
4845
return {}
4946
if path not in files_cached:
5047
logger.debug("[CONF] Configmap {} found".format(path))
48+
self.path = path
5149
if fn:
5250
files_cached[path] = fn(path)
5351
else:

tests/test_cmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_crypt_file_error(self):
1919
cmd = Command(arguments=arguments, autorun=False)
2020
with pytest.raises(FileDoesNotExistException) as excinfo:
2121
cmd.run()
22-
assert ("Decrypt key key.key not exists. You must set a correct env var KEY_FILE or run "
22+
assert ("Decrypt key None not exists. You must set a correct env var KEY_FILE or run "
2323
"`pyms crypt create-key` command") \
2424
in str(excinfo.value)
2525

tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_crypt_file_error(self):
3131
crypt = Crypt()
3232
with pytest.raises(FileDoesNotExistException) as excinfo:
3333
crypt.read_key()
34-
assert ("Decrypt key key.key not exists. You must set a correct env var KEY_FILE or run "
34+
assert ("Decrypt key None not exists. You must set a correct env var KEY_FILE or run "
3535
"`pyms crypt create-key` command") \
3636
in str(excinfo.value)
3737

0 commit comments

Comments
 (0)