2024-11-28 08:19:41 +01:00
|
|
|
"""
|
|
|
|
|
Silique (https://www.silique.fr)
|
2026-01-12 09:33:41 +01:00
|
|
|
Copyright (C) 2022-2026
|
2026-06-21 17:06:32 +02:00
|
|
|
|
2024-11-28 08:19:41 +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/>.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from typing import Any, List, Optional
|
|
|
|
|
|
|
|
|
|
from json import dumps
|
|
|
|
|
|
2025-12-19 13:34:58 +01:00
|
|
|
from tiramisu import groups
|
2024-11-28 08:19:41 +01:00
|
|
|
from tiramisu.error import PropertiesOptionError, ConfigError
|
2025-11-21 08:03:34 +01:00
|
|
|
from rougail.error import ExtensionError
|
2024-11-28 08:19:41 +01:00
|
|
|
|
|
|
|
|
from .i18n import _
|
2025-04-09 21:35:28 +02:00
|
|
|
from .__version__ import __version__
|
2024-11-28 08:19:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class RougailOutputJson:
|
2025-05-11 19:12:15 +02:00
|
|
|
output_name = "json"
|
2024-12-02 20:24:01 +01:00
|
|
|
|
2024-11-28 08:19:41 +01:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
config: "Config",
|
2024-12-02 20:24:01 +01:00
|
|
|
*,
|
2024-11-28 08:19:41 +01:00
|
|
|
rougailconfig: "RougailConfig" = None,
|
|
|
|
|
user_data_errors: Optional[list] = None,
|
|
|
|
|
user_data_warnings: Optional[list] = None,
|
2026-01-12 09:33:41 +01:00
|
|
|
true_config: "Config" = None,
|
2025-11-03 09:00:18 +01:00
|
|
|
**kwargs,
|
2024-11-28 08:19:41 +01:00
|
|
|
) -> None:
|
|
|
|
|
if rougailconfig is None:
|
|
|
|
|
from rougail import RougailConfig
|
2025-05-11 19:12:15 +02:00
|
|
|
|
2024-11-28 08:19:41 +01: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)
|
|
|
|
|
)
|
2024-11-28 08:19:41 +01:00
|
|
|
self.rougailconfig = rougailconfig
|
|
|
|
|
self.config = config
|
2026-01-12 09:33:41 +01:00
|
|
|
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-11-28 08:19:41 +01:00
|
|
|
|
2024-12-02 20:24:01 +01:00
|
|
|
def run(self) -> None:
|
2025-02-10 09:43:37 +01:00
|
|
|
ret = self.exporter()
|
2025-03-19 10:57:56 +01:00
|
|
|
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 = {}
|
2025-03-19 10:57:56 +01:00
|
|
|
self.manage_warnings()
|
2026-05-04 11:22:50 +02:00
|
|
|
if not self.manage_errors():
|
|
|
|
|
return False
|
2026-01-12 09:33:41 +01:00
|
|
|
if not self.config.isoptiondescription():
|
|
|
|
|
self.dico = self.config.value.get()
|
|
|
|
|
return True
|
2024-12-02 20:24:01 +01:00
|
|
|
self.parse_family(
|
2026-01-12 09:33:41 +01:00
|
|
|
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-11-28 08:19:41 +01:00
|
|
|
|
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
|
2024-11-28 08:19:41 +01:00
|
|
|
|
|
|
|
|
def parse_family(
|
|
|
|
|
self,
|
2026-07-06 19:52:07 +02:00
|
|
|
parent,
|
|
|
|
|
parent_obj,
|
2025-12-19 13:34:58 +01:00
|
|
|
namespace,
|
2024-11-28 08:19:41 +01:00
|
|
|
):
|
2026-07-06 19:52:07 +02:00
|
|
|
for child in parent:
|
|
|
|
|
if child.isoptiondescription():
|
|
|
|
|
if child.isleadership():
|
|
|
|
|
child_obj = []
|
2026-06-11 08:34:32 +02:00
|
|
|
self.parse_sequence(
|
2026-07-06 19:52:07 +02:00
|
|
|
child,
|
|
|
|
|
child_obj,
|
2024-11-28 08:19:41 +01:00
|
|
|
)
|
|
|
|
|
else:
|
2026-06-21 17:06:32 +02:00
|
|
|
if (
|
|
|
|
|
namespace is None
|
|
|
|
|
and self.support_namespace
|
2026-07-06 19:52:07 +02:00
|
|
|
and child.group_type() is groups.namespace
|
2026-06-21 17:06:32 +02:00
|
|
|
):
|
2026-07-06 19:52:07 +02:00
|
|
|
subnamespace = child.name()
|
2025-12-19 13:34:58 +01:00
|
|
|
else:
|
|
|
|
|
subnamespace = namespace
|
2026-07-06 19:52:07 +02:00
|
|
|
child_obj = self.get_default_family(child)
|
|
|
|
|
self.parse_family(child, child_obj, subnamespace)
|
|
|
|
|
self.family_informations(parent, parent_obj, child, child_obj)
|
2024-11-28 08:19:41 +01:00
|
|
|
else:
|
2026-07-06 19:52:07 +02:00
|
|
|
self.variable_informations(parent_obj, child)
|
2024-11-28 08:19:41 +01:00
|
|
|
|
2026-06-11 08:34:32 +02:00
|
|
|
def parse_sequence(
|
2024-11-28 08:19:41 +01:00
|
|
|
self,
|
2026-07-06 19:52:07 +02:00
|
|
|
child,
|
|
|
|
|
obj,
|
2024-11-28 08:19:41 +01:00
|
|
|
):
|
2026-07-06 19:52:07 +02:00
|
|
|
leader, *followers = list(child)
|
2024-11-28 08:19:41 +01:00
|
|
|
leader_values = leader.value.get()
|
|
|
|
|
for idx, leader_value in enumerate(leader_values):
|
|
|
|
|
leader_dict = {leader.name(): leader_value}
|
2026-07-06 19:52:07 +02:00
|
|
|
obj.append(leader_dict)
|
2024-11-28 08:19:41 +01:00
|
|
|
for follower in list(followers):
|
|
|
|
|
if follower.index() != idx:
|
|
|
|
|
continue
|
|
|
|
|
followers.remove(follower)
|
|
|
|
|
leader_dict[follower.name()] = follower.value.get()
|
|
|
|
|
|
2026-07-06 19:52:07 +02:00
|
|
|
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()
|
|
|
|
|
|
2024-11-28 08:19:41 +01:00
|
|
|
|
|
|
|
|
RougailOutput = RougailOutputJson
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ("RougailOutputJson",)
|