From eb7a642094909bb351da7ffbc9a335dbcf8b10d6 Mon Sep 17 00:00:00 2001 From: Baudouin Raoult Date: Tue, 28 May 2024 11:44:54 +0100 Subject: [PATCH] Work on documentation --- docs/conf.py | 2 + src/anemoi/models/commands/__init__.py | 64 ++------------------------ 2 files changed, 7 insertions(+), 59 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 215c7d05..fa31a5cd 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -113,3 +113,5 @@ todo_include_todos = not read_the_docs_build + +autodoc_member_order = "bysource" # Keep file order diff --git a/src/anemoi/models/commands/__init__.py b/src/anemoi/models/commands/__init__.py index 0932e6e4..cebb5395 100644 --- a/src/anemoi/models/commands/__init__.py +++ b/src/anemoi/models/commands/__init__.py @@ -8,69 +8,15 @@ # nor does it submit to any jurisdiction. # -import argparse -import importlib -import logging import os -import sys -LOG = logging.getLogger(__name__) +from anemoi.utils.cli import Command +from anemoi.utils.cli import Failed +from anemoi.utils.cli import register_commands +__all__ = ["Command"] -def register(here, package, select, fail=None): - result = {} - not_available = {} - - for p in os.listdir(here): - full = os.path.join(here, p) - if p.startswith("_"): - continue - if not (p.endswith(".py") or (os.path.isdir(full) and os.path.exists(os.path.join(full, "__init__.py")))): - continue - - name, _ = os.path.splitext(p) - - try: - imported = importlib.import_module( - f".{name}", - package=package, - ) - except ImportError as e: - not_available[name] = e - continue - - obj = select(imported) - if obj is not None: - result[name] = obj - - for name, e in not_available.items(): - if fail is None: - pass - if callable(fail): - result[name] = fail(name, e) - - return result - - -class Command: - def run(self, args): - raise NotImplementedError(f"Command not implemented: {args.command}") - - -class Failed(Command): - def __init__(self, name, error): - self.name = name - self.error = error - - def add_arguments(self, command_parser): - command_parser.add_argument("x", nargs=argparse.REMAINDER) - - def run(self, args): - print(f"Command '{self.name}' not available: {self.error}") - sys.exit(1) - - -COMMANDS = register( +COMMANDS = register_commands( os.path.dirname(__file__), __name__, lambda x: x.command(),