Files
@ 67ead6cfe058
Branch filter:
Location: FVDE/ennstatus/ennstatus/cli/commands/config.py
67ead6cfe058
3.1 KiB
text/x-python
Added GPL Boilerplate to python files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | # Ënnstatus
# Copyright (C) 2015 Dennis Fink
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import ipaddress
from pprint import pprint
import click
from ...utils import check_ip
@click.group(short_help='Configure ennstatus')
def config():
pass
@config.command('show', short_help='Show current configuration')
@click.pass_obj
def show(obj):
with obj['config_file'].open(encoding='utf-8') as f:
config = json.load(f)
pprint(config)
@config.group(short_help='Configure servers')
def server():
pass
@server.command('add', short_help='Add server')
@click.argument('name')
@click.option('-i', '--ips', prompt='IPs (comma separated)')
@click.password_option()
@click.option('--bridgeprogram/--no-bridgeprogram', default=False)
@click.pass_obj
def add(obj, name, ips, password, bridgeprogram):
with obj['config_file'].open() as f:
config = json.load(f)
name = name.lower()
ips = [ip.strip() for ip in ips.split(',')]
try:
if name in config['ENNSTATUS_SERVERS']:
try:
click.confirm('Server already exits! Overwrite?', abort=True)
except click.Abort as e:
raise SystemExit from e
except KeyError:
config['ENNSTATUS_SERVERS'] = {}
converted_ips = {ipaddress.ip_address(ip) for ip in ips}
for ip in converted_ips:
if not check_ip(ip):
raise SystemExit('ip {} is not accepted!'.format(str(ip)))
config['ENNSTATUS_SERVERS'][name] = {
'IPS': ips,
'PASSWORD': password
}
if bridgeprogram:
try:
config['ENNSTATUS_BRIDGE_PROGRAM'].append(name)
except KeyError:
config['ENNSTATUS_BRIDGE_PROGRAM'] = [name]
with obj['config_file'].open(mode='w', encoding='utf-8') as f:
json.dump(config, f, indent=4, sort_keys=True)
@server.command('delete', short_help='Remove server')
@click.argument('name')
@click.pass_obj
def delete(obj, name):
with obj['config_file'].open() as f:
config = json.load(f)
name = name.lower()
try:
del config['ENNSTATUS_SERVERS'][name]
except KeyError:
raise SystemExit('{} does not exists!'.format(name))
try:
config['ENNSTATUS_BRIDGE_PROGRAM'].remove(name)
except KeyError:
pass
with obj['config_file'].open(mode='w', encoding='utf-8') as f:
json.dump(config, f, indent=4, sort_keys=True)
filename = obj['data_dir'] / (name + '.json')
try:
filename.unlink()
except FileNotFoundError:
pass
|