From e44fd8a7a5850ad159ce451b5b6cab930a615188 Mon Sep 17 00:00:00 2001 From: Firq Date: Fri, 5 Jul 2024 11:15:07 +0200 Subject: [PATCH] Refactor, typing improvements, ci improvements --- .forgejo/workflows/build-release.yaml | 11 +- .forgejo/workflows/{lint.yaml => check.yaml} | 21 ++- dockge_cli/client/commandprovider/bindings.py | 4 +- .../client/commandprovider/descriptors.py | 172 +++++++++--------- dockge_cli/client/commandprovider/factory.py | 18 +- dockge_cli/client/parser.py | 2 +- dockge_cli/models/__init__.py | 2 + dockge_cli/models/commands.py | 9 + pyproject.toml | 9 + 9 files changed, 141 insertions(+), 107 deletions(-) rename .forgejo/workflows/{lint.yaml => check.yaml} (51%) create mode 100644 dockge_cli/models/commands.py diff --git a/.forgejo/workflows/build-release.yaml b/.forgejo/workflows/build-release.yaml index 1170c10..16bebd7 100644 --- a/.forgejo/workflows/build-release.yaml +++ b/.forgejo/workflows/build-release.yaml @@ -6,7 +6,7 @@ on: - '[0-9]+\.[0-9]+\.[0-9]' jobs: - backend-pylint: + lint-and-typing: runs-on: docker container: nikolaik/python-nodejs:python3.11-nodejs21 steps: @@ -15,15 +15,20 @@ jobs: - name: Install packages run: | pip install -e . -q + pip install pylint~=2.17.7 mypy~=1.10.1 --disable-pip-version-check -q + mypy --install-types python -m pip list --format=columns --disable-pip-version-check - pip install pylint~=2.17.7 --disable-pip-version-check -q - name: Run pylint run: | pylint --version pylint **/*.py --exit-zero --rc-file pyproject.toml + - name: Run mypy + run: | + mypy --version + mypy . build-artifacts: - needs: ["backend-pylint"] + needs: ["lint-and-typing"] runs-on: docker container: nikolaik/python-nodejs:python3.11-nodejs21 steps: diff --git a/.forgejo/workflows/lint.yaml b/.forgejo/workflows/check.yaml similarity index 51% rename from .forgejo/workflows/lint.yaml rename to .forgejo/workflows/check.yaml index a88449c..346a154 100644 --- a/.forgejo/workflows/lint.yaml +++ b/.forgejo/workflows/check.yaml @@ -3,7 +3,7 @@ on: branches: "**" jobs: - backend-pylint: + pylint: runs-on: docker container: nikolaik/python-nodejs:python3.11-nodejs21 steps: @@ -12,9 +12,26 @@ jobs: - name: Install packages run: | pip install -e . -q - python -m pip list --format=columns --disable-pip-version-check pip install pylint~=2.17.7 --disable-pip-version-check -q + python -m pip list --format=columns --disable-pip-version-check - name: Run pylint run: | pylint --version pylint **/*.py --exit-zero --rc-file pyproject.toml + + typing-mypy: + runs-on: docker + container: nikolaik/python-nodejs:python3.11-nodejs21 + steps: + - name: Checkout source code + uses: https://code.forgejo.org/actions/checkout@v3 + - name: Install packages + run: | + pip install -e . -q + pip install mypy~=1.10.1 --disable-pip-version-check -q + mypy --install-types + python -m pip list --format=columns --disable-pip-version-check + - name: Run mypy + run: | + mypy --version + mypy . diff --git a/dockge_cli/client/commandprovider/bindings.py b/dockge_cli/client/commandprovider/bindings.py index 6e813de..d6c7685 100644 --- a/dockge_cli/client/commandprovider/bindings.py +++ b/dockge_cli/client/commandprovider/bindings.py @@ -1,10 +1,10 @@ from urllib.parse import urlparse from getpass import getpass -from ...models.parser import Credentials +from ...models import Credentials from ...service import storage -from ..utils import stack_formatter, status_formatter, generic_formatter, get_credential_parser from ...service.communicate import DockgeConnection +from ..utils import stack_formatter, status_formatter, generic_formatter, get_credential_parser class ExecutionCommands(): @staticmethod diff --git a/dockge_cli/client/commandprovider/descriptors.py b/dockge_cli/client/commandprovider/descriptors.py index cd346fd..10e60f3 100644 --- a/dockge_cli/client/commandprovider/descriptors.py +++ b/dockge_cli/client/commandprovider/descriptors.py @@ -1,88 +1,90 @@ +from typing import List +from ...models import Command from .bindings import ExecutionCommands -command_mappings = [ - { - "command": "host", - "description": "Sets and gets the URI of the dockge instance. Remove any unnecessary subdomains/protocols from the URI", - "args": 1, - "optional": True, - "binding": ExecutionCommands.host - }, - { - "command": "login", - "description": "Logs into a given dockge account, either with an interactive dialogue or by passing --user and --password", - "args": 2, - "optional": True, - "binding": ExecutionCommands.login - }, - { - "command": "logout", - "description": "Removes the credentials from the local storage.", - "args": 0, - "optional": False, - "binding": ExecutionCommands.logout - }, - { - "command": "list", - "description": "Lists all available stacks with their status", - "args": 0, - "optional": False, - "binding": ExecutionCommands.list - }, - { - "command": "status", - "description": "Returns the status of one stack", - "args": 1, - "optional": False, - "binding": ExecutionCommands.status - }, - { - "command": "restart", - "description": "Restarts a given stack", - "args": 1, - "optional": False, - "binding": ExecutionCommands.restart - }, - { - "command": "start", - "description": "Starts a given stack", - "args": 1, - "optional": False, - "binding": ExecutionCommands.start - }, - { - "command": "stop", - "description": "Stops a given stack", - "args": 1, - "optional": False, - "binding": ExecutionCommands.stop - }, - { - "command": "down", - "description": "Stop & Downs a given stack", - "args": 1, - "optional": False, - "binding": ExecutionCommands.down - }, - { - "command": "update", - "description": "Updates a stack", - "args": 1, - "optional": False, - "binding": ExecutionCommands.update - }, - { - "command": "exit", - "description": "Exits the CLI - this will reset all settings, including credentials and host", - "args": 0, - "optional": False, - "binding": ExecutionCommands.exit - }, - { - "command": "help", - "description": "Displays helping hints for commands", - "args": 1, - "optional": True, - "binding": ExecutionCommands.help - } +mapping: List[Command] = [ + Command( + cmd="host", + description="Sets and gets the URI of the dockge instance. Remove any unnecessary subdomains/protocols from the URI", + args=1, + optional=True, + bind=ExecutionCommands.host + ), + Command( + cmd="login", + description="Logs into a given dockge account, either with an interactive dialogue or by passing --user and --password", + args=2, + optional=True, + bind=ExecutionCommands.login + ), + Command( + cmd="logout", + description="Removes the credentials from the local storage.", + args=0, + optional=False, + bind=ExecutionCommands.logout + ), + Command( + cmd="list", + description="Lists all available stacks with their status", + args=0, + optional=False, + bind=ExecutionCommands.list + ), + Command( + cmd="status", + description="Returns the status of one stack", + args=1, + optional=False, + bind=ExecutionCommands.status + ), + Command( + cmd="restart", + description="Restarts a given stack", + args=1, + optional=False, + bind=ExecutionCommands.restart + ), + Command( + cmd="start", + description="Starts a given stack", + args=1, + optional=False, + bind=ExecutionCommands.start + ), + Command( + cmd="stop", + description="Stops a given stack", + args=1, + optional=False, + bind=ExecutionCommands.stop + ), + Command( + cmd="down", + description="Stop & Downs a given stack", + args=1, + optional=False, + bind=ExecutionCommands.down + ), + Command( + cmd="update", + description="Updates a stack", + args=1, + optional=False, + bind=ExecutionCommands.update + ), + Command( + cmd="exit", + description="Exits the CLI - this will reset all settings, including credentials and host", + args=0, + optional=False, + bind=ExecutionCommands.exit + ), + Command( + cmd="help", + description="Displays helping hints for commands", + args=1, + optional=True, + bind=ExecutionCommands.help + ) ] diff --git a/dockge_cli/client/commandprovider/factory.py b/dockge_cli/client/commandprovider/factory.py index 4bfe95a..6bb2fbe 100644 --- a/dockge_cli/client/commandprovider/factory.py +++ b/dockge_cli/client/commandprovider/factory.py @@ -1,17 +1,7 @@ -from typing import List, Callable -from pydantic import BaseModel -from .descriptors import command_mappings - -class Command(BaseModel): - command: str - description: str - args: int - optional: bool - binding: Callable +from ...models import Command +from .descriptors import mapping commands: dict[str, Command] = {} -descriptors: List[dict[str, object]] = command_mappings -for descriptor in descriptors: - c = Command(**descriptor) # type: ignore - commands.update({ c.command: c }) +for descriptor in mapping: + commands.update({ descriptor.cmd: descriptor }) diff --git a/dockge_cli/client/parser.py b/dockge_cli/client/parser.py index 2efbc30..f76bc36 100644 --- a/dockge_cli/client/parser.py +++ b/dockge_cli/client/parser.py @@ -2,7 +2,7 @@ import argparse import sys from .. import __version__ -from ..models.parser import Arguments +from ..models import Arguments from .commandprovider.factory import commands def parse_arguments(): diff --git a/dockge_cli/models/__init__.py b/dockge_cli/models/__init__.py index ef15c9b..e412edf 100644 --- a/dockge_cli/models/__init__.py +++ b/dockge_cli/models/__init__.py @@ -1 +1,3 @@ from .codes import StackStatus +from .commands import Command +from .parser import Arguments, Credentials diff --git a/dockge_cli/models/commands.py b/dockge_cli/models/commands.py new file mode 100644 index 0000000..5bc7fb6 --- /dev/null +++ b/dockge_cli/models/commands.py @@ -0,0 +1,9 @@ +from typing import Callable +from pydantic import BaseModel + +class Command(BaseModel): + cmd: str + bind: Callable + args: int + optional: bool + description: str diff --git a/pyproject.toml b/pyproject.toml index 50b898b..9d64f03 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,15 @@ include = ["dockge_cli*"] [tool.pylint."MAIN"] disable = [ "line-too-long", "missing-module-docstring", "missing-function-docstring", "missing-class-docstring" ] +[tool.mypy] +python_version = "3.11" +warn_return_any = true +warn_unused_configs = true + +[[tool.mypy.overrides]] +module = 'socketio.*' +ignore_missing_imports = true + [build-system] requires = ["setuptools >= 61.0"] build-backend = "setuptools.build_meta"