rougail-output-json/src/rougail/output_json/__init__.py

147 lines
4.7 KiB
Python
Raw Normal View History

"""
Silique (https://www.silique.fr)
Copyright (C) 2022-2024
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 typing import Any, List, Optional
from json import dumps
from tiramisu import undefined
from tiramisu.error import PropertiesOptionError, ConfigError
from .i18n import _
class RougailOutputJson:
def __init__(
self,
config: "Config",
rougailconfig: "RougailConfig" = None,
user_data_errors: Optional[list] = None,
user_data_warnings: Optional[list] = None,
) -> None:
if rougailconfig is None:
from rougail import RougailConfig
rougailconfig = RougailConfig
self.rougailconfig = rougailconfig
self.config = config
2024-11-30 15:50:28 +01:00
self.errors = []
self.warnings = []
self.read_write = self.rougailconfig["json.read_write"]
self.is_mandatory = self.rougailconfig["json.mandatory"]
self.dico = {}
def mandatory(self):
if not self.is_mandatory:
return
title = False
options_with_error = []
try:
mandatories = self.config.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) -> None:
self.config.property.read_write()
self.mandatory()
if self.errors:
self.dico = {"_errors": self.errors}
return
if self.warnings:
self.dico["_warnings"] = self.warnings
if self.read_write:
self.config.property.read_write()
else:
self.config.property.read_only()
self.parse_family(
self.config,
self.dico,
)
def run(self) -> None:
self.exporter()
return dumps(self.dico, ensure_ascii=False, indent=2) + '\n'
def print(self) -> str:
print(self.run())
def parse_family(
self,
conf,
child,
):
for option in conf:
if option.isoptiondescription():
if option.isleadership():
parent = []
self.parse_leadership(
option,
parent,
)
else:
parent = {}
self.parse_family(option, parent)
child[option.name()] = parent
else:
child[option.name()] = option.value.get()
def parse_leadership(
self,
conf,
parent,
):
leader, *followers = list(conf)
leader_values = leader.value.get()
for idx, leader_value in enumerate(leader_values):
leader_dict = {leader.name(): leader_value}
parent.append(leader_dict)
for follower in list(followers):
if follower.index() != idx:
continue
followers.remove(follower)
leader_dict[follower.name()] = follower.value.get()
RougailOutput = RougailOutputJson
__all__ = ("RougailOutputJson",)