]> gitweb.pimeys.fr Git - bots/parrot.git/blobdiff - quotes.py
Changement d'ops
[bots/parrot.git] / quotes.py
index e8e40610b0b67e7e013b445463acd465e6d29a0f..7391cf81849b804be125f3cdcbfa6ae11161b3a2 100644 (file)
--- a/quotes.py
+++ b/quotes.py
@@ -54,7 +54,7 @@ class Quote(object):
     def jsonize(self):
         d = {"author" : self.author, "content" : self.content,
              "timestamp" : self.timestamp.strftime(u"%F_%T"),
-             "place" : self.place, "quoter" : self.quoter}
+             "place" : self.proper_place, "quoter" : self.proper_quoter}
         return d
     
     def __get_proper_place(self):
@@ -184,35 +184,42 @@ class QuoteDB(object):
         """ Sort une quote aléatoire de ``author`` """
         return random.choice(self.quotesfrom(author))
     
-    def search(self, inquote=None, author=None, regexp=False):
-        """Fait une recherche dans les quotes."""
+    def search(self, inquote=None, author=None, place=None, regexp=False):
+        """
+        Fait une recherche dans les quotes.
+        C'est une conjonction de cas : on garde la quote si
+        ``inquote`` matche dans le contenu
+        *et* si ``author`` matche l'auteur
+        *et* si ``place`` matche la place
+        
+        Si ``regexp=True``, utilise directement les termes comme des regexp.
+        """
+        params = [inquote, author, place]
         if regexp:
-            if inquote is None:
-                inquote = ".*"
-            if author is None:
-                author = ".*"
-            qreg = re.compile(inquote, flags=re.UNICODE)
-            areg = re.compile(author, flags=re.UNICODE)
-            l = [q for q in self.quotelist if qreg.match(q.content) and areg.match(q.author)]
+            regexps = []
+            for param in params:
+                if param is None:
+                    param = u".*"
+                regexps.append(re.compile(param, flags=re.UNICODE + re.IGNORECASE))
+            l = [q for q in self.quotelist if all([reg.match(truc) for (reg, truc) in zip(regexps, [q.content, q.author, q.proper_place])])]
         else:
-            if inquote is None:
-                inquote = ""
-            if author is None:
-                author = ""
-            l = [q for q in self.quotelist if inquote in q.content and author in q.author]
+            for (i, param) in enumerate(params):
+                if param is None:
+                    params[i] = u""
+            l = [q for q in self.quotelist if all([param.lower() in truc.lower() for (param, truc) in zip(params, [q.content, q.author, q.proper_place])])]
         return l
     
     def search_authors(self, author=None, regexp=False):
         """Renvoie la liste des auteurs contenant ``author`` ou qui matchent la regexp."""
         if regexp:
             if author is None:
-                author = ".*"
-            areg = re.compile(author, flags=re.UNICODE)
+                author = u".*"
+            areg = re.compile(author, flags=re.UNICODE + re.IGNORECASE)
             l = list(set([q.author for q in self.quotelist if areg.match(q.author)]))
         else:
             if author is None:
-                author = ""
-            l = list(set([q.author for q in self.quotelist if author in q.author]))
+                author = u""
+            l = list(set([q.author for q in self.quotelist if author.lower() in q.author.lower()]))
         return l
 
 def dump(quotedb, dump_file=None):