add commands and subcommands

This commit is contained in:
Thomas Peetz
2025-01-14 22:58:15 +01:00
parent 4cea554116
commit 3466c10a88
8 changed files with 86 additions and 4 deletions
+5 -2
View File
@@ -18,7 +18,7 @@ class CliBase(Controller):
description = 'Kontor CLI Tool'
# text displayed at the bottom of --help output
epilog = 'Usage: kontor gui|database'
epilog = 'Usage: kontor (gui | database | media) [subcommands]'
# controller level arguments. ex: 'kontor --version'
arguments = [
@@ -26,11 +26,14 @@ class CliBase(Controller):
(['-v', '--version'],
{'action': 'version',
'version': VERSION_BANNER}),
(['-m', '--dry-run'],
{'action': 'store_true',
'dest': 'dry_run'})
]
def _default(self):
"""Default action if no sub-command is passed."""
self.gui()
self.app.args.print_help()
@ex(
help='start GUI'
+23
View File
@@ -30,3 +30,26 @@ class Database(Controller):
table_list = kontor_db.get_table_names()
kontor_db.export_db(data['export_type'], data['db_file'], table_list)
self.app.render(data, 'command1.jinja2')
@ex(
label='import',
help='import data from file into database',
arguments=[
(['-f', '--file'],
{'help': 'file to read data',
'action': 'store',
'dest': 'db_file'})
],
)
def import_cmd(self):
data = {
'db_file': 'data.json',
'data_type': 'JSON',
}
if self.app.pargs.db_file is not None:
data['db_file'] = self.app.pargs.db_file
kontor_db = KontorDB(self.app.session, self.app.log)
if self.app.pargs.dry_run:
self.app.render(data, 'import.jinja2')
else:
kontor_db.import_db(data['db_file'])
+40
View File
@@ -0,0 +1,40 @@
from cement import Controller, ex
class Media(Controller):
class Meta:
label = 'media'
stacked_type = 'nested'
stacked_on = 'base'
@ex(
label='update',
help='update title for mediafiles',
)
def update_title(self):
if self.app.pargs.dry_run:
print('print command to shell')
self.app.render({}, 'update.jinja2')
@ex(
label='download',
help='download all marked videos',
arguments=[
(['-d', '--dir'],
{'help': 'directory to store videos',
'action': 'store',
'dest': 'media_dir'})
],
)
def download(self):
data = {
'media_dir': '/data/media',
}
if self.app.pargs.media_dir is not None:
data['media_dir'] = self.app.pargs.media_dir
if self.app.pargs.dry_run:
print('print command to shell')
self.app.render(data, 'download.jinja2')
+3
View File
@@ -153,3 +153,6 @@ class KontorDB:
self.log.debug("unknown export type")
if export_file.exists():
self.log.debug(f"{export_file} exists")
def import_db(self, import_file_name: str):
pass
+9 -2
View File
@@ -3,20 +3,23 @@ from cement.core.exc import CaughtSignal
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from kontor.controllers.media import Media
from .core.exc import KontorError
from .database.base import Base
from .controllers.clibase import CliBase
from .controllers.database import Database
# configuration defaults
CONFIG = init_defaults('kontor', 'mariadb')
CONFIG = init_defaults('kontor', 'mariadb','output.json', 'output.yaml')
CONFIG['kontor']['foo'] = 'bar'
CONFIG['mariadb']['user'] = 'kontor'
CONFIG['mariadb']['password'] = 'kontor'
CONFIG['mariadb']['host'] = '127.0.0.1'
CONFIG['mariadb']['port'] = '3306'
CONFIG['mariadb']['database'] = 'kontor'
META = init_defaults('output.json', 'output.yaml')
META['output.json']['overridable'] = True
META['output.yaml']['overridable'] = True
def extend_sqlalchemy(app):
app.log.info('extending kontor application with sqlalchemy')
@@ -48,12 +51,15 @@ class Kontor(App):
# configuration defaults
config_defaults = CONFIG
meta_defaults = META
# call sys.exit() on close
exit_on_close = True
# load additional framework extensions
extensions = [
'yaml',
'json',
'colorlog',
'jinja2',
]
@@ -78,6 +84,7 @@ class Kontor(App):
handlers = [
CliBase,
Database,
Media,
]
+2
View File
@@ -0,0 +1,2 @@
Download videos to directory {{ media_dir }}
+2
View File
@@ -0,0 +1,2 @@
Import data from {{ db_file }}
+2
View File
@@ -0,0 +1,2 @@
Update entries of mediafile