Errbot Dynamic Regex

176 Views Asked by At

I will generate the regex from a file or something and need it in the @re_botcmd

but i get the error: "not defined" is there a way do define a variable that re_botcmd is finding?

from errbot import BotPlugin, re_botcmd
from pathlib import Path
import re

class ModHelper(BotPlugin):
 """Help Mods Warning User and kick/ban them"""

 def activate(self):
     self.my_file = Path("./filter.txt")
     if not self.my_file.is_file():
         return

     self.filter = open('filter.txt', 'r')
     for self.tmp in self.filter:
         if not self['reg']:
             self['reg'] = '(',self.tmp,')'
         else:
             self['reg'] = self['reg'],'|(',self.tmp,')'

     return super().activate()


 @re_botcmd(pattern=self['reg'], prefixed=False, flags=re.IGNORECASE)
 def test_warn(self, msg, match):
      """Test"""
      return "Warn User"

Error from Log file:

Errbot‎: File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/home/errbot/errbot-root/plugins/err-modhelper/modhelper.py", line 5, in <module>
class ModHelper(BotPlugin):
File "/home/errbot/errbot-root/plugins/err-modhelper/modhelper.py", line 23, in ModHelper
@re_botcmd(pattern=self['reg'], prefixed=False, flags=re.IGNORECASE)
NameError: name 'self' is not defined

Thanks ~

2

There are 2 best solutions below

0
On

I had a similar problem when recognizing mentions of Jira issues in chat; the recognizer needed to know the list of Jira projects. I solved it by getting that list at compile time in a small utility class and then having the recognizer pick up the list at run time:

class JiraProjects(object):

    def __init__(self):
        jira = Jira()
        self.list = jira.active_projects()
        self.recognizer = r'\b(?P<issue>(?:%s)-\d+)\b' % '|'.join(self.list)

projects = JiraProjects()

class JiraPlugin(BotPlugin):

    def activate(self):
        self.jira = Jira()
        super().activate()

@re_botcmd(prefixed=False, pattern=projects.recognizer, flags=re.IGNORECASE, matchall=True)
    def jira_recognize(self, msg, matches):
        """
        Provides information about any jira issue that is mentioned
        """
        for match in matches:
            issue = self.jira.get_issue(match.group(0))
            yield self.show_issue(issue)
2
On

Try somethin like this:

class ModHelper(BotPlugin):
 """Help Mods Warning User and kick/ban them"""
 def __init__(self, reg=None):
    self.reg = reg

 def activate(self):
     self.my_file = Path("./filter.txt")
     if not self.my_file.is_file():
         return

     self.filter = open('filter.txt', 'r')
     for self.tmp in self.filter:
         if not self.reg:
             self.reg = '(',self.tmp,')'
         else:
             self.reg = self.reg,'|(',self.tmp,')'

     return super().activate()


 @re_botcmd(pattern=self.reg, prefixed=False, flags=re.IGNORECASE)
 def test_warn(self, msg, match):
      """Test"""
      return "Warn User"