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
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 rougail.object_model import CONVERT_OPTION
@ -32,6 +33,7 @@ class RougailUserDataEnvironment:
rougailconfig: RougailConfig=None,
user_datas=None,
):
# this is the tiramisu config object
self.config = config
if rougailconfig is None:
rougailconfig = RougailConfig
@ -49,6 +51,7 @@ class RougailUserDataEnvironment:
else:
self.errors = []
self.warnings = []
# FIXME à enlever
# this variable will be used for generating warnings
# about unused ROUGAIL_ environment variables
self.unused_environment_var = get_rougail_environment_var()
@ -57,6 +60,7 @@ class RougailUserDataEnvironment:
self.config.property.read_write()
self.parse(self.config)
self.config.property.read_only()
# FIXME à refaire avec le dico directement
# if there are unused environment variables
if len(self.unused_environment_var):
unused_environment_var_str = " ".join(self.unused_environment_var)
@ -67,36 +71,36 @@ class RougailUserDataEnvironment:
if option.isoptiondescription():
self.parse(option)
else:
# FIXME gerer les erreurs
# try except
# ValueError suivant le type
# PropertyError -> hidden ou frozen...
# test: mettre hidden à true dans le yaml
# pour les tests: mettre hidden à true dans le yaml
self.load_environment(option)
def load_environment(self, option):
# FIXME : mettre un .upper()
# pour que les var de env soient en maj
# FIXME : mettre un .upper() pour que les var de env soient en maj
option_name = option.path()
# FIXME : A REFAIRE
# this is used only for warning purposes
if option_name not in self.unused_environment_var:
return
# the rougail type is not the tiramisu type
# tiramisu type is option.type
# actually the rougail type **is not** the same thing as
# the tiramisu type, which is in `option.type`
option_type = option.information.get('type')
ismulti = option.ismulti()
# here we retrieve the conversion func of the considered type
# carefull, it could be None -> in this case, do nothing
# here we retrieve the type conversion function
# carefull, the coercion function 'func' could be None
# -> in this case, do nothing
type_obj = CONVERT_OPTION.get(option_type, {}).get("func")
self.unused_environment_var.remove(option_name)
# let's parse the environment variables values
# 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]
if ismulti:
# here we expect the bash option value of a multi to be coma separated:
# FIXME faire un strip() la dessus
option_bash_value = option_bash_value.split(",")
option_bash_value = [opt.strip() for opt in option_bash_value.split(",")]
if type_obj:
option_bash_value = [type_obj(opt) for opt in option_bash_value]
elif type_obj:

View file

@ -36,3 +36,19 @@ def get_rougail_environment_dict():
for var in get_rougail_environment_var():
rougail_environment_dict[var] = os.environ[rougail_environment_var + var]
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