rougail-user-data-environment/src/rougail/user_data_environment/data.py

107 lines
4.3 KiB
Python
Raw Normal View History

2024-09-04 16:40:46 +02:00
"""
Silique (https://www.silique.fr)
Copyright (C) 2024
2024-09-04 16:50:15 +02:00
2024-09-04 16:40:46 +02:00
distribued with GPL-2 or later license
2024-09-04 16:50:15 +02:00
2024-09-04 16:40:46 +02:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
2024-09-04 16:50:15 +02:00
2024-09-04 16:40:46 +02:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2024-09-04 16:50:15 +02:00
2024-09-04 16:40:46 +02:00
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
"""
2024-09-10 12:07:22 +02:00
from .helper import get_rougail_environment
2024-09-09 11:39:49 +02:00
2024-09-04 16:40:46 +02:00
from rougail.object_model import CONVERT_OPTION
from rougail.config import RougailConfig
from tiramisu.error import ValueOptionError
2024-09-04 16:50:15 +02:00
class RougailUserDataEnvironment:
2024-09-04 16:40:46 +02:00
def __init__(self,
config: 'Config',
*,
rougailconfig: RougailConfig=None,
2024-09-10 09:13:17 +02:00
user_datas=None,
2024-09-04 16:40:46 +02:00
):
2024-09-10 12:07:22 +02:00
# this is the tiramisu config object
2024-09-04 16:40:46 +02:00
self.config = config
if rougailconfig is None:
rougailconfig = RougailConfig
user_data = rougailconfig['step.user_data']
2024-09-04 16:50:15 +02:00
if 'environment' not in user_data:
user_data.append('environment')
2024-09-04 16:40:46 +02:00
rougailconfig['step.user_data'] = user_data
user_data = rougailconfig['step.user_data']
2024-09-04 16:50:15 +02:00
if 'environment' not in user_data:
raise Exception('environment is not set in step.user_data')
2024-09-04 16:40:46 +02:00
self.rougailconfig = rougailconfig
2024-09-10 09:13:17 +02:00
if user_datas:
self.errors = user_datas['errors']
self.warnings = user_datas['warnings']
else:
self.errors = []
self.warnings = []
2024-09-10 15:35:00 +02:00
# bash environment variables (key/value pairs)
self.rougail_environment_vars = get_rougail_environment()
2024-09-04 16:40:46 +02:00
def run(self):
self.config.property.read_write()
2024-09-18 11:00:47 +02:00
self.parse(self.config.unrestraint)
2024-09-04 16:40:46 +02:00
self.config.property.read_only()
2024-09-10 15:35:00 +02:00
# about unused bash environment variables
if len(self.rougail_environment_vars):
unused_env_var_str = ", ".join(self.rougail_environment_vars.keys())
self.warnings.append("the following rougail bash environment "
"variable(s) are not used : " + unused_env_var_str)
2024-09-04 16:40:46 +02:00
2024-09-10 09:51:22 +02:00
def parse(self, config):
2024-09-04 16:40:46 +02:00
for option in config:
if option.isoptiondescription():
2024-09-10 09:59:32 +02:00
self.parse(option)
2024-09-04 16:40:46 +02:00
else:
2024-09-10 12:07:22 +02:00
# FIXME gerer les erreurs
2024-09-10 10:25:15 +02:00
# try except
2024-09-10 11:21:41 +02:00
# ValueError suivant le type
# PropertyError -> hidden ou frozen...
2024-09-10 12:07:22 +02:00
# pour les tests: mettre hidden à true dans le yaml
2024-09-10 16:15:28 +02:00
# try:
2024-09-10 09:51:22 +02:00
self.load_environment(option)
2024-09-10 16:15:28 +02:00
# except Exception as exc:
# print(exc)
2024-09-10 09:51:22 +02:00
def load_environment(self, option):
2024-09-10 10:25:15 +02:00
option_name = option.path()
2024-09-10 12:32:04 +02:00
# the bash variable are in upper case
option_name = option_name.upper()
2024-09-10 15:09:05 +02:00
# if the option is not in the bash environment, do nothing
2024-09-10 15:35:00 +02:00
if option_name not in self.rougail_environment_vars.keys():
2024-09-10 10:25:15 +02:00
return
2024-09-10 12:07:22 +02:00
# actually the rougail type **is not** the same thing as
# the tiramisu type, which is in `option.type`
2024-09-04 16:40:46 +02:00
option_type = option.information.get('type')
ismulti = option.ismulti()
2024-09-10 12:07:22 +02:00
# here we retrieve the type conversion function
# carefull, the coercion function 'func' could be None
# -> in this case, do nothing
2024-09-04 16:40:46 +02:00
type_obj = CONVERT_OPTION.get(option_type, {}).get("func")
2024-09-10 15:35:00 +02:00
option_bash_value = self.rougail_environment_vars.pop(option_name)
2024-09-10 10:07:00 +02:00
if ismulti:
# here we expect the bash option value of a multi to be coma separated:
2024-09-10 12:07:22 +02:00
option_bash_value = [opt.strip() for opt in option_bash_value.split(",")]
2024-09-10 10:15:02 +02:00
if type_obj:
2024-09-10 11:21:41 +02:00
option_bash_value = [type_obj(opt) for opt in option_bash_value]
2024-09-10 10:15:02 +02:00
elif type_obj:
2024-09-10 10:13:23 +02:00
option_bash_value = type_obj(option_bash_value)
2024-09-10 16:15:28 +02:00
try:
option.value.set(option_bash_value)
except Exception as exc:
print(exc)