1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-21 20:33:00 +00:00

Runner for subcommands

This commit is contained in:
Jörn-Michael Miehe 2020-08-06 14:34:25 +02:00
parent 1929a1369e
commit 69dc1fd213
9 changed files with 122 additions and 92 deletions

View file

@ -1,18 +1,10 @@
#!/usr/bin/env python3
import kiwi
from kiwi.cmd_init import InitSubCommand
def main():
isc = InitSubCommand()
isc.setup()
args = kiwi.Parser.get_args()
print(args.command)
isc.run()
pass
kiwi.Runner.setup_all()
kiwi.Runner.run()
if __name__ == "__main__":

View file

@ -1,4 +1,3 @@
from .core import Parser
from .config import Config
from .runner import Runner
__all__ = ['Parser', 'Config']
__all__ = ['Runner']

View file

@ -1,40 +0,0 @@
import logging
import os
from .core import KIWI_CONF_NAME, Parser, SubCommand
from .config import Config
class InitSubCommand(SubCommand):
def __init__(self):
super(InitSubCommand, self).__init__('init')
def setup(self):
init_parser = Parser.get_subparsers().add_parser(str(self), help="Create new kiwi-config instance")
# init_parser.add_argument('cmd', metavar='command', type=str, help='subcommand to execute')
def __user_input(self, config, key, prompt):
# prompt user as per argument
result = input("{} [Current: {}] ".format(prompt, config[key])).strip()
# store result if present
if result:
config[key] = result
def run(self):
config = Config.default()
if os.path.isfile(KIWI_CONF_NAME):
logging.warning("Overwriting existing '%s'!", KIWI_CONF_NAME)
self.__user_input(config, 'version', "Choose kiwi-config version")
self.__user_input(config, 'markers:project', "Enter marker string for project directories")
self.__user_input(config, 'markers:down', "Enter marker string for disabled projects")
self.__user_input(config, 'network:name', "Enter name for local docker network")
self.__user_input(config, 'network:cidr', "Enter CIDR block for local docker network")
self.__user_input(config, 'runtime:storage', "Enter main directory for local data")
print(str(config))

View file

@ -1,5 +1,6 @@
import logging
import re
import os
import yaml
from .core import KIWI_ROOT, KIWI_CONF_NAME
@ -13,6 +14,26 @@ DEFAULT_KIWI_CONF_NAME = KIWI_ROOT + "/default.kiwi.yml"
class Config:
__yml_content = {}
def __key_resolve(self, key):
# "a:b:c" => path = ['a', 'b'], key = 'c'
path = key.split(':')
path, key = path[:-1], path[-1]
# resolve path
container = self.__yml_content
for step in path:
container = container[step]
return container, key
def __getitem__(self, key):
container, key = self.__key_resolve(key)
return container[key]
def __setitem__(self, key, value):
container, key = self.__key_resolve(key)
container[key] = value
def __str__(self):
# dump yml content
yml_string = yaml.dump(self.__yml_content, default_flow_style=False, sort_keys=False)
@ -28,26 +49,6 @@ class Config:
return yml_string
def __key_resolve(self, key):
# "a:b:c" => path = ['a', 'b'], key = 'c'
path = key.split(':')
path, key = path[:-1], path[-1]
# resolve path
container = self.__yml_content
for step in path:
container = container[step]
return container, key
def __setitem__(self, key, value):
container, key = self.__key_resolve(key)
container[key] = value
def __getitem__(self, key):
container, key = self.__key_resolve(key)
return container[key]
def __update_from_file(self, filename):
with open(filename, 'r') as stream:
try:
@ -55,6 +56,10 @@ class Config:
except yaml.YAMLError as exc:
logging.error(exc)
def __save_to_file(self, filename):
with open(filename, 'w') as stream:
stream.write(str(self))
@classmethod
def default(cls):
result = cls()
@ -68,9 +73,11 @@ class Config:
@classmethod
def load(cls):
result = cls.default()
result.__update_from_file(KIWI_CONF_NAME)
if os.path.isfile(KIWI_CONF_NAME):
result.__update_from_file(KIWI_CONF_NAME)
return result
def save(self):
pass
self.__save_to_file(KIWI_CONF_NAME)

View file

@ -38,19 +38,3 @@ class Parser:
cls.__args = cls.get_instance().parse_args()
return cls.__args
class SubCommand:
__cmd = None
def __init__(self, cmd):
self.__cmd = cmd
def __str__(self):
return self.__cmd
def setup(self):
pass
def run(self):
pass

24
src/kiwi/runner.py Normal file
View file

@ -0,0 +1,24 @@
from typing import List
from .core import Parser
from .subcommands import *
class Runner:
__commands: List[SubCommand] = [
InitCommand
]
@classmethod
def setup_all(cls):
for cmd in cls.__commands:
cmd.setup()
@classmethod
def run(cls):
args = Parser.get_args()
for cmd in cls.__commands:
if cmd.get_cmd() == args.command:
cmd.run()
return

View file

@ -0,0 +1,4 @@
from .subcommand import SubCommand
from .init import InitCommand
__all__ = ['InitCommand', 'SubCommand']

View file

@ -0,0 +1,48 @@
import logging
import os
from kiwi.core import KIWI_CONF_NAME, Parser
from kiwi.config import Config
from .subcommand import SubCommand
def user_input(config, key, prompt):
# prompt user as per argument
result = input("{} [Current: {}] ".format(prompt, config[key])).strip()
# store result if present
if result:
config[key] = result
class InitCommand(SubCommand):
__parser = None
@classmethod
def get_cmd(cls):
return 'init'
@classmethod
def setup(cls):
cls.__parser = Parser.get_subparsers().add_parser(cls.get_cmd(), help="Create new kiwi-config instance")
# cls.__parser.add_argument('cmd', metavar='command', type=str, help='subcommand to execute')
@classmethod
def run(cls):
config = Config.default()
if os.path.isfile(KIWI_CONF_NAME):
logging.warning("Overwriting existing '%s'!", KIWI_CONF_NAME)
user_input(config, 'version', "Choose kiwi-config version")
user_input(config, 'markers:project', "Enter marker string for project directories")
user_input(config, 'markers:down', "Enter marker string for disabled projects")
user_input(config, 'network:name', "Enter name for local docker network")
user_input(config, 'network:cidr', "Enter CIDR block for local docker network")
user_input(config, 'runtime:storage', "Enter main directory for local data")
config.save()

View file

@ -0,0 +1,12 @@
class SubCommand:
@classmethod
def get_cmd(cls):
pass
@classmethod
def setup(cls):
pass
@classmethod
def run(cls):
pass