Code Duplication    Length = 50-50 lines in 2 locations

modules/qdb.py 1 location

@@ 84-133 (lines=50) @@
81
        except AttributeError:
82
            pass
83
        try:
84
            if_url = re.search(if_regex, quote).group("url")
85
        except AttributeError:
86
            pass
87
88
        if not tsd_url and not if_url:
89
            return quote
90
91
        if tsd_url:
92
          url = tsd_url
93
        if if_url:
94
          url = if_url
95
96
        repl = self._imgurify(url)
97
98
        if tsd_url:
99
          new_quote = re.sub(tsd_regex, repl[0]['link'], quote)
100
        if if_url:
101
          new_quote = re.sub(if_regex, repl[0]['link'], quote)
102
        return new_quote
103
    
104
    def strip_formatting(self, msg):
105
        """Uses regex to replace any special formatting in IRC (bold, colors) with nothing"""
106
        return re.sub('([\x02\x1D\x1F\x16\x0F]|\x03([0-9]{2})?)', '', msg)
107
108
    def add_buffer(self, event=None, debug=False): 
109
        """Takes a channel name and line passed to it and stores them in the bot's mem_store dict
110
        for future access. The dict will have channel as key. The value to that key will be a list
111
        of formatted lines of activity. 
112
        If the buffer size is not yet exceeded, lines are just added. If the buffer
113
        is maxed out, the oldest line is removed and newest one inserted at the beginning.
114
        """
115
        if debug:
116
            print "Line: " + event.line
117
            print "Verb: " + event.verb
118
            print "Channel: " + event.channel
119
            print ""
120
        if not event:
121
            return
122
        #there are certain things we want to record in history, like nick changes and quits
123
        #these often add to the humor of a quote. however, these are not specific to a channel
124
        #in IRC and our bot does not maintain a userlist per channel. Therefore, when nick
125
        #changes and quits occur, we will add them to every buffer. This is not technically
126
        #correct behavior and could very well lead to quits/nick changes that are not visible
127
        #showing up in a quote, but it's the best we can do at the moment
128
        if not event.channel:
129
            #discard events with unwanted verbs 
130
            if event.verb not in ["QUIT", "NICK"]:
131
                return
132
            try:
133
                for chan in self.bot.mem_store['qdb'].keys():
134
                    if chan != '_recent':
135
                        if len(self.bot.mem_store['qdb'][chan]) >= self.MAX_BUFFER_SIZE:
136
                            self.bot.mem_store['qdb'][chan].pop()

modules/replace.py 1 location

@@ 26-75 (lines=50) @@
23
    self.MAX_BUFFER_SIZE = 300 
24
    self.MAX_HISTORY_SIZE = 10
25
26
  def add_buffer(self, event=None, debug=False): 
27
    """Takes a channel name and line passed to it and stores them in the bot's mem_store dict
28
    for future access. The dict will have channel as key. The value to that key will be a list
29
    of formatted lines of activity. 
30
    If the buffer size is not yet exceeded, lines are just added. If the buffer
31
    is maxed out, the oldest line is removed and newest one inserted at the beginning.
32
    """
33
    if debug:
34
      print "Line: " + event.line
35
      print "Verb: " + event.verb
36
      print "Channel: " + event.channel
37
      print ""
38
    if not event:
39
      return
40
    #there are certain things we want to record in history, like nick changes and quits
41
    #these often add to the humor of a quote. however, these are not specific to a channel
42
    #in IRC and our bot does not maintain a userlist per channel. Therefore, when nick
43
    #changes and quits occur, we will add them to every buffer. This is not technically
44
    #correct behavior and could very well lead to quits/nick changes that are not visible
45
    #showing up in a quote, but it's the best we can do at the moment
46
    if not event.channel:
47
      #discard events with unwanted verbs 
48
      if event.verb not in ["QUIT", "NICK"]:
49
        return
50
      try:
51
        for chan in self.bot.mem_store['replace'].keys():
52
          if len(self.bot.mem_store['replace'][chan]) >= self.MAX_BUFFER_SIZE:
53
            self.bot.mem_store['replace'][chan].pop()
54
          line = self.format_line(event)
55
          if line:
56
            self.bot.mem_store['replace'][chan].insert(0, line)
57
      except KeyError, IndexError:
58
        print "Replace add_buffer() error when no event channel"
59
    #now we continue with normal, per channel line addition
60
    #create a dictionary associating the channel with an empty list if it doesn't exist yet
61
    # END if not event.channel:
62
    else:
63
      if event.channel not in self.bot.mem_store['replace']:
64
        self.bot.mem_store['replace'][event.channel] = []
65
      try:
66
      #check for the length of the buffer. if it's too long, pop the last item
67
        if len(self.bot.mem_store['replace'][event.channel]) >= self.MAX_BUFFER_SIZE:
68
          self.bot.mem_store['replace'][event.channel].pop()
69
        #get a line by passing event to format_line
70
        #insert the line into the first position in the list
71
        line = self.format_line(event) 
72
        if line:
73
          self.bot.mem_store['replace'][event.channel].insert(0, line)
74
      except IndexError:
75
        print "Replace add_buffer() error. Couldn't access the list index."
76
77
  def format_line(self, event):
78
    """Takes an event and formats a string appropriate for quotation from it"""