Compare commits

..

3 Commits

Author SHA1 Message Date
867cd1c022 Edit: gitignore 2024-02-11 12:00:46 +05:00
2bc9b2e94f Add: db models vm6,dci, and Docker file 2024-02-11 12:00:46 +05:00
c776632195 Add: core cli, lazy_group, example apps 2024-02-11 03:01:18 +09:00
14 changed files with 143 additions and 15 deletions

1
.gitignore vendored
View File

@ -159,6 +159,7 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
.vscode/
# Project specific
config.json

View 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')

View 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

View 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')

View 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

View 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)

View File

View 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

View File

@ -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()

View File

@ -1,4 +1,4 @@
from settings.environment import BASE_DIR
from settings.general import BASE_DIR
from settings.platform import (
PLATFORM_TYPE,

View File

@ -2,18 +2,21 @@ from settings.environment import env
from settings.platform import PLATFORM_CONFIG
#! Required because some instance use psql db:
# ! Required because some instance use psql db:
DB_ENGINE = env.str(
'DB_ENGINE',
PLATFORM_CONFIG.get('DatabaseType', 'mysql')
)
# 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')
#! Do not pass password on production. Use value from config.json
# ! Do not pass password on production. Use value from config.json
DB_PASSWORD = env.str(
'DB_PASSWORD',
PLATFORM_CONFIG.get('MysqlRootPassword', '')

View File

@ -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()

View 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',
},
}

View File

@ -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