rougail-cli/src/rougail/cli/__main__.py

109 lines
3.5 KiB
Python
Raw Normal View History

2024-10-31 10:01:39 +01:00
"""
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.
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.
You should have received a copy of the GNU General Public License
along with Mtools. If not, see <http://www.gnu.org/licenses/>.
"""
2024-08-02 10:41:11 +02:00
from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import Config
from pathlib import Path
from rougail import Rougail, PropertiesOptionError
2024-08-02 10:41:11 +02:00
from rougail.config import get_rougail_config
2024-10-31 10:01:39 +01:00
from rougail.update import RougailUpgrade
2024-08-02 10:41:11 +02:00
from rougail.utils import load_modules
2024-10-31 10:01:39 +01:00
from .i18n import _
2024-08-02 10:41:11 +02:00
2024-10-31 10:01:39 +01:00
def _main():
2024-11-01 10:34:46 +01:00
rougailconfig = get_rougail_config(
backward_compatibility=False, add_extra_options=False
)
2024-08-02 10:41:11 +02:00
cmd_config = rougailconfig.config
cmd_config.property.read_write()
2024-11-01 10:34:46 +01:00
cmd_config.property.add("not_for_commandline")
parser = TiramisuCmdlineParser(
cmd_config,
add_extra_options=False,
short_name_max_len=2,
)
2024-08-02 10:41:11 +02:00
parser.parse_args()
2024-11-01 10:34:46 +01:00
cmd_config.property.remove("not_for_commandline")
2024-08-02 10:41:11 +02:00
cmd_config.property.read_only()
2024-11-01 10:34:46 +01:00
if rougailconfig["upgrade"]:
2024-10-31 10:01:39 +01:00
RougailUpgrade(rougailconfig=rougailconfig).run()
return
try:
user_data_names = rougailconfig["step.user_data"]
except PropertiesOptionError:
user_data_names = []
2024-11-01 10:34:46 +01:00
output_name = rougailconfig["step.output"]
2024-08-02 10:41:11 +02:00
# structural
rougail = Rougail(rougailconfig)
for user_data_name in user_data_names:
2024-11-01 10:34:46 +01:00
rougail.converted.plugins.append("user_data_" + user_data_name)
rougail.converted.plugins.append("output_" + output_name)
2024-08-02 10:41:11 +02:00
config = rougail.get_config()
# data user
if not user_data_names:
user_datas = None
else:
config.property.read_write()
2024-10-31 10:01:39 +01:00
user_datas = []
2024-08-02 10:41:11 +02:00
for user_data_name in user_data_names:
2024-11-01 10:34:46 +01:00
path = (
Path(__file__).parent.parent
/ ("user_data_" + user_data_name)
/ "__init__.py"
)
2024-08-02 10:41:11 +02:00
if not path.is_file():
2024-11-01 10:34:46 +01:00
raise Exception(
_('cannot find "user_data" module "{0}"').format(user_data_name)
)
module = load_modules("rougail.user_data_" + user_data_name, str(path))
user_datas.extend(
module.RougailUserData(
config,
rougailconfig=rougailconfig,
).run()
)
2024-10-31 10:01:39 +01:00
if user_datas:
err_warn = rougail.user_datas(user_datas)
else:
2024-11-01 10:34:46 +01:00
err_warn = {"errors": [], "warnings": []}
2024-08-02 10:41:11 +02:00
# output
config.property.read_only()
2024-11-01 10:34:46 +01:00
path = Path(__file__).parent.parent / ("output_" + output_name) / "__init__.py"
2024-08-02 10:41:11 +02:00
if not path.is_file():
2024-11-01 10:34:46 +01:00
raise Exception(
_('cannot find cli file for "output_name" module "{0}"').format(output_name)
)
module = load_modules("rougail.output_" + output_name, str(path))
module.RougailOutput(
config=config,
rougailconfig=rougailconfig,
user_data_errors=err_warn["errors"],
user_data_warnings=err_warn["warnings"],
).run()
2024-10-31 10:01:39 +01:00
def main():
try:
_main()
except Exception as err:
2024-11-01 10:34:46 +01:00
print(_("ERROR: {0}").format(err))
2024-10-31 10:01:39 +01:00
exit(1)