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

186 lines
5.6 KiB
Python
Raw Normal View History

"""
Silique (https://www.silique.fr)
Copyright (C) 2022-2026
2026-06-21 17:06:32 +02: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/>.
"""
from typing import Any, List, Optional
from json import dumps
2025-12-19 13:34:58 +01:00
from tiramisu import groups
from tiramisu.error import PropertiesOptionError, ConfigError
2025-11-21 08:03:34 +01:00
from rougail.error import ExtensionError
from .i18n import _
2025-04-09 21:35:28 +02:00
from .__version__ import __version__
class RougailOutputJson:
2025-05-11 19:12:15 +02:00
output_name = "json"
2024-12-02 20:24:01 +01:00
def __init__(
self,
config: "Config",
2024-12-02 20:24:01 +01:00
*,
rougailconfig: "RougailConfig" = None,
user_data_errors: Optional[list] = None,
user_data_warnings: Optional[list] = None,
true_config: "Config" = None,
2025-11-03 09:00:18 +01:00
**kwargs,
) -> None:
if rougailconfig is None:
from rougail import RougailConfig
2025-05-11 19:12:15 +02:00
rougailconfig = RougailConfig
2024-12-02 20:24:01 +01:00
rougailconfig["step.output"] = self.output_name
if rougailconfig["step.output"] != self.output_name:
2025-11-21 08:03:34 +01:00
raise ExtensionError(
2025-05-11 19:12:15 +02:00
_('the "step.output" is not set to "{0}"').format(self.output_name)
)
self.rougailconfig = rougailconfig
self.config = config
if true_config:
self.true_config = true_config
else:
self.true_config = config
2025-12-19 13:34:58 +01:00
try:
groups.namespace
self.support_namespace = True
except AttributeError:
self.support_namespace = False
2024-12-02 20:24:01 +01:00
if user_data_errors:
self.errors = user_data_errors
else:
self.errors = []
if user_data_warnings:
self.warnings = user_data_warnings
else:
self.warnings = []
2024-12-02 20:24:01 +01:00
def run(self) -> None:
2025-02-10 09:43:37 +01:00
ret = self.exporter()
if isinstance(self.dico, str):
value = self.dico
else:
value = dumps(self.dico, ensure_ascii=False, indent=2)
return ret, value
2024-12-02 20:24:01 +01:00
def print(self) -> str:
2025-02-10 09:43:37 +01:00
ret, data = self.run()
print(data)
return ret
2024-12-02 20:24:01 +01:00
def exporter(self) -> None:
2025-11-21 08:03:34 +01:00
self.dico = {}
self.manage_warnings()
2026-05-04 11:22:50 +02:00
if not self.manage_errors():
return False
if not self.config.isoptiondescription():
self.dico = self.config.value.get()
return True
2024-12-02 20:24:01 +01:00
self.parse_family(
self.config,
2024-12-02 20:24:01 +01:00
self.dico,
2025-12-19 13:34:58 +01:00
None,
2024-12-02 20:24:01 +01:00
)
2025-12-19 13:34:58 +01:00
return self.manage_errors()
2024-12-02 20:24:01 +01:00
def manage_warnings(self) -> None:
2026-01-20 15:02:07 +01:00
if not self.warnings:
return
warnings = []
for w in self.warnings:
if isinstance(w, dict):
msg, opt = next(iter(w.items()))
2026-06-21 17:06:32 +02:00
warnings.append(_("{0}: {1}").format(opt.path, msg))
2026-01-20 15:02:07 +01:00
else:
warnings.append(w)
self.dico["_warnings"] = warnings
2024-12-02 20:24:01 +01:00
def manage_errors(self) -> bool:
if not self.errors:
2026-01-20 15:02:07 +01:00
return True
errors = []
for e in self.errors:
if isinstance(e, dict):
msg, opt = next(iter(e.items()))
2026-06-21 17:06:32 +02:00
errors.append(_("{0}: {1}").format(opt.path, msg))
2026-01-20 15:02:07 +01:00
else:
errors.append(e)
2026-05-04 11:22:50 +02:00
self.dico["_errors"] = errors
2026-06-21 17:06:32 +02:00
# self.manage_warnings()
2026-01-20 15:02:07 +01:00
return False
def parse_family(
self,
parent,
parent_obj,
2025-12-19 13:34:58 +01:00
namespace,
):
for child in parent:
if child.isoptiondescription():
if child.isleadership():
child_obj = []
2026-06-11 08:34:32 +02:00
self.parse_sequence(
child,
child_obj,
)
else:
2026-06-21 17:06:32 +02:00
if (
namespace is None
and self.support_namespace
and child.group_type() is groups.namespace
2026-06-21 17:06:32 +02:00
):
subnamespace = child.name()
2025-12-19 13:34:58 +01:00
else:
subnamespace = namespace
child_obj = self.get_default_family(child)
self.parse_family(child, child_obj, subnamespace)
self.family_informations(parent, parent_obj, child, child_obj)
else:
self.variable_informations(parent_obj, child)
2026-06-11 08:34:32 +02:00
def parse_sequence(
self,
child,
obj,
):
leader, *followers = list(child)
leader_values = leader.value.get()
for idx, leader_value in enumerate(leader_values):
leader_dict = {leader.name(): leader_value}
obj.append(leader_dict)
for follower in list(followers):
if follower.index() != idx:
continue
followers.remove(follower)
leader_dict[follower.name()] = follower.value.get()
def get_default_family(self, child):
return dict()
def family_informations(self, parent, parent_obj, child, child_obj):
parent_obj[child.name()] = child_obj
def variable_informations(self, parent_obj, child):
parent_obj[child.name()] = child.value.get()
RougailOutput = RougailOutputJson
__all__ = ("RougailOutputJson",)