cucchiaiata/src/cucchiaiata/config.py

45 lines
1.5 KiB
Python

"""Risotto command line configuration parser
"""
from os.path import isfile, expanduser, join
from yaml import load, SafeLoader, YAMLError
from .i18n import _
class Config:
def __init__(self):
config_file = join(expanduser("~"), '.zephir-config.yaml')
if not isfile(config_file):
print(_('Attention, there is no configuration file'))
url = input(_('Address to Risotto server: '))
version = input(_('Risotto API\'s version (default: "v1"): '))
if not version:
version = "v1"
yaml_template = f"""url: {url}
version: {version}"""
with open(config_file, 'w') as fh:
fh.write(yaml_template)
print(_('config file {} created').format(config_file))
with open(config_file, 'r') as stream:
try:
config = load(stream, Loader=SafeLoader)
except YAMLError as err:
raise Exception(_('Error when creating the config file {}').format(err))
self.url = config['url']
self.version = config['version']
self.debug = 'debug' in config
self.remote_url = 'http://{}/api/{}'.format(self.url, self.version)
self.token_file = join(expanduser("~"), '.zephir-client.jwt.token')
self.indent = config.get('indent', 2)
TO_JSON = {'config.session.server.get': ['content'],
'config.session.servermodel.get': ['content'],
'server.describe': ['configuration'],
'server.exec.describe': ['return']}
config = Config()