Skip to content

Commit 385bb93

Browse files
author
jmrivas
committed
Added anyconfig to make agnostic configuration file
1 parent 77d667c commit 385bb93

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

pyms/config/confile.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from typing import Text
66

7-
import yaml
7+
import anyconfig
88

99
from pyms.constants import CONFIGMAP_FILE_ENVIRONMENT, LOGGER_NAME
1010
from pyms.exceptions import AttrDoesNotExistException, ConfigDoesNotFoundException
@@ -19,7 +19,7 @@ def __init__(self, *args, **kwargs):
1919
environment with the constant `CONFIGMAP_FILE_ENVIRONMENT`
2020
"""
2121

22-
config = kwargs.get("config") or self._get_conf_from_yaml_file(kwargs.get("path")) or self._get_conf_from_env()
22+
config = kwargs.get("config") or self._get_conf_from_file(kwargs.get("path")) or self._get_conf_from_env()
2323

2424
if not config:
2525
raise ConfigDoesNotFoundException("Configuration file not found")
@@ -56,19 +56,14 @@ def __getattr__(self, name, *args, **kwargs):
5656
def _get_conf_from_env(self):
5757
file = os.environ.get(CONFIGMAP_FILE_ENVIRONMENT)
5858
logger.info("[CONF] Searching file in ENV[{}]: {}...".format(CONFIGMAP_FILE_ENVIRONMENT, file))
59-
return self._get_conf_from_yaml_file(os.environ.get(CONFIGMAP_FILE_ENVIRONMENT))
59+
return self._get_conf_from_file(os.environ.get(CONFIGMAP_FILE_ENVIRONMENT))
6060

61-
def _get_conf_from_yaml_file(self, path: Text) -> dict:
61+
def _get_conf_from_file(self, path: Text) -> dict:
6262
if not path or not os.path.isfile(path):
6363
return {}
6464
logger.info("[CONF] Configmap {} found".format(path))
65-
f = open(path, "r")
66-
conf = self._get_conf_from_yaml(f.read())
67-
f.close()
65+
conf = anyconfig.load(path)
6866
return conf
6967

70-
def _get_conf_from_yaml(self, config: Text) -> dict:
71-
return yaml.safe_load(config)
72-
7368
def __setattr__(self, name, value, *args, **kwargs):
7469
super(ConfFile, self).__setattr__(name, value)

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ PyYAML==3.13
44
# Optionals:
55
connexion==2.0.0
66
flask==1.0.2
7-
Flask-OpenTracing==0.2.0
7+
Flask-OpenTracing==0.2.0
8+
anyconfig==0.9.7

tests/config-tests.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"my-ms": {
3+
"DEBUG": true,
4+
"TESTING": true,
5+
"APP_NAME": "Python Microservice",
6+
"APPLICATION_ROOT": "/",
7+
"test_var": "general",
8+
"subservice1": {
9+
"test": "input"
10+
},
11+
"subservice2": {
12+
"test": "output"
13+
}
14+
}
15+
}

tests/tests_config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def test_example_test_file_from_env(self):
2424

2525

2626
class ConfTests(unittest.TestCase):
27+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
2728

2829
def test_dictionary_replace_key(self):
2930
config = ConfFile(config={"test-1": "a", "test_2": "b"})
@@ -57,3 +58,11 @@ def test_example_test_config_not_exixsts(self):
5758
def test_example_test_file_not_exists(self):
5859
with self.assertRaises(ConfigDoesNotFoundException):
5960
config = ConfFile(path="path/not/exist.yml")
61+
62+
def test_example_test_yaml_file(self):
63+
config = ConfFile(path=os.path.join(self.BASE_DIR, "config-tests.yml"))
64+
self.assertEqual(config.my_ms.test_var, "general")
65+
66+
def test_example_test_json_file(self):
67+
config = ConfFile(path=os.path.join(self.BASE_DIR, "config-tests.json"))
68+
self.assertEqual(config.my_ms.test_var, "general")

0 commit comments

Comments
 (0)