fix: black
This commit is contained in:
parent
7a4414bf13
commit
7fb47d1105
5 changed files with 276 additions and 239 deletions
|
@ -5,12 +5,11 @@ requires = ["flit_core >=3.8.0,<4"]
|
|||
[project]
|
||||
name = "rougail.output_exporter"
|
||||
version = "0.0.0"
|
||||
authors = [
|
||||
{name = "Emmanuel Garette", email = "gnunux@gnunux.info"},
|
||||
]
|
||||
description = "Rougail output exporter"
|
||||
authors = [{name = "Emmanuel Garette", email = "gnunux@gnunux.info"}]
|
||||
readme = "README.md"
|
||||
description = "Rougail output exporter"
|
||||
requires-python = ">=3.8"
|
||||
license = {file = "LICENSE"}
|
||||
classifiers = [
|
||||
"License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)",
|
||||
"Programming Language :: Python",
|
||||
|
@ -39,3 +38,4 @@ tag_format = "$version"
|
|||
version_scheme = "pep440"
|
||||
version_provider = "pep621"
|
||||
update_changelog_on_bump = true
|
||||
changelog_merge_prerelease = true
|
||||
|
|
|
@ -23,22 +23,26 @@ from .i18n import _
|
|||
|
||||
|
||||
class RougailOutputExporter:
|
||||
def __init__(self,
|
||||
config: 'Config',
|
||||
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
|
||||
outputs = OutPuts().get()
|
||||
output = rougailconfig['exporter.output_format']
|
||||
output = rougailconfig["exporter.output_format"]
|
||||
if output not in outputs:
|
||||
raise Exception(f'cannot find output "{output}", available outputs: {list(outputs)}')
|
||||
raise Exception(
|
||||
f'cannot find output "{output}", available outputs: {list(outputs)}'
|
||||
)
|
||||
self.rougailconfig = rougailconfig
|
||||
self.config = config
|
||||
self.read_write = self.rougailconfig['exporter.read_write']
|
||||
self.read_write = self.rougailconfig["exporter.read_write"]
|
||||
self.errors = []
|
||||
self.warnings = []
|
||||
if user_data_errors is None:
|
||||
|
@ -51,31 +55,37 @@ class RougailOutputExporter:
|
|||
self.root = self.formater.root()
|
||||
|
||||
def mandatory(self):
|
||||
if not self.rougailconfig['exporter.mandatory']:
|
||||
if not self.rougailconfig["exporter.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}')
|
||||
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:"))
|
||||
self.errors.append(
|
||||
_("The following variables are mandatory but have no value:")
|
||||
)
|
||||
title = True
|
||||
self.errors.append(f' - {option.description()}')
|
||||
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()}')
|
||||
self.errors.append(
|
||||
_(
|
||||
"The following variables are inaccessible but are empty and mandatory :"
|
||||
)
|
||||
)
|
||||
self.errors.append(f" - {option.description()}")
|
||||
|
||||
def exporter(self) -> bool:
|
||||
self.config.property.read_write()
|
||||
|
@ -93,7 +103,8 @@ class RougailOutputExporter:
|
|||
if warnings:
|
||||
self.formater.warnings(warnings)
|
||||
self.formater.header()
|
||||
self.parse_options(self.config,
|
||||
self.parse_options(
|
||||
self.config,
|
||||
self.root,
|
||||
)
|
||||
self.formater.end()
|
||||
|
@ -106,7 +117,8 @@ class RougailOutputExporter:
|
|||
self.exporter()
|
||||
return self.print()
|
||||
|
||||
def parse_options(self,
|
||||
def parse_options(
|
||||
self,
|
||||
conf,
|
||||
parent,
|
||||
):
|
||||
|
@ -114,17 +126,20 @@ class RougailOutputExporter:
|
|||
if option.isoptiondescription():
|
||||
family = parent.add_family(option)
|
||||
if option.isleadership():
|
||||
self.parse_leadership(option,
|
||||
self.parse_leadership(
|
||||
option,
|
||||
family,
|
||||
)
|
||||
else:
|
||||
self.parse_options(option,
|
||||
self.parse_options(
|
||||
option,
|
||||
family,
|
||||
)
|
||||
else:
|
||||
parent.add_variable(option)
|
||||
|
||||
def parse_leadership(self,
|
||||
def parse_leadership(
|
||||
self,
|
||||
conf,
|
||||
parent,
|
||||
):
|
||||
|
@ -132,7 +147,8 @@ class RougailOutputExporter:
|
|||
leader_values = leader.value.get()
|
||||
for idx, leader_value in enumerate(leader_values):
|
||||
leader_obj = parent.add_family(leader)
|
||||
leader_obj.add_variable(leader,
|
||||
leader_obj.add_variable(
|
||||
leader,
|
||||
value=leader_value,
|
||||
leader_index=idx,
|
||||
)
|
||||
|
@ -145,4 +161,4 @@ class RougailOutputExporter:
|
|||
RougailOutput = RougailOutputExporter
|
||||
|
||||
|
||||
__all__ = ('RougailOutputExporter',)
|
||||
__all__ = ("RougailOutputExporter",)
|
||||
|
|
|
@ -17,6 +17,7 @@ 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 pathlib import Path
|
||||
from rougail.utils import load_modules
|
||||
|
||||
|
@ -25,18 +26,20 @@ OUTPUTS = None
|
|||
|
||||
|
||||
def get_outputs() -> None:
|
||||
module_name = 'rougail.output_exporter.output'
|
||||
module_name = "rougail.output_exporter.output"
|
||||
outputs = {}
|
||||
for path in (Path(__file__).parent / 'output').iterdir():
|
||||
for path in (Path(__file__).parent / "output").iterdir():
|
||||
name = path.name
|
||||
if not name.endswith(".py") or name.endswith("__.py"):
|
||||
continue
|
||||
module = load_modules(module_name + '.' + name, str(path))
|
||||
module = load_modules(module_name + "." + name, str(path))
|
||||
if "Formater" not in dir(module):
|
||||
continue
|
||||
level = module.Formater.level
|
||||
if level in outputs:
|
||||
raise Exception(f'duplicated level rougail-exporter for output "{level}": {module.Formater.name} and {outputs[level].name}')
|
||||
raise Exception(
|
||||
f'duplicated level rougail-exporter for output "{level}": {module.Formater.name} and {outputs[level].name}'
|
||||
)
|
||||
outputs[module.Formater.level] = module.Formater
|
||||
return {outputs[level].name: outputs[level] for level in sorted(outputs)}
|
||||
|
||||
|
@ -55,7 +58,8 @@ class OutPuts: # pylint: disable=R0903
|
|||
return OUTPUTS
|
||||
|
||||
|
||||
def get_rougail_config(*,
|
||||
def get_rougail_config(
|
||||
*,
|
||||
backward_compatibility=True,
|
||||
) -> dict:
|
||||
outputs = tuple(OutPuts().get())
|
||||
|
@ -88,14 +92,17 @@ exporter:
|
|||
alternative_name: eo
|
||||
default: DEFAULT
|
||||
choices:
|
||||
""".replace('DEFAULT', outputs[0])
|
||||
""".replace(
|
||||
"DEFAULT", outputs[0]
|
||||
)
|
||||
for output in outputs:
|
||||
options += f" - {output}\n"
|
||||
return {'name': 'exporter',
|
||||
'process': 'output',
|
||||
'options': options,
|
||||
'level': 40,
|
||||
return {
|
||||
"name": "exporter",
|
||||
"process": "output",
|
||||
"options": options,
|
||||
"level": 40,
|
||||
}
|
||||
|
||||
|
||||
__all__ = ("OutPuts", 'get_rougail_config')
|
||||
__all__ = ("OutPuts", "get_rougail_config")
|
||||
|
|
|
@ -2,23 +2,23 @@
|
|||
Silique (https://www.silique.fr)
|
||||
Copyright (C) 2024
|
||||
|
||||
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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
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.
|
||||
|
||||
Mtools 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.
|
||||
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 General Public License
|
||||
along with Mtools. If not, see <http://www.gnu.org/licenses/>.
|
||||
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 gettext import translation
|
||||
from pathlib import Path
|
||||
|
||||
t = translation('rougail_output_exporter', str(Path(__file__).parent / 'locale'))
|
||||
t = translation("rougail_output_exporter", str(Path(__file__).parent / "locale"), fallback=True)
|
||||
|
||||
_ = t.gettext
|
||||
|
|
|
@ -28,25 +28,26 @@ from ...i18n import _
|
|||
|
||||
|
||||
class Formater:
|
||||
name = 'console'
|
||||
name = "console"
|
||||
level = 10
|
||||
variable_hidden_color = 'orange1'
|
||||
variable_advanced_color = 'bright_blue'
|
||||
variable_advanced_and_modified_color = 'red1'
|
||||
value_unmodified_color = 'gold1'
|
||||
value_default_color = 'green'
|
||||
variable_hidden_color = "orange1"
|
||||
variable_advanced_color = "bright_blue"
|
||||
variable_advanced_and_modified_color = "red1"
|
||||
value_unmodified_color = "gold1"
|
||||
value_default_color = "green"
|
||||
|
||||
def __init__(self,
|
||||
rougailconfig: 'RougailConfig',
|
||||
def __init__(
|
||||
self,
|
||||
rougailconfig: "RougailConfig",
|
||||
) -> None:
|
||||
self.console = Console(force_terminal=True)
|
||||
self.rougailconfig = rougailconfig
|
||||
self.read_write = self.rougailconfig['exporter.read_write']
|
||||
self.show_secrets = self.rougailconfig['exporter.show_secrets']
|
||||
self.read_write = self.rougailconfig["exporter.read_write"]
|
||||
self.show_secrets = self.rougailconfig["exporter.show_secrets"]
|
||||
self.out = []
|
||||
|
||||
def header(self):
|
||||
header_variable = 'Variable\n'
|
||||
header_variable = "Variable\n"
|
||||
# header_variable += f'[{self.variable_advanced_color}]Variable non documentée[/{self.variable_advanced_color}]\n'
|
||||
# header_variable += f'[{self.variable_advanced_and_modified_color}]Variable non documentée mais modifiée[/{self.variable_advanced_and_modified_color}]'
|
||||
header_variable += f'[{self.variable_advanced_color}]{_("Undocumented variable")}[/{self.variable_advanced_color}]\n'
|
||||
|
@ -58,24 +59,27 @@ class Formater:
|
|||
# header_value += 'Valeur modifiée\n'
|
||||
# header_value += f'([{self.value_default_color}]Valeur par défaut originale[/{self.value_default_color}])'
|
||||
header_value = f'[{self.value_unmodified_color}]{_("Default value")}[/{self.value_unmodified_color}]\n'
|
||||
header_value += _('Modified value') + '\n'
|
||||
header_value += _("Modified value") + "\n"
|
||||
header_value += f'([{self.value_default_color}]{_("Original default value")}[/{self.value_default_color}])'
|
||||
header = Table.grid(padding=1, collapse_padding=True)
|
||||
header.pad_edge = False
|
||||
header.add_row(header_variable, header_value)
|
||||
self.out.append(Panel.fit(header, title=_("Caption")))
|
||||
|
||||
def errors(self,
|
||||
def errors(
|
||||
self,
|
||||
errors,
|
||||
) -> None:
|
||||
tree = Tree(":stop_sign: ERRORS",
|
||||
tree = Tree(
|
||||
":stop_sign: ERRORS",
|
||||
guide_style="bold bright_red",
|
||||
)
|
||||
for error in errors:
|
||||
tree.add(error)
|
||||
self.out.append(tree)
|
||||
|
||||
def warnings(self,
|
||||
def warnings(
|
||||
self,
|
||||
warnings: list,
|
||||
) -> None:
|
||||
tree = Tree(":warning: WARNINGS")
|
||||
|
@ -84,7 +88,8 @@ class Formater:
|
|||
self.out.append(tree)
|
||||
|
||||
def root(self) -> None:
|
||||
self.output = OutputFamily(_("Variables:"),
|
||||
self.output = OutputFamily(
|
||||
_("Variables:"),
|
||||
None,
|
||||
self,
|
||||
no_icon=True,
|
||||
|
@ -100,31 +105,30 @@ class Formater:
|
|||
|
||||
|
||||
class OutputFamily:
|
||||
def __init__(self,
|
||||
family,
|
||||
parent,
|
||||
root,
|
||||
*,
|
||||
is_leader: bool=False,
|
||||
no_icon: bool=False
|
||||
def __init__(
|
||||
self, family, parent, root, *, is_leader: bool = False, no_icon: bool = False
|
||||
) -> None:
|
||||
if parent is None:
|
||||
tree = Tree
|
||||
else:
|
||||
tree = parent.add
|
||||
if is_leader:
|
||||
self.tree = tree(f":notebook: {family} :",
|
||||
self.tree = tree(
|
||||
f":notebook: {family} :",
|
||||
guide_style="bold bright_blue",
|
||||
)
|
||||
elif no_icon:
|
||||
self.tree = tree(f"{family}",
|
||||
self.tree = tree(
|
||||
f"{family}",
|
||||
guide_style="bold bright_blue",
|
||||
)
|
||||
else:
|
||||
self.tree = tree(f":open_file_folder: {family}",
|
||||
self.tree = tree(
|
||||
f":open_file_folder: {family}",
|
||||
guide_style="bold bright_blue",
|
||||
)
|
||||
self.root = root
|
||||
|
||||
#
|
||||
# def parse_option(self,
|
||||
# option,
|
||||
|
@ -187,17 +191,20 @@ class OutputFamily:
|
|||
# # it's a variable
|
||||
# self.parse_variable(option, value)
|
||||
#
|
||||
def add_family(self,
|
||||
def add_family(
|
||||
self,
|
||||
option,
|
||||
) -> None:
|
||||
properties = option.property.get()
|
||||
if 'hidden' in properties:
|
||||
if "hidden" in properties:
|
||||
color = self.root.variable_hidden_color
|
||||
elif 'advanced' in properties:
|
||||
elif "advanced" in properties:
|
||||
color = self.root.variable_advanced_color
|
||||
else:
|
||||
color = None
|
||||
return OutputFamily(self.colorize(None,
|
||||
return OutputFamily(
|
||||
self.colorize(
|
||||
None,
|
||||
option.name(),
|
||||
color,
|
||||
None,
|
||||
|
@ -206,24 +213,22 @@ class OutputFamily:
|
|||
self.root,
|
||||
)
|
||||
|
||||
def add_variable(self,
|
||||
option,
|
||||
value: Any=undefined,
|
||||
leader_index: Optional[int]=None
|
||||
def add_variable(
|
||||
self, option, value: Any = undefined, leader_index: Optional[int] = None
|
||||
):
|
||||
properties = option.property.get()
|
||||
variable_color = None
|
||||
if option.owner.isdefault():
|
||||
if 'hidden' in properties:
|
||||
if "hidden" in properties:
|
||||
variable_color = self.root.variable_hidden_color
|
||||
elif 'advanced' in properties:
|
||||
elif "advanced" in properties:
|
||||
variable_color = self.root.variable_advanced_color
|
||||
color = self.root.value_unmodified_color
|
||||
default_value = None
|
||||
else:
|
||||
if 'hidden' in properties:
|
||||
if "hidden" in properties:
|
||||
variable_color = self.root.variable_hidden_color
|
||||
elif 'advanced' in properties:
|
||||
elif "advanced" in properties:
|
||||
variable_color = self.root.variable_advanced_and_modified_color
|
||||
color = None
|
||||
default_value = option.value.default()
|
||||
|
@ -231,18 +236,21 @@ class OutputFamily:
|
|||
default_value = default_value[leader_index]
|
||||
if value is undefined:
|
||||
value = option.value.get()
|
||||
key = self.colorize(None,
|
||||
key = self.colorize(
|
||||
None,
|
||||
option.name(),
|
||||
variable_color,
|
||||
None,
|
||||
)
|
||||
value = self.colorize(option,
|
||||
value = self.colorize(
|
||||
option,
|
||||
value,
|
||||
color,
|
||||
default_value,
|
||||
)
|
||||
if isinstance(value, list):
|
||||
subtree = self.tree.add(f":notebook: {key} :",
|
||||
subtree = self.tree.add(
|
||||
f":notebook: {key} :",
|
||||
guide_style="bold bright_blue",
|
||||
)
|
||||
for val in value:
|
||||
|
@ -250,7 +258,8 @@ class OutputFamily:
|
|||
else:
|
||||
self.tree.add(f":notebook: {key}: {value}")
|
||||
|
||||
def colorize(self,
|
||||
def colorize(
|
||||
self,
|
||||
option,
|
||||
value,
|
||||
color: str,
|
||||
|
@ -267,38 +276,43 @@ class OutputFamily:
|
|||
if idx < len_value:
|
||||
val = value[idx]
|
||||
else:
|
||||
val = ''
|
||||
val = ""
|
||||
if idx < len_default_value:
|
||||
if val:
|
||||
val += ' '
|
||||
val += " "
|
||||
default = default_value[idx]
|
||||
else:
|
||||
default = None
|
||||
ret.append(self.colorize(option,
|
||||
ret.append(
|
||||
self.colorize(
|
||||
option,
|
||||
val,
|
||||
color,
|
||||
default,
|
||||
))
|
||||
)
|
||||
)
|
||||
return ret
|
||||
if option and value is not None:
|
||||
value = self.convert_value(option,
|
||||
value = self.convert_value(
|
||||
option,
|
||||
value,
|
||||
)
|
||||
else:
|
||||
value = str(value)
|
||||
if color is not None:
|
||||
ret = f'[{color}]{value}[/{color}]'
|
||||
ret = f"[{color}]{value}[/{color}]"
|
||||
else:
|
||||
ret = value
|
||||
if default_value and 'force_store_value' not in option.property.get():
|
||||
if default_value and "force_store_value" not in option.property.get():
|
||||
default_value_color = self.root.value_default_color
|
||||
ret += f' ([{default_value_color}]{default_value}[/{default_value_color}])'
|
||||
ret += f" ([{default_value_color}]{default_value}[/{default_value_color}])"
|
||||
return ret
|
||||
|
||||
def convert_value(self,
|
||||
def convert_value(
|
||||
self,
|
||||
option,
|
||||
value,
|
||||
):
|
||||
if not self.root.show_secrets and option.type() == 'password':
|
||||
if not self.root.show_secrets and option.type() == "password":
|
||||
return "*" * 10
|
||||
return str(value)
|
||||
|
|
Loading…
Reference in a new issue