2019-04-03 04:27:37 +00:00
|
|
|
__package__ = 'archivebox.cli'
|
|
|
|
|
|
|
|
import os
|
2019-04-19 01:09:54 +00:00
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
from typing import Dict, List, Optional, IO
|
2019-04-03 04:27:37 +00:00
|
|
|
from importlib import import_module
|
|
|
|
|
|
|
|
CLI_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
2019-04-19 01:09:54 +00:00
|
|
|
# these common commands will appear sorted before any others for ease-of-use
|
2019-04-27 21:26:24 +00:00
|
|
|
meta_cmds = ('help', 'version')
|
|
|
|
main_cmds = ('init', 'info', 'config')
|
|
|
|
archive_cmds = ('add', 'remove', 'update', 'list')
|
|
|
|
|
|
|
|
display_first = (*meta_cmds, *main_cmds, *archive_cmds)
|
2019-04-03 04:27:37 +00:00
|
|
|
|
2019-04-19 01:09:54 +00:00
|
|
|
# every imported command module must have these properties in order to be valid
|
|
|
|
required_attrs = ('__package__', '__command__', 'main')
|
2019-04-03 04:27:37 +00:00
|
|
|
|
2019-04-19 01:09:54 +00:00
|
|
|
# basic checks to make sure imported files are valid subcommands
|
|
|
|
is_cli_module = lambda fname: fname.startswith('archivebox_') and fname.endswith('.py')
|
|
|
|
is_valid_cli_module = lambda module, subcommand: (
|
|
|
|
all(hasattr(module, attr) for attr in required_attrs)
|
|
|
|
and module.__command__.split(' ')[-1] == subcommand
|
|
|
|
)
|
2019-04-11 10:59:14 +00:00
|
|
|
|
2019-04-19 01:09:54 +00:00
|
|
|
def list_subcommands() -> Dict[str, str]:
|
|
|
|
"""find and import all valid archivebox_<subcommand>.py files in CLI_DIR"""
|
2019-04-11 10:59:14 +00:00
|
|
|
|
|
|
|
COMMANDS = []
|
2019-04-03 04:27:37 +00:00
|
|
|
for filename in os.listdir(CLI_DIR):
|
2019-04-19 01:09:54 +00:00
|
|
|
if is_cli_module(filename):
|
2019-04-03 04:27:37 +00:00
|
|
|
subcommand = filename.replace('archivebox_', '').replace('.py', '')
|
|
|
|
module = import_module('.archivebox_{}'.format(subcommand), __package__)
|
2019-04-19 01:09:54 +00:00
|
|
|
assert is_valid_cli_module(module, subcommand)
|
|
|
|
COMMANDS.append((subcommand, module.__description__)) # type: ignore
|
|
|
|
globals()[subcommand] = module.main
|
|
|
|
module.main.__doc__ = module.__description__
|
|
|
|
|
|
|
|
display_order = lambda cmd: (
|
|
|
|
display_first.index(cmd[0])
|
|
|
|
if cmd[0] in display_first else
|
|
|
|
100 + len(cmd[0])
|
|
|
|
)
|
2019-04-03 04:27:37 +00:00
|
|
|
|
2019-04-19 01:09:54 +00:00
|
|
|
return dict(sorted(COMMANDS, key=display_order))
|
2019-04-03 04:27:37 +00:00
|
|
|
|
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
def run_subcommand(subcommand: str,
|
|
|
|
subcommand_args: List[str]=None,
|
|
|
|
stdin: Optional[IO]=None,
|
|
|
|
pwd: Optional[str]=None) -> None:
|
2019-04-19 01:09:54 +00:00
|
|
|
"""run a given ArchiveBox subcommand with the given list of args"""
|
2019-04-03 04:27:37 +00:00
|
|
|
|
|
|
|
module = import_module('.archivebox_{}'.format(subcommand), __package__)
|
2019-04-27 21:26:24 +00:00
|
|
|
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
|
2019-04-19 01:09:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
SUBCOMMANDS = list_subcommands()
|
|
|
|
|
|
|
|
__all__ = (
|
|
|
|
'SUBCOMMANDS',
|
|
|
|
'list_subcommands',
|
|
|
|
'run_subcommand',
|
|
|
|
*SUBCOMMANDS.keys(),
|
|
|
|
)
|