Nicklist.handle()   F
last analyzed

Complexity

Conditions 10

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
dl 0
loc 25
rs 3.1304
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like Nicklist.handle() 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 util import strip_nick
2
from event import Event
3
try:
4
  from basemodule import BaseModule
5
except ImportError:
6
  from modules.basemodule import BaseModule
7
class Nicklist(BaseModule):
8
  def post_init(self):
9
    nicklisting_self_join = Event("__.nicklisting_self_join__")
10
    nicklisting_other_join = Event("__.nicklisting_other_join__")
11
    nicklisting_quit = Event("__.nicklisting_quit__")
12
    nicklisting_part = Event("__.nicklisting_part__")
13
14
    nicklisting_command = Event("__.nicklist__")
15
    nicklisting_command.define(msg_definition="^\.nicklist")
16
17
    nicklisting_self_join.define(message_id=353)
18
    nicklisting_other_join.define(definition="JOIN")
19
    nicklisting_quit.define(definition="QUIT")
20
    nicklisting_part.define(definition="PART")
21
22
    nicklisting_command.subscribe(self)
23
    nicklisting_self_join.subscribe(self)
24
    nicklisting_other_join.subscribe(self)
25
    nicklisting_quit.subscribe(self)
26
    nicklisting_part.subscribe(self)
27
28
    # register ourself to our new custom event(s)
29
    self.bot.register_event(nicklisting_self_join, self)
30
    self.bot.register_event(nicklisting_other_join, self)
31
    self.bot.register_event(nicklisting_quit, self)
32
    self.bot.register_event(nicklisting_command,self)
33
    self.bot.register_event(nicklisting_part,self)
34
35
    self.bot.mem_store['nicklist'] = dict()
36
37
  def handle(self, event):
38
    if event.message_id == 353:
39
      self.bot.mem_store['nicklist'][event.channel] = event.line.split(":")[2].split()
40
    
41
    if event.msg.startswith(".nicklist"):
42
      print self.bot.mem_store['nicklist'][event.channel]
43
44
    if event._type == "__.nicklisting_other_join__":
45
      try:
46
        self.bot.mem_store['nicklist'][event.channel].append(strip_nick(event.user))
47
      except KeyError:
48
        self.bot.mem_store['nicklist'][event.channel] = list()
49
50
    if event._type == "__.nicklisting_part__":
51
      try:
52
        self.bot.mem_store['nicklist'][event.channel].remove(strip_nick(event.user))
53
      except ValueError:
54
        pass
55
56
    if event._type == "__.nicklisting_quit__":
57
      try:
58
        for name, chan in self.bot.mem_store['nicklist'].iteritems():
59
          chan.remove(strip_nick(event.user))
60
      except ValueError:
61
        pass
62