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

93 lines
3.5 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-11-23 11:08:24 +01:00
import os
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 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-11-23 11:08:24 +01:00
self.rougailconfig = rougailconfig
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-11-23 11:08:24 +01:00
self.errors = []
self.warnings = []
2024-09-04 16:40:46 +02:00
def run(self):
2024-11-23 11:08:24 +01:00
values = self.parse()
return [{'source': 'environment',
'errors': self.errors,
'warnings': self.warnings,
'values': values,
'options': {'multi_separator': ',',
'needs_convert': True,
},
}]
def parse(self):
variables = {}
found_ns = False
for option in self.config:
if option.group_type() == "namespace":
found_ns = True
variables.update(get_rougail_environment(option.name()))
if not found_ns:
if self.rougailconfig['main_namespace'] is None:
return get_rougail_environment(self.rougailconfig['environment.default_environment_name'])
return get_rougail_environment(self.rougailconfig['main_namespace'])
return variables
2024-09-04 16:40:46 +02:00
2024-09-24 16:42:03 +02:00
2024-11-23 11:08:24 +01:00
def get_rougail_environment(namespace):
"""gets all the rougail environment variables and their values
2024-09-24 16:42:03 +02:00
2024-11-23 11:08:24 +01:00
:sample: {'VARINT': '5', 'VARNAME34': '58, 22', 'VARNAME2': 'tata',
'VARNAME1': 'titi', 'MYFAMILY.VARNAME3': 'spam'}
:returns: rougail environment variables as a key/value dict
"""
# first we look at all environment variables
all_envvar = os.environ
# then we filter the ROUGAIL_ environment variables
if namespace is None:
rougail_environment_var = rougail_default_environment_var.upper() + '_'
len_env = len(rougail_environment_var) + 1
root = ''
else:
rougail_environment_var = namespace.upper() + '_'
len_env = len(rougail_environment_var)
root = namespace.lower() + '.'
return {root + envvar[len_env:].lower(): envval
for envvar, envval in all_envvar.items()
if envvar.startswith(rougail_environment_var)}