mirror of
https://git.isptech.ru/ISPsystem/isp-maintenance.git
synced 2025-02-01 02:50:51 +01:00
Add: core cli, lazy_group, example apps
This commit is contained in:
parent
c8c7060201
commit
c776632195
12
isp_maintenance/apps/dci6/access/commands.py
Normal file
12
isp_maintenance/apps/dci6/access/commands.py
Normal file
@ -0,0 +1,12 @@
|
||||
import click
|
||||
|
||||
|
||||
@click.group(help='access command for lazy example')
|
||||
@click.option('--debug/--no-debug', default=False)
|
||||
def cli(debug):
|
||||
click.echo(f"Debug mode is {'on' if debug else 'off'}")
|
||||
|
||||
|
||||
@cli.command()
|
||||
def enable():
|
||||
click.echo('Access granted')
|
13
isp_maintenance/apps/dci6/commands.py
Normal file
13
isp_maintenance/apps/dci6/commands.py
Normal file
@ -0,0 +1,13 @@
|
||||
import click
|
||||
|
||||
from core.cli.lazy_group import LazyGroup
|
||||
from settings.general import INSTALLED_APPS
|
||||
|
||||
|
||||
@click.group(
|
||||
cls=LazyGroup,
|
||||
lazy_subcommands=INSTALLED_APPS['dci6'],
|
||||
help='dci6 command for lazy example',
|
||||
)
|
||||
def cli():
|
||||
pass
|
12
isp_maintenance/apps/vm6/access/commands.py
Normal file
12
isp_maintenance/apps/vm6/access/commands.py
Normal file
@ -0,0 +1,12 @@
|
||||
import click
|
||||
|
||||
|
||||
@click.group(help='access command for lazy example')
|
||||
@click.option('--debug/--no-debug', default=False)
|
||||
def cli(debug):
|
||||
click.echo(f"Debug mode is {'on' if debug else 'off'}")
|
||||
|
||||
|
||||
@cli.command()
|
||||
def enable():
|
||||
click.echo('Access granted')
|
12
isp_maintenance/apps/vm6/commands.py
Normal file
12
isp_maintenance/apps/vm6/commands.py
Normal file
@ -0,0 +1,12 @@
|
||||
import click
|
||||
from core.cli.lazy_group import LazyGroup
|
||||
from settings.general import INSTALLED_APPS
|
||||
|
||||
|
||||
@click.group(
|
||||
cls=LazyGroup,
|
||||
lazy_subcommands=INSTALLED_APPS['vm6'],
|
||||
help='vm6 command for lazy example',
|
||||
)
|
||||
def cli():
|
||||
pass
|
13
isp_maintenance/apps/vm6/nodes/commands.py
Normal file
13
isp_maintenance/apps/vm6/nodes/commands.py
Normal file
@ -0,0 +1,13 @@
|
||||
import click
|
||||
|
||||
|
||||
@click.group(help='nodes command for lazy example')
|
||||
def cli():
|
||||
pass
|
||||
|
||||
|
||||
@cli.command(name='list')
|
||||
def nodes_list():
|
||||
click.echo('NODES LIST: etc...')
|
||||
for num in range(1, 10):
|
||||
click.echo(num)
|
0
isp_maintenance/core/cli/__init__.py
Normal file
0
isp_maintenance/core/cli/__init__.py
Normal file
39
isp_maintenance/core/cli/lazy_group.py
Normal file
39
isp_maintenance/core/cli/lazy_group.py
Normal file
@ -0,0 +1,39 @@
|
||||
import importlib
|
||||
import click
|
||||
|
||||
|
||||
class LazyGroup(click.Group):
|
||||
def __init__(self, *args, lazy_subcommands=None, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
# lazy_subcommands is a map of the form:
|
||||
#
|
||||
# {command-name} -> {module-name}.{command-object-name}
|
||||
#
|
||||
self.lazy_subcommands = lazy_subcommands or {}
|
||||
|
||||
def list_commands(self, ctx):
|
||||
base = super().list_commands(ctx)
|
||||
lazy = sorted(self.lazy_subcommands.keys())
|
||||
return base + lazy
|
||||
|
||||
def get_command(self, ctx, cmd_name):
|
||||
if cmd_name in self.lazy_subcommands:
|
||||
return self._lazy_load(cmd_name)
|
||||
return super().get_command(ctx, cmd_name)
|
||||
|
||||
def _lazy_load(self, cmd_name):
|
||||
# lazily loading a command,
|
||||
# first get the module name and attribute name
|
||||
import_path = self.lazy_subcommands[cmd_name]
|
||||
modname, cmd_object_name = import_path.rsplit(".", 1)
|
||||
# do the import
|
||||
mod = importlib.import_module(modname)
|
||||
# get the Command object from that module
|
||||
cmd_object = getattr(mod, cmd_object_name)
|
||||
# check the result to make debugging easier
|
||||
if not isinstance(cmd_object, click.BaseCommand):
|
||||
raise ValueError(
|
||||
f"Lazy loading of {import_path} failed by returning "
|
||||
"a non-command object"
|
||||
)
|
||||
return cmd_object
|
@ -1,9 +1,21 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
from settings.environment import env
|
||||
import click
|
||||
|
||||
from core.cli.lazy_group import LazyGroup
|
||||
|
||||
|
||||
# TODO: delete this demo
|
||||
# Just show you which env get application or default values
|
||||
for key, value in env.dump().items():
|
||||
print(key, '|', value)
|
||||
@click.group(
|
||||
cls=LazyGroup,
|
||||
lazy_subcommands={
|
||||
'vm6': 'apps.vm6.commands.cli',
|
||||
'dci6': 'apps.dci6.commands.cli',
|
||||
},
|
||||
help='main CLI command for lazy example',
|
||||
)
|
||||
def cli():
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
cli()
|
||||
|
@ -1,4 +1,4 @@
|
||||
from settings.environment import BASE_DIR
|
||||
from settings.general import BASE_DIR
|
||||
|
||||
from settings.platform import (
|
||||
PLATFORM_TYPE,
|
||||
|
@ -9,7 +9,10 @@ DB_ENGINE = env.str(
|
||||
)
|
||||
|
||||
# Connection parameters:
|
||||
DB_HOST = env.str('DB_HOST', 'mysql')
|
||||
DB_HOST = env.str(
|
||||
'DB_HOST',
|
||||
PLATFORM_CONFIG.get('DatabaseType', 'mysql')
|
||||
)
|
||||
DB_PORT = env.int('DB_PORT', 3306)
|
||||
DB_USER = env.str('DB_USER', 'root')
|
||||
|
||||
|
@ -1,11 +1,6 @@
|
||||
import pathlib
|
||||
from environs import Env
|
||||
|
||||
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
BASE_DIR = pathlib.Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
# Init environment:
|
||||
env = Env()
|
||||
|
||||
|
15
isp_maintenance/settings/general.py
Normal file
15
isp_maintenance/settings/general.py
Normal file
@ -0,0 +1,15 @@
|
||||
import pathlib
|
||||
|
||||
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
BASE_DIR = pathlib.Path(__file__).resolve().parent.parent
|
||||
|
||||
INSTALLED_APPS = {
|
||||
'vm6': {
|
||||
'access': 'apps.vm6.access.commands.cli',
|
||||
'nodes': 'apps.vm6.nodes.commands.cli',
|
||||
},
|
||||
'dci6': {
|
||||
'access': 'apps.dci6.access.commands.cli',
|
||||
},
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
from settings.environment import env, BASE_DIR
|
||||
from settings.environment import env
|
||||
from settings.general import BASE_DIR
|
||||
from utils.helpers import parse_json_file
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user