Gitlab Community Edition Instance

Commit 70b0863b authored by Marcel Hellkamp's avatar Marcel Hellkamp
Browse files

Autodiscover command modules in pycdsttar3.cli.commands.*

parent c8d06406
......@@ -10,7 +10,6 @@ import sys
from pycdstar3 import __version__ as VERSION
from pycdstar3.client import ApiError
from pycdstar3.cli._utils import Printer
from pycdstar3.cli.commands import BUILDIN_COMMANDS
__ALL__ = ["main", "printer"]
......@@ -33,10 +32,26 @@ subparsers = parser.add_subparsers(title="available commands",
printer = Printer(level=0, file=sys.stderr)
def _autodiscover_commands():
""" Autodiscover and import all modules in the pycdstar3.cli.commands namespace.
This also works for namespace packages. Another approach would be to auto-discover
all top-level modules named `pycdstar3_*`.
"""
import pkgutil
import pycdstar3.cli.commands
for mod in pkgutil.iter_modules(pycdstar3.cli.commands.__path__):
if mod.ispkg:
continue
name = mod.name
ipath = pycdstar3.cli.commands.__name__ + "." + mod.name
yield name, importlib.import_module(ipath)
def main(*args): # noqa: C901
# Load and register all built-in commands
for name in BUILDIN_COMMANDS:
importlib.import_module(".commands." + name, __name__).register(subparsers)
for name, command in _autodiscover_commands():
command.register(subparsers)
# Parse command line arguments (may fail)
opts = parser.parse_args(args)
......
#: Commands to load automatically. The module pycdstar3.cli.NAME must have register(subparsers) defined.
BUILDIN_COMMANDS = ["init", "put", "get", "acl"]
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment