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)
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)
def test_quote_groupdict(self): result = quote_groupdict(self.groupdict1, self.quoteList1) self.assertEqual(result['a'], urllib.quote(self.groupdict1['a'])) self.assertEqual(result['b'], self.groupdict1['b'])