Skip to content

Commit

Permalink
Separate command line metadata from the cli module
Browse files Browse the repository at this point in the history
This fixed issues with building the man pages and pulling the
libseccomp import.
  • Loading branch information
igo95862 committed Feb 21, 2023
1 parent cc8e418 commit bfe2730
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 134 deletions.
6 changes: 2 additions & 4 deletions docs/man_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
138 changes: 9 additions & 129 deletions src/bubblejail/bubblejail_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
# along with bubblejail. If not, see <https://www.gnu.org/licenses/>.
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]:
Expand Down Expand Up @@ -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,
}


Expand All @@ -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(
Expand Down
145 changes: 145 additions & 0 deletions src/bubblejail/bubblejail_cli_metadata.py
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
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.',
},
}
1 change: 1 addition & 0 deletions src/bubblejail/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion test/test_auto_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit bfe2730

Please sign in to comment.