Beispiel #1
0
def _quip_respond(message):
    """ Search for matching quip, respond if exists """
    for phrase in db.helga_quip.entries.find():
        result = re.search(phrase['regex'], message, re.I)
        if result:
            quip = phrase['kind']

            # get the quote list option; if it doesn't exist set it to
            # empty list.
            quotelist = phrase['options'].get('-q', None)
            quotelist = quotelist.split(",") if quotelist else []

            # TODO this will handle either named groups or positional. it needs
            # some work to support hybrid backreferenced named and positonal
            # groups, but I may implement in the near future. putting in this
            # current work as value added, but hopefully I'll not be lazy soon.

            # take care of backreferenced named groups, ez mode
            if result.groupdict():
                try:
                    quip = quip.format(
                        **quote_groupdict(result.groupdict(), quotelist))
                except IndexError:
                    # really python, no partial format support? i have
                    # to write my own formatter (using different $ syntax) or
                    # override a dictionaries __missing__? lame, defer for now
                    # http://stackoverflow.com/questions/11283961/partial-string-formatting
                    pass
            # take care of positional arguments
            else:
                quip = quip.format(*quote_group(result.groups(), quotelist))
            return ('success', quip)
Beispiel #2
0
def _quip_respond(message):
    """ Search for matching quip, respond if exists """
    for phrase in db.helga_quip.entries.find():
        result = re.search(phrase['regex'], message, re.I)
        if result:
            quip = phrase['kind']

            # get the quote list option; if it doesn't exist set it to
            # empty list.
            quotelist = phrase['options'].get('-q', None)
            quotelist = quotelist.split(",") if quotelist else []

            # TODO this will handle either named groups or positional. it needs
            # some work to support hybrid backreferenced named and positonal
            # groups, but I may implement in the near future. putting in this
            # current work as value added, but hopefully I'll not be lazy soon.

            # take care of backreferenced named groups, ez mode
            if result.groupdict():
                try:
                    quip = quip.format(**quote_groupdict(result.groupdict(), quotelist))
                except IndexError:
                    # really python, no partial format support? i have
                    # to write my own formatter (using different $ syntax) or
                    # override a dictionaries __missing__? lame, defer for now
                    # http://stackoverflow.com/questions/11283961/partial-string-formatting
                    pass
            # take care of positional arguments
            else:
                quip = quip.format(*quote_group(result.groups(), quotelist))
            return ('success', quip)
Beispiel #3
0
 def test_quote_group_empty(self):
     result = quote_group(None, [])
     quip = "foo".format(*result)
     self.assertEqual("foo", quip)
Beispiel #4
0
 def test_quote_group(self):
     result = quote_group(self.group1, self.quoteList2)
     self.assertEqual(result[1], urllib.quote(self.group1[1]))
     self.assertEqual(self.group1[0], result[0])
     self.assertEqual(self.group1[2], result[2])