Completed
Branch development (19ca9f)
by Jochen
02:17
created

weitersager.config._get_http_config()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
"""
2
weitersager.config
3
~~~~~~~~~~~~~~~~~~
4
5
Configuration loading
6
7
:Copyright: 2007-2020 Jochen Kupperschmidt
8
:License: MIT, see LICENSE for details.
9
"""
10
11 1
import rtoml
12
13 1
from .irc import Channel, Config as IrcConfig, Server as IrcServer
14
15
16 1
DEFAULT_HTTP_HOST = '127.0.0.1'
17 1
DEFAULT_HTTP_PORT = 8080
18 1
DEFAULT_IRC_SERVER_PORT = 6667
19 1
DEFAULT_IRC_REALNAME = 'Weitersager'
20
21
22 1
def load_config(path):
23
    """Load configuration from file."""
24 1
    data = rtoml.load(path)
25
26 1
    http_host, http_port = _get_http_config(data)
27 1
    irc_config = _get_irc_config(data)
28
29 1
    return irc_config, http_host, http_port
30
31
32 1
def _get_http_config(data):
33 1
    data_http = data.get('http', {})
34
35 1
    host = data_http.get('host', DEFAULT_HTTP_HOST)
36 1
    port = int(data_http.get('port', DEFAULT_HTTP_PORT))
37
38 1
    return host, port
39
40
41 1
def _get_irc_config(data):
42 1
    data_irc = data['irc']
43
44 1
    server = _get_irc_server(data_irc)
45 1
    nickname = data_irc['bot']['nickname']
46 1
    realname = data_irc['bot'].get('realname', DEFAULT_IRC_REALNAME)
47 1
    channels = list(_get_irc_channels(data_irc))
48
49 1
    return IrcConfig(
50
        server=server,
51
        nickname=nickname,
52
        realname=realname,
53
        channels=channels,
54
    )
55
56
57 1
def _get_irc_server(data_irc):
58 1
    data_server = data_irc['server']
59 1
    host = data_server['host']
60 1
    port = int(data_server.get('port', DEFAULT_IRC_SERVER_PORT))
61 1
    password = data_server.get('password')
62
63 1
    return IrcServer(host, port, password)
64
65
66 1
def _get_irc_channels(data_irc):
67 1
    for channel in data_irc.get('channels', []):
68 1
        name = channel['name']
69 1
        password = channel.get('password')
70
        yield Channel(name, password)
71