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

142 lines
5.3 KiB
Python
Raw Normal View History

2024-07-28 17:52:11 +02:00
"""
Silique (https://www.silique.fr)
Copyright (C) 2022-2024
distribued with GPL-2 or later license
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.
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.
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-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 rougail import RougailConfig
from .config import OutPuts
from .utils import _
class RougailOutputExporter:
def __init__(self,
conf: 'Config',
rougailconfig: RougailConfig=None,
2024-09-04 09:04:08 +02:00
user_data_errors: Optional[list]=None,
user_data_warnings: Optional[list]=None,
2024-07-28 17:52:11 +02:00
) -> None:
if rougailconfig is None:
rougailconfig = RougailConfig
outputs = OutPuts().get()
output = rougailconfig['exporter.output_format']
if output not in outputs:
raise Exception(f'cannot find output "{output}", available outputs: {list(outputs)}')
self.rougailconfig = rougailconfig
self.conf = conf
self.read_write = self.rougailconfig['exporter.read_write']
self.errors = []
self.warnings = []
2024-09-04 09:04:08 +02:00
self.user_data_errors = user_data_errors
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-09-04 09:04:08 +02:00
if not self.rougailconfig['exporter.mandatory']:
2024-07-28 17:52:11 +02:00
return
title = False
options_with_error = []
try:
mandatories = self.conf.value.mandatory()
except (ConfigError, PropertiesOptionError) as err:
self.errors.append(f'Error in config: {err}')
return
for option in mandatories:
try:
option.value.get()
if not title:
#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:"))
title = True
self.errors.append(f' - {option.description()}')
except PropertiesOptionError:
options_with_error.append(option)
if not title:
for idx, option in enumerate(options_with_error):
if not idx:
#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()}')
def exporter(self) -> bool:
self.conf.property.read_write()
2024-07-28 17:52:11 +02:00
self.mandatory()
if self.read_write:
self.conf.property.read_write()
else:
self.conf.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()
self.parse_options(self.conf,
self.root,
)
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()
self.print()
2024-07-28 17:52:11 +02:00
def parse_options(self,
conf,
parent,
):
for option in conf:
if option.isoptiondescription():
family = parent.add_family(option)
if option.isleadership():
self.parse_leadership(option,
family,
)
else:
self.parse_options(option,
family,
)
else:
parent.add_variable(option)
def parse_leadership(self,
conf,
parent,
):
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)
leader_obj.add_variable(leader,
value=leader_value,
leader_index=idx,
)
for follower in followers:
if follower.index() != idx:
continue
leader_obj.add_variable(follower)