refactoring

This commit is contained in:
gwen 2024-09-10 12:07:22 +02:00
parent f315137936
commit 0b343f82d8
2 changed files with 31 additions and 11 deletions

View file

@ -19,6 +19,7 @@ You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
""" """
from .helper import get_rougail_environment
from .helper import get_rougail_environment_var, get_rougail_environment_dict from .helper import get_rougail_environment_var, get_rougail_environment_dict
from rougail.object_model import CONVERT_OPTION from rougail.object_model import CONVERT_OPTION
@ -32,6 +33,7 @@ class RougailUserDataEnvironment:
rougailconfig: RougailConfig=None, rougailconfig: RougailConfig=None,
user_datas=None, user_datas=None,
): ):
# this is the tiramisu config object
self.config = config self.config = config
if rougailconfig is None: if rougailconfig is None:
rougailconfig = RougailConfig rougailconfig = RougailConfig
@ -49,6 +51,7 @@ class RougailUserDataEnvironment:
else: else:
self.errors = [] self.errors = []
self.warnings = [] self.warnings = []
# FIXME à enlever
# this variable will be used for generating warnings # this variable will be used for generating warnings
# about unused ROUGAIL_ environment variables # about unused ROUGAIL_ environment variables
self.unused_environment_var = get_rougail_environment_var() self.unused_environment_var = get_rougail_environment_var()
@ -57,6 +60,7 @@ class RougailUserDataEnvironment:
self.config.property.read_write() self.config.property.read_write()
self.parse(self.config) self.parse(self.config)
self.config.property.read_only() self.config.property.read_only()
# FIXME à refaire avec le dico directement
# if there are unused environment variables # if there are unused environment variables
if len(self.unused_environment_var): if len(self.unused_environment_var):
unused_environment_var_str = " ".join(self.unused_environment_var) unused_environment_var_str = " ".join(self.unused_environment_var)
@ -67,36 +71,36 @@ class RougailUserDataEnvironment:
if option.isoptiondescription(): if option.isoptiondescription():
self.parse(option) self.parse(option)
else: else:
# FIXME gerer les erreurs
# try except # try except
# ValueError suivant le type # ValueError suivant le type
# PropertyError -> hidden ou frozen... # PropertyError -> hidden ou frozen...
# test: mettre hidden à true dans le yaml # pour les tests: mettre hidden à true dans le yaml
self.load_environment(option) self.load_environment(option)
def load_environment(self, option): def load_environment(self, option):
# FIXME : mettre un .upper() # FIXME : mettre un .upper() pour que les var de env soient en maj
# pour que les var de env soient en maj
option_name = option.path() option_name = option.path()
# FIXME : A REFAIRE
# this is used only for warning purposes # this is used only for warning purposes
if option_name not in self.unused_environment_var: if option_name not in self.unused_environment_var:
return return
# the rougail type is not the tiramisu type # actually the rougail type **is not** the same thing as
# tiramisu type is option.type # the tiramisu type, which is in `option.type`
option_type = option.information.get('type') option_type = option.information.get('type')
ismulti = option.ismulti() ismulti = option.ismulti()
# here we retrieve the conversion func of the considered type # here we retrieve the type conversion function
# carefull, the coercion function 'func' could be None
# carefull, it could be None -> in this case, do nothing # -> in this case, do nothing
type_obj = CONVERT_OPTION.get(option_type, {}).get("func") type_obj = CONVERT_OPTION.get(option_type, {}).get("func")
self.unused_environment_var.remove(option_name) self.unused_environment_var.remove(option_name)
# let's parse the environment variables values # let's parse the environment variables values
# FIXME enlever les deux fonction de helper # FIXME enlever les deux fonction de helper
# FIXME option_bash_value = self.unused_environment_var.pop(option_name # FIXME et faire un pop (un del plutot) option_bash_value = self.unused_environment_var.pop(option_name)
option_bash_value = get_rougail_environment_dict()[option_name] option_bash_value = get_rougail_environment_dict()[option_name]
if ismulti: if ismulti:
# here we expect the bash option value of a multi to be coma separated: # here we expect the bash option value of a multi to be coma separated:
# FIXME faire un strip() la dessus option_bash_value = [opt.strip() for opt in option_bash_value.split(",")]
option_bash_value = option_bash_value.split(",")
if type_obj: if type_obj:
option_bash_value = [type_obj(opt) for opt in option_bash_value] option_bash_value = [type_obj(opt) for opt in option_bash_value]
elif type_obj: elif type_obj:

View file

@ -36,3 +36,19 @@ def get_rougail_environment_dict():
for var in get_rougail_environment_var(): for var in get_rougail_environment_var():
rougail_environment_dict[var] = os.environ[rougail_environment_var + var] rougail_environment_dict[var] = os.environ[rougail_environment_var + var]
return rougail_environment_dict return rougail_environment_dict
def get_rougail_environment():
"""gets all the rougail environment variables and their values
:returns: rougail environment variables as a key/value dict
{MYVARIABLE: my_value}
"""
# first we look at all environment variables
all_envvar = os.environ
# then we filter the ROUGAIL_ environment variables
rougail_envvar = [envvar.replace(rougail_environment_var, '')
for envvar in all_envvar
if envvar.startswith(rougail_environment_var)]
for var in rougail_envvar:
rougail_environment_dict[var] = os.environ[rougail_environment_var + var]
return rougail_environment_dict