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

weitersager.config   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 71
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0
wmc 6

5 Functions

Rating   Name   Duplication   Size   Complexity  
A _get_irc_channels() 0 5 2
A _get_irc_server() 0 7 1
A _get_irc_config() 0 13 1
A _get_http_config() 0 7 1
A load_config() 0 8 1
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