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.

PsywerxGroups._handle_mentions()   F
last analyzed

Complexity

Conditions 12

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
dl 0
loc 32
rs 2.7855
c 0
b 0
f 0

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