rougail-output-console/src/rougail/output_exporter/__init__.py

165 lines
5.4 KiB
Python
Raw Normal View History

2024-07-28 17:52:11 +02:00
"""
Silique (https://www.silique.fr)
Copyright (C) 2022-2024
2024-10-31 18:55:00 +01:00
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/>.
2024-07-28 17:52:11 +02:00
"""
2024-09-04 09:04:08 +02:00
from typing import Optional
2024-07-28 17:52:11 +02:00
from tiramisu.error import PropertiesOptionError, ConfigError
from .config import OutPuts
2024-10-31 18:55:00 +01:00
from .i18n import _
2024-07-28 17:52:11 +02:00
class RougailOutputExporter:
2024-11-01 10:58:53 +01:00
def __init__(
self,
config: "Config",
rougailconfig: "RougailConfig" = None,
user_data_errors: Optional[list] = None,
user_data_warnings: Optional[list] = None,
) -> None:
2024-07-28 17:52:11 +02:00
if rougailconfig is None:
2024-10-31 18:55:00 +01:00
from rougail import RougailConfig
2024-11-01 10:58:53 +01:00
2024-07-28 17:52:11 +02:00
rougailconfig = RougailConfig
outputs = OutPuts().get()
2024-11-01 10:58:53 +01:00
output = rougailconfig["exporter.output_format"]
2024-07-28 17:52:11 +02:00
if output not in outputs:
2024-11-01 10:58:53 +01:00
raise Exception(
f'cannot find output "{output}", available outputs: {list(outputs)}'
)
2024-07-28 17:52:11 +02:00
self.rougailconfig = rougailconfig
2024-10-31 18:55:00 +01:00
self.config = config
2024-11-01 10:58:53 +01:00
self.read_write = self.rougailconfig["exporter.read_write"]
2024-07-28 17:52:11 +02:00
self.errors = []
self.warnings = []
2024-10-31 18:55:00 +01:00
if user_data_errors is None:
user_data_errors = []
2024-09-04 09:04:08 +02:00
self.user_data_errors = user_data_errors
2024-10-31 18:55:00 +01:00
if user_data_warnings is None:
user_data_warnings = []
2024-09-04 09:04:08 +02:00
self.user_data_warnings = user_data_warnings
2024-07-28 17:52:11 +02:00
self.formater = outputs[output](self.rougailconfig)
self.root = self.formater.root()
def mandatory(self):
2024-11-01 10:58:53 +01:00
if not self.rougailconfig["exporter.mandatory"]:
2024-07-28 17:52:11 +02:00
return
title = False
options_with_error = []
try:
2024-10-31 18:55:00 +01:00
mandatories = self.config.value.mandatory()
2024-07-28 17:52:11 +02:00
except (ConfigError, PropertiesOptionError) as err:
2024-11-01 10:58:53 +01:00
self.errors.append(f"Error in config: {err}")
2024-07-28 17:52:11 +02:00
return
for option in mandatories:
try:
option.value.get()
if not title:
2024-11-01 10:58:53 +01:00
# self.errors.append("Les variables suivantes sont obligatoires mais n'ont pas de valeur :")
self.errors.append(
_("The following variables are mandatory but have no value:")
)
2024-07-28 17:52:11 +02:00
title = True
2024-11-01 10:58:53 +01:00
self.errors.append(f" - {option.description()}")
2024-07-28 17:52:11 +02:00
except PropertiesOptionError:
options_with_error.append(option)
if not title:
for idx, option in enumerate(options_with_error):
if not idx:
2024-11-01 10:58:53 +01:00
# self.errors.append("Les variables suivantes sont inaccessibles mais sont vides et obligatoires :")
self.errors.append(
_(
"The following variables are inaccessible but are empty and mandatory :"
)
)
self.errors.append(f" - {option.description()}")
2024-07-28 17:52:11 +02:00
def exporter(self) -> bool:
2024-10-31 18:55:00 +01:00
self.config.property.read_write()
2024-07-28 17:52:11 +02:00
self.mandatory()
if self.read_write:
2024-10-31 18:55:00 +01:00
self.config.property.read_write()
2024-07-28 17:52:11 +02:00
else:
2024-10-31 18:55:00 +01:00
self.config.property.read_only()
2024-09-04 09:04:08 +02:00
errors = self.user_data_errors + self.errors
if errors:
self.formater.errors(errors)
2024-07-28 17:52:11 +02:00
if self.errors:
return False
2024-09-04 09:04:08 +02:00
warnings = self.user_data_warnings + self.warnings
if warnings:
self.formater.warnings(warnings)
2024-07-28 17:52:11 +02:00
self.formater.header()
2024-11-01 10:58:53 +01:00
self.parse_options(
self.config,
self.root,
)
2024-07-28 17:52:11 +02:00
self.formater.end()
return True
def print(self) -> None:
return self.formater.print()
2024-09-04 09:04:08 +02:00
def run(self) -> None:
self.exporter()
2024-10-31 18:55:00 +01:00
return self.print()
2024-09-04 09:04:08 +02:00
2024-11-01 10:58:53 +01:00
def parse_options(
self,
conf,
parent,
):
2024-07-28 17:52:11 +02:00
for option in conf:
if option.isoptiondescription():
family = parent.add_family(option)
if option.isleadership():
2024-11-01 10:58:53 +01:00
self.parse_leadership(
option,
family,
)
2024-07-28 17:52:11 +02:00
else:
2024-11-01 10:58:53 +01:00
self.parse_options(
option,
family,
)
2024-07-28 17:52:11 +02:00
else:
parent.add_variable(option)
2024-11-01 10:58:53 +01:00
def parse_leadership(
self,
conf,
parent,
):
2024-07-28 17:52:11 +02:00
leader, *followers = list(conf)
leader_values = leader.value.get()
2024-09-04 09:04:08 +02:00
for idx, leader_value in enumerate(leader_values):
leader_obj = parent.add_family(leader)
2024-11-01 10:58:53 +01:00
leader_obj.add_variable(
leader,
value=leader_value,
leader_index=idx,
)
2024-09-04 09:04:08 +02:00
for follower in followers:
if follower.index() != idx:
continue
leader_obj.add_variable(follower)
2024-10-31 18:55:00 +01:00
RougailOutput = RougailOutputExporter
2024-11-01 10:58:53 +01:00
__all__ = ("RougailOutputExporter",)