Completed
Push — main ( 8ad40f...5f8723 )
by Jochen
01:34
created

weitersager.processor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 63.41%

Importance

Changes 0
Metric Value
wmc 10
eloc 66
dl 0
loc 115
rs 10
c 0
b 0
f 0
ccs 26
cts 41
cp 0.6341

6 Methods

Rating   Name   Duplication   Size   Complexity  
A Processor.connect_to_signals() 0 4 1
A Processor.__init__() 0 3 1
A Processor.enable_channel() 0 3 1
A Processor.handle_shutdown_requested() 0 2 1
A Processor.run() 0 6 2
A Processor.handle_message() 0 18 2

2 Functions

Rating   Name   Duplication   Size   Complexity  
A start_with_args() 0 16 1
A start() 0 27 1
1
"""
2
weitersager.processor
3
~~~~~~~~~~~~~~~~~~~~~
4
5
Connect HTTP server and IRC bot.
6
7
:Copyright: 2007-2020 Jochen Kupperschmidt
8
:License: MIT, see LICENSE for details.
9
"""
10
11 1
from time import sleep
12
13 1
from .argparser import parse_args
14 1
from .httpreceiver import ReceiveServer
15 1
from .irc import create_bot
16 1
from .signals import (
17
    channel_joined,
18
    message_approved,
19
    message_received,
20
    shutdown_requested,
21
)
22 1
from .util import log
23
24
25 1
class Processor:
26
27 1
    def __init__(self):
28 1
        self.enabled_channel_names = set()
29 1
        self.shutdown = False
30
31 1
    def connect_to_signals(self):
32 1
        channel_joined.connect(self.enable_channel)
33 1
        message_received.connect(self.handle_message)
34 1
        shutdown_requested.connect(self.handle_shutdown_requested)
35
36 1
    def enable_channel(self, sender, *, channel_name=None):
37 1
        log('Enabled forwarding to channel {}.', channel_name)
38 1
        self.enabled_channel_names.add(channel_name)
39
40 1
    def handle_message(
41
        self, sender, *, channel_name=None, text=None, source_address=None
42
    ):
43
        """Log and announce an incoming message."""
44 1
        source = f'{source_address[0]}:{source_address[1]:d}'
45
46 1
        log(
47
            'Received message from {} for channel {} with text "{}"',
48
            source,
49
            channel_name,
50
            text,
51
        )
52
53 1
        if channel_name not in self.enabled_channel_names:
54
            log('Could not send message to channel {}, not joined.', channel_name)
55
            return
56
57 1
        message_approved.send(channel_name=channel_name, text=text)
58
59 1
    def handle_shutdown_requested(self, sender):
60
        self.shutdown = True
61
62 1
    def run(self):
63
        """Run the main loop until shutdown is requested."""
64
        while not self.shutdown:
65
            sleep(0.5)
66
67
        log('Shutting down ...')
68
69
70 1
def start(
71
    irc_server,
72
    irc_nickname,
73
    irc_realname,
74
    irc_channels,
75
    http_ip_address,
76
    http_port,
77
    **options,
78
):
79
    """Start the IRC bot and HTTP listen server."""
80
    bot = create_bot(
81
        irc_server, irc_nickname, irc_realname, irc_channels, **options
82
    )
83
    message_approved.connect(bot.say)
84
85
    processor = Processor()
86
87
    # Up to this point, no signals must have been sent.
88
89
    processor.connect_to_signals()
90
91
    # Signals are allowed be sent from here on.
92
93
    ReceiveServer.start(http_ip_address, http_port)
94
    bot.start()
95
96
    processor.run()
97
98
99 1
def start_with_args(irc_channels, **options):
100
    """Start the IRC bot and HTTP listen server,
101
102
    Most arguments (except for the IRC channels to join) are read from
103
    the command line.
104
    """
105
    args = parse_args()
106
107
    start(
108
        args.irc_server,
109
        args.irc_nickname,
110
        args.irc_realname,
111
        irc_channels,
112
        args.http_ip_address,
113
        args.http_port,
114
        **options,
115
    )
116