127 lines
4.7 KiB
Python
127 lines
4.7 KiB
Python
"""
|
|
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
|
|
"""
|
|
|
|
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,
|
|
) -> 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 = []
|
|
self.formater = outputs[output](self.rougailconfig)
|
|
self.root = self.formater.root()
|
|
|
|
def mandatory(self):
|
|
if self.rougailconfig['exporter.no_mandatory']:
|
|
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.mandatory()
|
|
if self.read_write:
|
|
self.conf.property.read_write()
|
|
else:
|
|
self.conf.property.read_only()
|
|
if self.errors:
|
|
self.formater.errors(self.errors)
|
|
return False
|
|
if self.warnings:
|
|
self.formater.warnings(self.warnings)
|
|
self.formater.header()
|
|
self.parse_options(self.conf,
|
|
self.root,
|
|
)
|
|
self.formater.end()
|
|
return True
|
|
|
|
def print(self) -> None:
|
|
return self.formater.print()
|
|
|
|
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)
|
|
idx = -1
|
|
leader_values = leader.value.get()
|
|
for follower in followers:
|
|
if idx != follower.index():
|
|
idx += 1
|
|
leader_obj = parent.add_family(leader)
|
|
leader_obj.add_variable(leader,
|
|
value=follower.value.get(),
|
|
)
|
|
leader_obj.add_variable(follower)
|