Skip to content

Commit abc469a

Browse files
committed
Enabled empty conf or optional conf
1 parent a1e9c7b commit abc469a

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

pyms/config/conf.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@ class Config:
99
def __init__(self):
1010
pass
1111

12-
@property
13-
def config(self):
12+
def config(self, *args, **kwargs):
1413
"""Set the configuration, if our yaml file is like:
1514
myservice:
1615
myservice1:
1716
myvar1
1817
and we want to get the configuration of service1, our self.service will be "myservice.myservice1"
1918
"""
2019
if not self._config:
21-
self._config = ConfFile()
20+
self._config = ConfFile(*args, **kwargs)
2221
if not self.service:
2322
raise ServiceDoesNotExistException("Service not defined")
2423
return getattr(self._config, self.service)
2524

2625

27-
def get_conf(service=None):
26+
def get_conf(service=None, *args, **kwargs):
2827
config = Config()
2928
config.service = service
30-
return config.config
29+
return config.config(*args, **kwargs)

pyms/config/confile.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Module to read yaml or json conf"""
22
import logging
33
import os
4-
54
from typing import Text
65

76
import anyconfig
@@ -13,16 +12,23 @@
1312

1413

1514
class ConfFile(dict):
15+
empty_init = False
16+
1617
def __init__(self, *args, **kwargs):
1718
"""
1819
Get configuration from a dictionary(variable `config`), from path (variable `path`) or from
1920
environment with the constant `CONFIGMAP_FILE_ENVIRONMENT`
2021
"""
21-
22-
config = kwargs.get("config") or self._get_conf_from_file(kwargs.get("path")) or self._get_conf_from_env()
22+
self.empty_init = kwargs.get("empty_init", False)
23+
config = kwargs.get("config")
24+
if config is None:
25+
config = self._get_conf_from_file(kwargs.get("path")) or self._get_conf_from_env()
2326

2427
if not config:
25-
raise ConfigDoesNotFoundException("Configuration file not found")
28+
if self.empty_init:
29+
config = {}
30+
else:
31+
raise ConfigDoesNotFoundException("Configuration file not found")
2632

2733
logger.debug("[CONF] INIT: Settings {kwargs}".format(
2834
kwargs=kwargs,
@@ -35,7 +41,7 @@ def __init__(self, *args, **kwargs):
3541
def normalize_config(self, config):
3642
for key, item in config.items():
3743
if isinstance(item, dict):
38-
item = ConfFile(config=item)
44+
item = ConfFile(config=item, empty_init=self.empty_init)
3945
yield self.normalize_keys(key), item
4046

4147
def normalize_keys(self, key):
@@ -51,7 +57,10 @@ def __getattr__(self, name, *args, **kwargs):
5157
aux_dict = aux_dict[k]
5258
return aux_dict
5359
except KeyError:
54-
raise AttrDoesNotExistException("Variable {} not exist in the config file".format(name))
60+
if self.empty_init:
61+
return ConfFile(config={}, empty_init=self.empty_init)
62+
else:
63+
raise AttrDoesNotExistException("Variable {} not exist in the config file".format(name))
5564

5665
def _get_conf_from_env(self):
5766
file = os.environ.get(CONFIGMAP_FILE_ENVIRONMENT)

tests/tests_config.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import logging
2+
import os
23
import unittest
34

4-
import os
55
from pyms.config.confile import ConfFile
6-
from pyms.exceptions import AttrDoesNotExistException, ConfigDoesNotFoundException
76
from pyms.constants import CONFIGMAP_FILE_ENVIRONMENT, LOGGER_NAME
7+
from pyms.exceptions import AttrDoesNotExistException, ConfigDoesNotFoundException
88

99
logger = logging.getLogger(LOGGER_NAME)
1010

@@ -66,3 +66,17 @@ def test_example_test_yaml_file(self):
6666
def test_example_test_json_file(self):
6767
config = ConfFile(path=os.path.join(self.BASE_DIR, "config-tests.json"))
6868
self.assertEqual(config.my_ms.test_var, "general")
69+
70+
71+
class ConfNotExistTests(unittest.TestCase):
72+
def test_empty_conf(self):
73+
config = ConfFile(empty_init=True)
74+
self.assertEqual(config.my_ms, {})
75+
76+
def test_empty_conf_two_levels(self):
77+
config = ConfFile(empty_init=True)
78+
self.assertEqual(config.my_ms.level_two, {})
79+
80+
def test_empty_conf_three_levels(self):
81+
config = ConfFile(empty_init=True)
82+
self.assertEqual(config.my_ms.level_two.level_three, {})

0 commit comments

Comments
 (0)