55import os
66import re
77
8- LINUX_KEY_VARIABLE_PATTERN = r'\$([a-zA-Z][\w]+|\{[a-zA-Z][\w]+\})$'
8+ VARIABLE_PATTERN = r'\$([a-zA-Z][\w]+|\{[a-zA-Z][\w]+\})$'
99DEFAULT_CONFIG_FILES = ('config.json' , 'config.yaml' , 'config.yml' )
1010ENTITY_NAME_PATTERN = r'^[a-zA-Z][\w]+$'
1111SUPPORTED_EXTENSIONS = {
@@ -23,7 +23,7 @@ class ConfigFileNotFoundError(ConfigError):
2323 pass
2424
2525
26- class ConfigValue :
26+ class Config :
2727
2828 def __getitem__ (self , item ):
2929 return self .__dict__ [item ]
@@ -41,58 +41,52 @@ def values(self):
4141 return self .__dict__ .values ()
4242
4343
44- class Config :
45- __instance = None
46- __hold_an_instance = True
44+ class Configparser :
45+ def __init__ (self ):
46+ self .__instance = None
47+ self .__hold_an_instance = True
4748
48- @classmethod
49- def hold_an_instance (cls ):
50- return cls .__hold_an_instance
49+ @property
50+ def hold_an_instance (self ):
51+ return self .__hold_an_instance
5152
52- @classmethod
53- def set_hold_an_instance ( cls , value ):
53+ @hold_an_instance . setter
54+ def hold_an_instance ( self , value ):
5455 if type (value ) is not bool :
5556 raise ValueError ('value must be a bool' )
56- cls .__hold_an_instance = value
57+ self .__hold_an_instance = value
5758
58- def __new__ (cls , * args , ** kwargs ):
59- raise RuntimeError ('A instance of config is not allowed, use Config.get_config() instead' )
59+ def get_config (self , schema : dict = None , config_dir : str = 'config' , file_name : Any = DEFAULT_CONFIG_FILES ):
6060
61- @classmethod
62- def get_config (cls , schema : dict = None , config_dir : str = 'config' , file_name : Any = DEFAULT_CONFIG_FILES ):
63-
64- if cls .__instance is None :
65- instance = cls .__create_new_instance (schema , config_dir , file_name )
66- if cls .__hold_an_instance :
67- cls .__instance = instance
61+ if self .__instance is None :
62+ instance = self .__create_new_instance (schema , config_dir , file_name )
63+ if self .__hold_an_instance :
64+ self .__instance = instance
6865 else :
6966 return instance
70- return cls .__instance
67+ return self .__instance
7168
72- @classmethod
73- def __create_new_instance (cls , schema , config_dir , file_name ):
74- file_path = cls .__get_file_path (config_dir , file_name )
75- parser = cls .__get_file_parser (file_path )
76- file_buff = cls .__get_file_buff (file_path )
69+ def __create_new_instance (self , schema , config_dir , file_name ):
70+ file_path = self .__get_file_path (config_dir , file_name )
71+ parser = self .__get_file_parser (file_path )
72+ file_buff = self .__get_file_buff (file_path )
7773
7874 try :
79- config = cls .__validate_schema (schema , parser (file_buff ))
80- return cls .__dict_2_obj (config )
75+ config = self .__validate_schema (schema , parser (file_buff ))
76+ return self .__dict_2_obj (config )
8177 except ParseError as e :
8278 raise ConfigError (e )
8379 except SchemaError as e :
8480 raise ConfigError ('Schema validation error' , e )
8581
86- @classmethod
87- def __get_file_parser (cls , file_path ):
82+ def __get_file_parser (self , file_path ):
8883 try :
8984 extension = file_path .split ('.' )[- 1 ]
9085 return SUPPORTED_EXTENSIONS [extension ]
9186 except KeyError :
9287 raise ConfigError (f'Supported extensions: { list (SUPPORTED_EXTENSIONS .keys ())} ' )
9388
94- @classmethod
95- def __get_file_path (cls , config_dir , file_name ):
89+ def __get_file_path (self , config_dir , file_name ):
9690 file_path = f'{ os .getcwd ()} /{ config_dir } /'
9791 if type (file_name ) is str :
9892 file_name = [file_name ]
@@ -103,49 +97,52 @@ def __get_file_path(cls, config_dir, file_name):
10397
10498 raise ConfigFileNotFoundError (f'Config file { file_path } { file_name } was not found' )
10599
106- @classmethod
107- def __validate_schema (cls , schema , config_obj ):
100+ def __validate_schema (self , schema , config_obj ):
108101 if schema is None :
109102 return config_obj
110103 elif type (schema ) not in (dict , list ):
111104 raise ConfigError ('The first config\' s schema element should be a Map or a List' )
112105
113106 return Schema (schema ).validate (config_obj )
114107
115- @classmethod
116- def __get_file_buff (cls , path_file : str ):
108+ def __get_file_buff (self , path_file : str ):
117109 with open (path_file , 'r' ) as f :
118110 return f .read ()
119111
120- @classmethod
121- def __dict_2_obj (cls , data : Any ):
112+ def __dict_2_obj (self , data : Any ):
122113 _type = type (data )
123114
124115 if _type is dict :
125- obj = ConfigValue ()
116+ obj = Config ()
126117 for key , value in data .items ():
127- if re .search (ENTITY_NAME_PATTERN , key ) is None :
128- raise ConfigError (
129- f'The key { key } is invalid. The entity keys only may have words, number and underscores.' )
130- setattr (obj , key , cls .__dict_2_obj (value ))
118+ self .__is_a_valid_object_key (key )
119+ setattr (obj , key , self .__dict_2_obj (value ))
131120 return obj
132121 if _type in (list , set , tuple ):
133- return list (map (lambda v : cls .__dict_2_obj (v ), data ))
122+ return list (map (lambda v : self .__dict_2_obj (v ), data ))
134123 else :
135- if type ( data ) is str and re . search ( LINUX_KEY_VARIABLE_PATTERN , data ) is not None :
136- return cls . interpol_variable (data )
124+ if self . __is_variable ( data ):
125+ return self . __interpol_variable (data )
137126 return data
138127
139- @classmethod
140- def interpol_variable (cls , data ):
128+ def __is_a_valid_object_key (self , key ):
129+ if re .search (ENTITY_NAME_PATTERN , key ) is None :
130+ raise ConfigError (f'The key { key } is invalid. The entity keys only may have words, number and underscores.' )
131+
132+ def __is_variable (self , data ):
133+ return type (data ) is str and re .search (VARIABLE_PATTERN , data ) is not None
134+
135+ def __interpol_variable (self , data ):
141136 try :
142- return os .environ [cls . extract_env_variable_key (data )]
137+ return os .environ [self . __extract_env_variable_key (data )]
143138 except KeyError :
144139 raise ConfigError (f'Environment variable { data } was not found' )
145140
146- @classmethod
147- def extract_env_variable_key (cls , variable ):
141+ def __extract_env_variable_key (self , variable ):
148142 variable = variable [1 :]
149143 if variable [0 ] == '{' :
150144 return variable [1 :- 1 ]
151145 return variable
146+
147+
148+ configparser = Configparser ()
0 commit comments