From af217351af847868ad241cdf4ca0197b934d85d9 Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Wed, 9 Apr 2014 22:24:52 +0200 Subject: [PATCH] =?utf8?q?Regexp=20avec=20le=20flag=20UNICODE=20histoire?= =?utf8?q?=20de=20matcher=20correctement=20les=20espaces=20ins=C3=A9cables?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- config.py | 2 +- parrot.py | 2 +- quotes.py | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/config.py b/config.py index 25347ca..ad2d897 100644 --- a/config.py +++ b/config.py @@ -117,7 +117,7 @@ helpmsg_overops = u" SAY DO STAY NOSTAY OPS OVEROPS KICK DIE CRASH RESTORE" blacklisted_masks = [("Flo!*@*", [])] #: Regexp utilisée pour parser des quotes -quote_regexp = ur"""\s*(?:"|«|')\s*(?P.+?)\s*(?:"|»|') ?~ ?(?P.+?)\s*(?:$|\n)""" +quote_regexp = ur"""\s*(?:"|«|')\s*(?P.+?)\s*(?:"|»|')\s*~\s*(?P.+?)\s*(?:$|\n)""" #: Regexp utlisée pour parser une quote avec timestamp quote_regexp_with_timestamp = ur"(?P.{19}) " + quote_regexp diff --git a/parrot.py b/parrot.py index a507663..17d99c1 100755 --- a/parrot.py +++ b/parrot.py @@ -426,7 +426,7 @@ class Parrot(ircbot.SingleServerIRCBot): self.serv.privmsg(canal, "%s: %s" % (auteur, config.quote_display_url.encode("utf-8"))) else: # Vu que ce bot est prévu pour parser des quotes il va falloir bosser ici - match = quote_pattern.match(message) + match = self.quote_pattern.match(message) if match: d = match.groupdict() if self.quotedb.store(d["author"], d["content"]): diff --git a/quotes.py b/quotes.py index 35f4860..1365b12 100644 --- a/quotes.py +++ b/quotes.py @@ -11,6 +11,9 @@ import random import config +quote_matcher = re.compile(config.quote_regexp, flags=re.UNICODE) +quote_matcher_with_timestamp = re.compile(config.quote_regexp_with_timestamp, flags=re.UNICODE) + def get_now(): """ Renvoie la date actuelle """ return datetime.datetime(*time.localtime()[:6]) @@ -42,7 +45,6 @@ class Quote(object): Indépendamment de la date. """ return [self.author, self.content] == [otherquote.author, otherquote.content] -quote_matcher = re.compile(config.quote_regexp) def parse(text, date=None): """ Parse le ``text`` et renvoie une quote ou None. """ @@ -113,8 +115,7 @@ def restore(dump_file=None): with open(dump_file) as f: t = f.read() t = t.decode("utf-8") # Oui, ça peut fail, mais on ne doit alors pas continuer - regex = re.compile(config.quote_regexp_with_timestamp) - l = [m.groupdict() for m in regex.finditer(t)] + l = [m.groupdict() for m in quote_matcher_with_timestamp.finditer(t)] # On instancie les quotes grâce aux dicos qui ont déjà la bonne tronche l = [Quote(**q) for q in l] newquotedb = QuoteDB() -- 2.39.2