pinhook/pinhook/cli.py

61 lines
2.0 KiB
Python
Raw Normal View History

2019-08-27 19:12:26 +00:00
import click
from .bot import Bot
from marshmallow import Schema, fields, validate, INCLUDE
class Config(Schema):
nickname = fields.Str(required=True)
channels = fields.List(fields.Str(), required=True)
server = fields.Str(required=True)
port = fields.Int()
ops = fields.List(fields.Str())
ssl_required = fields.Bool()
plugin_dir = fields.Str()
ns_pass = fields.Str()
log_level = fields.Str(validate=validate.OneOf(['debug', 'warn', 'info', 'off', 'error']))
server_pass = fields.Str()
class Meta:
unknown = INCLUDE
2019-08-28 19:22:56 +00:00
def read_conf(config, conf_format):
2019-08-27 19:12:26 +00:00
schema = Config()
if not conf_format:
if config.name.endswith('.json'):
conf_format = 'json'
elif config.name.endswith(('.yaml', '.yml')):
conf_format = 'yaml'
elif config.name.endswith(('.toml', '.tml')):
conf_format = 'toml'
else:
click.echo('Could not detect file format, please supply using --format option', err=True)
if conf_type == 'json':
import json
2019-08-27 19:12:26 +00:00
to_json = json.loads(config.read())
output = schema.load(to_json)
elif conf_type == 'yaml':
try:
import yaml
except ImportError:
click.echo('yaml not installed, please use `pip3 install pinhook[yaml]` to install', err=True)
else:
to_yaml = yaml.load(config.read())
output = schema.load(to_yaml)
elif conf_type = 'toml':
try:
import toml
except ImportError:
click.echo('toml not installed, please use `pip3 install pinhook[toml]` to install', err=True)
else:
to_toml = toml.loads(config.read())
output = schema.loads(to_toml)
return output
2019-08-27 19:12:26 +00:00
@click.command()
@click.argument('config', type=click.File('rb'))
@click.option('--format', '-f', 'conf_format', type=click.Choice(['json', 'yaml', 'toml']))
2019-08-28 19:22:56 +00:00
def cli(config, conf_format):
config = read_conf(config, conf_format)
2019-08-27 19:12:26 +00:00
bot = Bot(**config)
bot.start()