diff --git a/docs/man_generator.py b/docs/man_generator.py index 310d1c0..4cb2dca 100644 --- a/docs/man_generator.py +++ b/docs/man_generator.py @@ -23,10 +23,8 @@ from jinja2 import Environment, FileSystemLoader, StrictUndefined -from bubblejail.bubblejail_cli import BUBBLEJAIL_CMD -from bubblejail.services import ( - SERVICES_CLASSES, -) +from bubblejail.bubblejail_cli_metadata import BUBBLEJAIL_CMD +from bubblejail.services import SERVICES_CLASSES if TYPE_CHECKING: from collections.abc import Iterator diff --git a/src/bubblejail/bubblejail_cli.py b/src/bubblejail/bubblejail_cli.py index 73847a7..21d84ef 100644 --- a/src/bubblejail/bubblejail_cli.py +++ b/src/bubblejail/bubblejail_cli.py @@ -15,20 +15,20 @@ # along with bubblejail. If not, see . from __future__ import annotations -from argparse import REMAINDER as ARG_REMAINDER from argparse import ArgumentParser from asyncio import run as async_run from pathlib import Path from sys import argv from typing import TYPE_CHECKING +from .bubblejail_cli_metadata import BUBBLEJAIL_CMD from .bubblejail_directories import BubblejailDirectories from .bubblejail_utils import BubblejailSettings from .services import SERVICES_CLASSES if TYPE_CHECKING: from collections.abc import Callable, Generator, Iterable, Iterator - from typing import Any, Optional, TypedDict + from typing import Optional def iter_profile_names() -> Generator[str, None, None]: @@ -186,132 +186,12 @@ def bjail_create_desktop_entry(instance_name: str, ) -if TYPE_CHECKING: - class CmdMetaDataDict(TypedDict): - add_argument: dict[str, dict[str, Any]] - argument: str - func: Callable[..., None] - description: 'str' - -BUBBLEJAIL_CMD: dict[str, CmdMetaDataDict] = { - 'run': { - 'add_argument': { - '--debug-shell': { - 'action': 'store_true', - 'help': ( - 'Opens a shell inside the sandbox instead of ' - 'running program. Useful for debugging.' - ), - }, - '--dry-run': { - 'action': 'store_true', - 'help': ( - 'Prints the bwrap and xdg-desktop-entry arguments ' - 'instead of running.' - ), - }, - '--debug-helper-script': { - 'type': Path, - 'help': ( - 'Use the specified helper script. ' - 'This is mainly development command.' - ), - 'metavar': 'script_path', - }, - '--debug-log-dbus': { - 'action': 'store_true', - 'help': 'Enables D-Bus proxy logging.', - }, - '--wait': { - 'action': 'store_true', - 'help': ( - 'Wait on the command inserted in to sandbox ' - 'and get the output.' - ), - }, - '--debug-bwrap-args': { - 'action': 'append', - 'nargs': '+', - 'help': ( - 'Add extra option to bwrap. ' - 'First argument will be prefixed with `--`.' - ), - 'metavar': ('bwrap_option', 'bwrap_option_args'), - }, - 'instance_name': { - 'help': 'Instance to run.', - }, - 'args_to_instance': { - 'nargs': ARG_REMAINDER, - 'help': 'Command and its arguments to run inside instance.', - }, - }, - 'argument': 'instance', - 'func': run_bjail, - 'description': 'Launch instance or run command inside.', - }, - 'create': { - 'add_argument': { - '--profile': { - 'help': 'Bubblejail profile to use.', - 'metavar': 'profile', - }, - '--no-desktop-entry': { - 'action': 'store_false', - 'help': 'Do not create desktop entry.', - }, - 'new_instance_name': { - 'help': 'New instance name.', - }, - }, - 'argument': 'any', - 'func': bjail_create, - 'description': 'Create new bubblejail instance.', - }, - 'list': { - 'add_argument': { - 'list_what': { - 'choices': { - 'instances', - 'profiles', - 'services', - }, - 'default': 'instances', - 'help': 'Type of entity to list.', - }, - }, - 'argument': 'any', - 'func': bjail_list, - 'description': 'List certain bubblejail entities.', - }, - 'edit': { - 'add_argument': { - 'instance_name': { - 'help': 'Instance to edit config.', - }, - }, - 'argument': 'instance', - 'func': bjail_edit, - 'description': 'Open instance config in $EDITOR.', - }, - 'generate-desktop-entry': { - 'add_argument': { - '--profile': { - 'help': 'Use desktop entry specified in profile.', - 'metavar': 'profile', - }, - '--desktop-entry': { - 'help': 'Desktop entry name or path to use.', - 'metavar': 'name_or_path', - }, - 'instance_name': { - 'help': 'Instance to generate desktop entry for', - }, - }, - 'argument': 'instance', - 'func': bjail_create_desktop_entry, - 'description': 'Generate XDG desktop entry for an instance.', - }, +COMMANDS_FUNCS: dict[str, Callable[..., None]] = { + 'run': run_bjail, + 'create': bjail_create, + 'list': bjail_list, + 'edit': bjail_edit, + 'generate-desktop-entry': bjail_create_desktop_entry, } @@ -326,7 +206,7 @@ def create_arg_parser() -> ArgumentParser: description='Available subcommands.' ) for subcommand_name, subcommand_data in BUBBLEJAIL_CMD.items(): - subfunction = subcommand_data['func'] + subfunction = COMMANDS_FUNCS[subcommand_name] description = subcommand_data['description'] subcommand_add_argument = subcommand_data['add_argument'] subparser = subparsers.add_parser( diff --git a/src/bubblejail/bubblejail_cli_metadata.py b/src/bubblejail/bubblejail_cli_metadata.py new file mode 100644 index 0000000..eaf101c --- /dev/null +++ b/src/bubblejail/bubblejail_cli_metadata.py @@ -0,0 +1,145 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +# Copyright 2023 igo95862 + +# This file is part of bubblejail. +# bubblejail 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. +# bubblejail 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 bubblejail. If not, see . +from __future__ import annotations + +from pathlib import Path +from argparse import REMAINDER as ARG_REMAINDER + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from typing import Any, TypedDict + + class CmdMetaDataDict(TypedDict): + add_argument: dict[str, dict[str, Any]] + argument: str + description: 'str' + +BUBBLEJAIL_CMD: dict[str, CmdMetaDataDict] = { + 'run': { + 'add_argument': { + '--debug-shell': { + 'action': 'store_true', + 'help': ( + 'Opens a shell inside the sandbox instead of ' + 'running program. Useful for debugging.' + ), + }, + '--dry-run': { + 'action': 'store_true', + 'help': ( + 'Prints the bwrap and xdg-desktop-entry arguments ' + 'instead of running.' + ), + }, + '--debug-helper-script': { + 'type': Path, + 'help': ( + 'Use the specified helper script. ' + 'This is mainly development command.' + ), + 'metavar': 'script_path', + }, + '--debug-log-dbus': { + 'action': 'store_true', + 'help': 'Enables D-Bus proxy logging.', + }, + '--wait': { + 'action': 'store_true', + 'help': ( + 'Wait on the command inserted in to sandbox ' + 'and get the output.' + ), + }, + '--debug-bwrap-args': { + 'action': 'append', + 'nargs': '+', + 'help': ( + 'Add extra option to bwrap. ' + 'First argument will be prefixed with `--`.' + ), + 'metavar': ('bwrap_option', 'bwrap_option_args'), + }, + 'instance_name': { + 'help': 'Instance to run.', + }, + 'args_to_instance': { + 'nargs': ARG_REMAINDER, + 'help': 'Command and its arguments to run inside instance.', + }, + }, + 'argument': 'instance', + 'description': 'Launch instance or run command inside.', + }, + 'create': { + 'add_argument': { + '--profile': { + 'help': 'Bubblejail profile to use.', + 'metavar': 'profile', + }, + '--no-desktop-entry': { + 'action': 'store_false', + 'help': 'Do not create desktop entry.', + }, + 'new_instance_name': { + 'help': 'New instance name.', + }, + }, + 'argument': 'any', + 'description': 'Create new bubblejail instance.', + }, + 'list': { + 'add_argument': { + 'list_what': { + 'choices': { + 'instances', + 'profiles', + 'services', + }, + 'default': 'instances', + 'help': 'Type of entity to list.', + }, + }, + 'argument': 'any', + 'description': 'List certain bubblejail entities.', + }, + 'edit': { + 'add_argument': { + 'instance_name': { + 'help': 'Instance to edit config.', + }, + }, + 'argument': 'instance', + 'description': 'Open instance config in $EDITOR.', + }, + 'generate-desktop-entry': { + 'add_argument': { + '--profile': { + 'help': 'Use desktop entry specified in profile.', + 'metavar': 'profile', + }, + '--desktop-entry': { + 'help': 'Desktop entry name or path to use.', + 'metavar': 'name_or_path', + }, + 'instance_name': { + 'help': 'Instance to generate desktop entry for', + }, + }, + 'argument': 'instance', + 'description': 'Generate XDG desktop entry for an instance.', + }, +} diff --git a/src/bubblejail/meson.build b/src/bubblejail/meson.build index 02b25c9..8afca8e 100644 --- a/src/bubblejail/meson.build +++ b/src/bubblejail/meson.build @@ -2,6 +2,7 @@ source_files = [ '__init__.py', 'bubblejail_cli.py', + 'bubblejail_cli_metadata.py', 'bubblejail_cli_autocomplete.py', 'bubblejail_directories.py', 'bubblejail_gui_qt.py', diff --git a/test/test_auto_completion.py b/test/test_auto_completion.py index ffe1ddc..5ed633f 100644 --- a/test/test_auto_completion.py +++ b/test/test_auto_completion.py @@ -17,8 +17,9 @@ from unittest import TestCase, main -from bubblejail.bubblejail_cli import BUBBLEJAIL_CMD, iter_list_choices +from bubblejail.bubblejail_cli import iter_list_choices from bubblejail.bubblejail_cli_autocomplete import AutoCompleteParser +from bubblejail.bubblejail_cli_metadata import BUBBLEJAIL_CMD class TestAutocomplete(TestCase):