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

108 lines
4 KiB
Python
Raw Normal View History

2024-09-04 16:40:46 +02:00
"""
Silique (https://www.silique.fr)
2025-02-10 09:30:20 +01:00
Copyright (C) 2024-2025
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
"""
2025-05-11 19:10:08 +02:00
2024-11-23 11:08:24 +01:00
import os
2025-06-18 06:38:04 +02:00
from rougail.tiramisu import CONVERT_OPTION
2024-09-04 16:40:46 +02:00
from rougail.config import RougailConfig
2025-11-21 07:59:17 +01:00
from rougail.error import ExtensionError
2024-09-04 16:40:46 +02:00
from tiramisu.error import ValueOptionError
2024-12-09 10:13:44 +01:00
2024-09-04 16:50:15 +02:00
class RougailUserDataEnvironment:
2025-05-11 19:10:08 +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
2025-05-11 19:10:08 +02:00
user_data = rougailconfig["step.user_data"]
if "environment" not in user_data:
user_data.append("environment")
rougailconfig["step.user_data"] = user_data
user_data = rougailconfig["step.user_data"]
if "environment" not in user_data:
2025-11-21 07:59:17 +01:00
raise ExtensionError("environment is not set in step.user_data")
2025-10-29 11:16:37 +01:00
if "environment.with_secrets" in rougailconfig:
self.with_secrets = rougailconfig["environment.with_secrets"]
2025-10-05 21:25:12 +02:00
else:
self.with_secrets = True
2025-10-29 11:16:37 +01:00
self.custom_separator = rougailconfig["environment.custom_separator"]
if not rougailconfig["main_namespace"]:
self.default_environment_name = rougailconfig["environment.default_environment_name"]
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()
2025-05-11 19:10:08 +02:00
return [
{
"source": "environment variable",
"errors": self.errors,
"warnings": self.warnings,
"values": values,
"options": {
"multi_separator": ",",
"needs_convert": True,
2025-10-05 21:25:12 +02:00
"allow_secrets_variables": self.with_secrets,
2025-05-11 19:10:08 +02:00
},
}
]
2024-11-23 11:08:24 +01:00
def parse(self):
variables = {}
2025-10-29 11:16:37 +01:00
self.prefixes = []
2024-11-23 11:08:24 +01:00
for option in self.config:
2024-11-27 16:39:46 +01:00
if not option.isoptiondescription() or option.group_type() != "namespace":
break
2025-10-29 11:16:37 +01:00
self.prefixes.append(option.name().upper() + ".")
2024-11-27 16:39:46 +01:00
else:
2025-10-29 11:16:37 +01:00
return self.get_rougail_environment(0)
# no namespace then we filter the ROUGAIL_ environment variables
self.prefixes = [self.default_environment_name + "_"]
return self.get_rougail_environment(len(self.prefixes[0]))
2024-09-04 16:40:46 +02:00
2025-10-29 11:16:37 +01:00
def get_rougail_environment(self, len_env):
"""gets all the rougail environment variables and their values
2024-09-24 16:42:03 +02:00
2025-10-29 11:16:37 +01:00
:sample: {'VARINT': '5', 'VARNAME34': '58, 22', 'VARNAME2': 'tata',
'VARNAME1': 'titi', 'MYFAMILY.VARNAME3': 'spam'}
:returns: rougail environment variables as a key/value dict
"""
return {
envvar[len_env:].lower(): envval
for envvar, envval in self.get_correct_envs()
}
2024-09-24 16:42:03 +02:00
2025-10-29 11:16:37 +01:00
def get_correct_envs(self):
for envvar, envval in os.environ.items():
if self.custom_separator:
envvar = envvar.replace(self.custom_separator, '.')
for prefix in self.prefixes:
if envvar.startswith(prefix):
yield envvar, envval
for prefix in self.prefixes:
if envvar.startswith(prefix):
yield envvar, envval