11"""Module to read yaml or json conf"""
22import logging
33import os
4-
54from typing import Text
65
76import anyconfig
1312
1413
1514class 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 )
0 commit comments