mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-11-22 12:53:00 +00:00
init routine in subcommand class
This commit is contained in:
parent
d108ae2183
commit
1929a1369e
9 changed files with 158 additions and 72 deletions
|
@ -1 +0,0 @@
|
||||||
VERSION="0.1"
|
|
1
kiwi.yml
Normal file
1
kiwi.yml
Normal file
|
@ -0,0 +1 @@
|
||||||
|
version: 0.1
|
|
@ -11,3 +11,4 @@ network:
|
||||||
runtime:
|
runtime:
|
||||||
storage: /var/kiwi
|
storage: /var/kiwi
|
||||||
env:
|
env:
|
||||||
|
HELLO_KIWI: Hello World!
|
||||||
|
|
|
@ -1,27 +1,16 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import argparse
|
import kiwi
|
||||||
from kiwi.config import *
|
from kiwi.cmd_init import InitSubCommand
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description='kiwi-config')
|
isc = InitSubCommand()
|
||||||
|
isc.setup()
|
||||||
|
|
||||||
subs = parser.add_subparsers()
|
args = kiwi.Parser.get_args()
|
||||||
subs.required = True
|
|
||||||
subs.dest = 'command'
|
|
||||||
|
|
||||||
subs.add_parser('init', help="Create new kiwi-config instance")
|
|
||||||
subs.add_parser('add', help="Add a project to kiwi-config")
|
|
||||||
|
|
||||||
# parser.add_argument('cmd', metavar='command', type=str, help='subcommand to execute')
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
|
||||||
print(args.command)
|
print(args.command)
|
||||||
|
|
||||||
cf = Config.default()
|
isc.run()
|
||||||
# cf.user_input()
|
|
||||||
cf.dump()
|
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
@ -1 +1,4 @@
|
||||||
__all__ = ['config']
|
from .core import Parser
|
||||||
|
from .config import Config
|
||||||
|
|
||||||
|
__all__ = ['Parser', 'Config']
|
||||||
|
|
40
src/kiwi/cmd_init.py
Normal file
40
src/kiwi/cmd_init.py
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
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))
|
|
@ -1,79 +1,76 @@
|
||||||
import os
|
import logging
|
||||||
import re
|
import re
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
from .core import KIWI_ROOT, KIWI_CONF_NAME
|
||||||
|
|
||||||
|
###########
|
||||||
|
# CONSTANTS
|
||||||
|
|
||||||
|
DEFAULT_KIWI_CONF_NAME = KIWI_ROOT + "/default.kiwi.yml"
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
KIWI_ROOT = os.getenv('KIWI_ROOT', '.')
|
__yml_content = {}
|
||||||
__yml_content = None
|
|
||||||
|
|
||||||
@classmethod
|
def __str__(self):
|
||||||
def __from_file(cls, filename):
|
# dump yml content
|
||||||
result = cls()
|
yml_string = yaml.dump(self.__yml_content, default_flow_style=False, sort_keys=False)
|
||||||
|
|
||||||
with open(filename, 'r') as stream:
|
# insert newline before every main key
|
||||||
try:
|
yml_string = re.sub(r'^(\S)', r'\n\1', yml_string, flags=re.MULTILINE)
|
||||||
result.__yml_content = yaml.safe_load(stream)
|
|
||||||
except yaml.YAMLError as exc:
|
|
||||||
print(exc)
|
|
||||||
|
|
||||||
return result
|
# extract header comment from default config
|
||||||
|
with open(DEFAULT_KIWI_CONF_NAME, 'r') as stream:
|
||||||
|
yml_header = stream.read().strip()
|
||||||
|
yml_header = re.sub(r'^[^#].*', r'', yml_header, flags=re.MULTILINE).strip()
|
||||||
|
yml_string = "{}\n{}".format(yml_header, yml_string)
|
||||||
|
|
||||||
@classmethod
|
return yml_string
|
||||||
def default(cls):
|
|
||||||
result = cls.__from_file(cls.KIWI_ROOT + "/default.kiwi.yml")
|
|
||||||
|
|
||||||
with open(cls.KIWI_ROOT + "/version-tag", 'r') as stream:
|
def __key_resolve(self, key):
|
||||||
result.__yml_content["version"] = stream.read().strip()
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
def __yml_resolve(self, key):
|
|
||||||
# "a:b:c" => path = ['a', 'b'], key = 'c'
|
# "a:b:c" => path = ['a', 'b'], key = 'c'
|
||||||
path = key.split(':')
|
path = key.split(':')
|
||||||
path, key = path[:-1], path[-1]
|
path, key = path[:-1], path[-1]
|
||||||
|
|
||||||
# resolve path
|
# resolve path
|
||||||
content = self.__yml_content
|
container = self.__yml_content
|
||||||
for step in path:
|
for step in path:
|
||||||
content = content[step]
|
container = container[step]
|
||||||
|
|
||||||
return content, key
|
return container, key
|
||||||
|
|
||||||
def __yml_get(self, key):
|
def __setitem__(self, key, value):
|
||||||
content, key = self.__yml_resolve(key)
|
container, key = self.__key_resolve(key)
|
||||||
return content[key]
|
container[key] = value
|
||||||
|
|
||||||
def __yml_set(self, key, value):
|
def __getitem__(self, key):
|
||||||
content, key = self.__yml_resolve(key)
|
container, key = self.__key_resolve(key)
|
||||||
content[key] = value
|
return container[key]
|
||||||
|
|
||||||
def __user_input(self, key, prompt):
|
def __update_from_file(self, filename):
|
||||||
# prompt user as per argument
|
with open(filename, 'r') as stream:
|
||||||
result = input("{} [Current: {}] ".format(prompt, self.__yml_get(key))).strip()
|
try:
|
||||||
|
self.__yml_content.update(yaml.safe_load(stream))
|
||||||
|
except yaml.YAMLError as exc:
|
||||||
|
logging.error(exc)
|
||||||
|
|
||||||
# store result if present
|
@classmethod
|
||||||
if result:
|
def default(cls):
|
||||||
self.__yml_set(key, result)
|
result = cls()
|
||||||
|
result.__update_from_file(DEFAULT_KIWI_CONF_NAME)
|
||||||
|
|
||||||
def user_input(self):
|
with open(KIWI_ROOT + "/version-tag", 'r') as stream:
|
||||||
self.__user_input("version", "Choose kiwi-config version")
|
result.__yml_content["version"] = stream.read().strip()
|
||||||
|
|
||||||
self.__user_input("markers:project", "Enter marker string for project directories")
|
return result
|
||||||
self.__user_input("markers:down", "Enter marker string for disabled projects")
|
|
||||||
|
|
||||||
self.__user_input("network:name", "Enter name for local docker network")
|
@classmethod
|
||||||
self.__user_input("network:cidr", "Enter CIDR block for local docker network")
|
def load(cls):
|
||||||
|
result = cls.default()
|
||||||
|
result.__update_from_file(KIWI_CONF_NAME)
|
||||||
|
|
||||||
self.__user_input("runtime:storage", "Enter main directory for local data")
|
return result
|
||||||
|
|
||||||
def dump(self):
|
def save(self):
|
||||||
yml_string = yaml.dump(self.__yml_content, default_flow_style=False, sort_keys=False)
|
pass
|
||||||
yml_string = re.sub(r'^(\S)', r'\n\1', yml_string, flags=re.MULTILINE)
|
|
||||||
|
|
||||||
with open(Config.KIWI_ROOT + "/default.kiwi.yml", 'r') as stream:
|
|
||||||
yml_header = stream.read().strip()
|
|
||||||
yml_header = re.sub(r'^[^#].*', r'', yml_header, flags=re.MULTILINE).strip()
|
|
||||||
yml_string = "{}\n{}".format(yml_header, yml_string)
|
|
||||||
|
|
||||||
print(yml_string)
|
|
||||||
|
|
56
src/kiwi/core.py
Normal file
56
src/kiwi/core.py
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
|
||||||
|
###########
|
||||||
|
# CONSTANTS
|
||||||
|
|
||||||
|
KIWI_ROOT = os.getenv('KIWI_ROOT', ".")
|
||||||
|
KIWI_CONF_NAME = os.getenv('KIWI_CONF_NAME', "kiwi.yml")
|
||||||
|
|
||||||
|
|
||||||
|
class Parser:
|
||||||
|
__instance = None
|
||||||
|
__subparsers = None
|
||||||
|
__args = None
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __init_instance(cls):
|
||||||
|
if not cls.__instance:
|
||||||
|
cls.__instance = argparse.ArgumentParser(description='kiwi-config')
|
||||||
|
|
||||||
|
cls.__subparsers = Parser.__instance.add_subparsers()
|
||||||
|
cls.__subparsers.required = True
|
||||||
|
cls.__subparsers.dest = 'command'
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_instance(cls):
|
||||||
|
cls.__init_instance()
|
||||||
|
return cls.__instance
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_subparsers(cls):
|
||||||
|
cls.__init_instance()
|
||||||
|
return cls.__subparsers
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_args(cls):
|
||||||
|
if not cls.__args:
|
||||||
|
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
|
Loading…
Reference in a new issue