GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f371b1...288e12 )
by
unknown
7s
created

PsywerxGroups._handle_mentions()   F

Complexity

Conditions 12

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 12
dl 0
loc 32
rs 2.7855

How to fix   Complexity   

Complexity

Complex classes like PsywerxGroups._handle_mentions() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
from base import PsywerxPlugin
2
import re
3
import json
4
5
6
class PsywerxGroups(PsywerxPlugin):
7
8
    def __init__(self, bot=None):
9
        super(PsywerxGroups, self).__init__(bot=bot)
10
        self.actions = {
11
            '@mygroup': self._basic_action('irc/mygroups'),
12
            '@group': self._basic_action('irc/groups'),
13
            '@leaveall': self._basic_action('irc/leaveAll'),
14
            '@leave': self._leave_action,
15
            '@join': self._join_action,
16
        }
17
18
    def _basic_action(self, url):
19
        def _req(channel, params, msg_lower):
20
            return self.request(channel, url, params)
21
        return _req
22
23
    def _join_action(self, channel, params, msg_lower):
24
        def parse_join(splt):
25
            if len(splt) == 2:
26
                return splt[1].replace('@', ''), False
27
            elif len(splt) == 3:
28
                return splt[1].replace('@', ''), True
29
            else:
30
                return False
31
        g = parse_join(msg_lower.split(' '))
32
        if not g:
33
            return
34
35
        params['group'] = g[0]
36
        params['offline'] = g[1]
37
        return self.request(channel, 'irc/join', params)
38
39
    def _leave_action(self, channel, params, msg_lower):
40
        splited = msg_lower.split(' ')
41
        if len(splited) == 2:
42
            group = splited[1].replace('@', '')
43
            params['group'] = group
44
            return self.request(channel, 'irc/leave', params)
45
46
    def _handle_actions(self, channel, nick, msg):
47
        msg_lower = msg.lower()
48
        for a in self.actions.keys():
49
            if msg_lower.startswith(a):
50
                params = {'nick': nick}
51
                response = self.actions[a](channel, params, msg_lower)
52
                if response:
53
                    self.bot.say(response.replace('"', ''), channel)
54
                return True
55
        return False
56
57
    def _handle_mentions(self, channel, nick, msg):
58
        msg_lower = msg.lower()
59
        mentions = set()
60
        offline_mentions = set()
61
62
        for group in re.findall(r'@(\w+\'?)', msg_lower):
63
            # if @group' ends with ', or empty msg, don't send to offline
64
            offline_mention = group[-1] != "'" and \
65
                re.match(r'(@\w+\'?[ \^]*)+$', msg_lower.strip()) is None
66
67
            if group[-1] == "'":
68
                group = group[:-1]
69
70
            if nick.lower() == '_haibot_':
71
                continue
72
73
            params = {'group': group}
74
            response = self.request(channel, 'irc/mention', params)
75
            if response:
76
                for n, _, o in json.loads(response):
77
                    if n.lower() == nick.lower():
78
                        continue
79
                    if n.lower() in self.bot.known_users[channel]:
80
                        mentions.add(n.encode('ascii', 'ignore'))
81
                    elif o and offline_mention:
82
                        offline_mentions.add(n.encode('ascii', 'ignore'))
83
84
        if mentions:
85
            self.bot.say("CC: " + ', '.join(mentions), channel)
86
        if offline_mentions:
87
            self.bot.say("@msg " + ','.join(offline_mentions)
88
                         + " " + msg, channel)
89
90
    def handle_message(self, channel, nick, msg, line=None):
91
        if not self._handle_actions(channel, nick, msg):
92
            self._handle_mentions(channel, nick, msg)
93