rougail-user-data-commandline/src/rougail/user_data_commandline/__init__.py

85 lines
2.8 KiB
Python

"""
Silique (https://www.silique.fr)
Copyright (C) 2025
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from tiramisu_cmdline_parser import TiramisuCmdlineParser
from rougail.error import ExtentionError
from .i18n import _
from .__version__ import __version__
class RougailUserDataCommandline:
"""Load parameter for commande line
"""
def __init__(
self,
config: "Config",
*,
rougailconfig: "RougailConfig" = None,
arguments = None,
**kwargs,
):
# this is the tiramisu config object
self.config = config.config.copy().unrestraint
if rougailconfig is None:
from rougail.config import RougailConfig
rougailconfig = RougailConfig
user_data = rougailconfig["step.user_data"]
if "commandline" not in user_data:
user_data.append("commandline")
rougailconfig["step.user_data"] = user_data
user_data = rougailconfig["step.user_data"]
if "commandline" not in user_data:
raise ExtentionError(_('"commandline" is not set in step.user_data'))
self.rougailconfig = rougailconfig
self.arguments = arguments
self.errors = []
self.warnings = []
self.parser = TiramisuCmdlineParser(
self.config,
exit_on_error=False,
**kwargs
)
def run(self):
try:
self.parser.parse_args(self.arguments, valid_mandatory=False)
except (SystemExit, ArgumentError) as err:
self.errors.append(str(err))
values = {}
for key in self.config.value.exportation():
option = self.config.option(key)
if not option.isfollower():
values[key] = self.config.option(key).value.get()
else:
values[key] = [self.config.option(key, index).value.get() for index in range(option.value.len())]
return [
{
"source": 'Commandline',
"errors": self.errors,
"warnings": self.warnings,
"values": values,
}
]
RougailUserData = RougailUserDataCommandline
__all__ = ("RougailUserDataCommandline",)