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

126 lines
5.3 KiB
Python
Raw Normal View History

2024-09-04 16:40:46 +02:00
#!/usr/bin/env python3
"""
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-09 11:39:49 +02:00
from .helper import get_rougail_environment_var, get_rougail_environment_dict
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
):
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 = []
# this variable will be used for generating warnings
# about unused ROUGAIL_ environment variables
self.unused_environment_var = get_rougail_environment_var()
2024-09-04 16:40:46 +02:00
def run(self):
self.config.property.read_write()
2024-09-04 16:50:15 +02:00
if self.rougailconfig['environment.mandatory']:
2024-09-04 16:40:46 +02:00
current_titles = []
while True:
mandatories = self.config.value.mandatory()
if not mandatories:
break
mandatory = mandatories[0]
path = mandatory.path()
if '.' in path:
current_config = self.config
for idx, p in enumerate(path.split('.')[0:-1]):
current_config = current_config.option(p)
if idx < len(current_titles):
if current_titles[idx] == p:
continue
current_titles = current_titles[0:idx]
current_titles.append(p)
self.print(current_config.description(), idx)
2024-09-04 16:50:15 +02:00
self.display_environment(mandatory)
2024-09-04 16:40:46 +02:00
else:
self.parse(self.config)
self.config.property.read_only()
2024-09-10 09:13:17 +02:00
# if there are unused environment variables
if len(self.unused_environment_var):
unused_environment_var_str = " ".join(self.unused_environment_var)
self.warnings.append("the rougail environment variables were not used : " + unused_environment_var_str)
2024-09-04 16:40:46 +02:00
def parse(self, config, title_level=0):
for option in config:
if option.isoptiondescription():
self.print(option.description(), title_level)
self.parse(option, title_level + 1)
else:
2024-09-04 16:50:15 +02:00
self.display_environment(option)
2024-09-04 16:40:46 +02:00
def print(self, title, title_level):
2024-09-09 15:19:10 +02:00
print(' ' * title_level + '📂 ' + title, 'bold')
2024-09-04 16:40:46 +02:00
2024-09-04 16:50:15 +02:00
def display_environment(self, option):
2024-09-04 16:40:46 +02:00
kwargs = {}
option_type = option.information.get('type')
default = option.value.get()
ismulti = option.ismulti()
# here we retrieve the conversion func of the considered type
2024-09-10 09:20:55 +02:00
# carefull, it 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-09 10:21:29 +02:00
rougail_environment_var = get_rougail_environment_var()
option_name = option.path()
2024-09-10 09:13:17 +02:00
# this is used only for warning purposes
self.unused_environment_var.remove(option_name)
# let's parse the environment variables values
2024-09-09 10:21:29 +02:00
if option_name in rougail_environment_var:
if ismulti:
2024-09-09 11:39:49 +02:00
option_bash_value = get_rougail_environment_dict()[option_name]
# here we expect the bash option value of a multi to be coma separated:
2024-09-09 11:39:49 +02:00
option_value = option_bash_value.split(",")
if type_obj is not None:
option_value_typed = [type_obj(opt) for opt in option_value]
option.value.set(option_value_typed)
else:
option.value.set(option_value)
2024-09-09 11:39:49 +02:00
else:
option_bash_value = get_rougail_environment_dict()[option_name]
if type_obj is not None:
option.value.set(type_obj(option_bash_value))
else:
2024-09-10 09:13:17 +02:00
option.value.set(option_bash_value)