X-Git-Url: http://gitweb.pimeys.fr/?p=bots%2Fparrot.git;a=blobdiff_plain;f=quotes.py;h=f6e4bc3dabbcf471f46cd6e123d9a403a5bac9b7;hp=2ff299e65a19b779e9b2db4f402c8021d4fdcdf6;hb=e16477da6ca556666492b839f5a79ec84c41018d;hpb=51b7d8da6804bd5018f5ecd169875fad92084ee6 diff --git a/quotes.py b/quotes.py index 2ff299e..f6e4bc3 100644 --- a/quotes.py +++ b/quotes.py @@ -40,7 +40,7 @@ def sanitize_author(raw): class Quote(object): """ Une citation """ - def __init__(self, author, content, timestamp=None): + def __init__(self, author, content, timestamp=None, place=None, quoter=None): if timestamp is None: timestamp = get_now() elif isinstance(timestamp, basestring): @@ -48,10 +48,13 @@ class Quote(object): self.author = sanitize_author(author) self.content = content self.timestamp = timestamp + self.place = place + self.quoter = quoter def jsonize(self): d = {"author" : self.author, "content" : self.content, - "timestamp" : self.timestamp.strftime(u"%F_%T")} + "timestamp" : self.timestamp.strftime(u"%F_%T"), + "place" : self.place, "quoter" : self.quoter} return d def __unicode__(self): @@ -60,6 +63,13 @@ class Quote(object): def __str__(self): return unicode(self).encode("utf-8") + def full_str(self): + """ Retourne une chaîne représentant la totalité des infos de la quote, + tout en étant parsable et human-readable. """ + place = self.place if self.place else "" + quoter = self.quoter if self.quoter else "" + return (u"%s %s | %s | %s" % (self.timestamp.strftime("%F_%T"), unicode(self), place, quoter)).encode("utf-8") + def __eq__(self, otherquote): """ Vérifie si cette phrase n'a pas déjà été dite par la même personne. Indépendamment de la date et de la casse. """ @@ -119,11 +129,13 @@ class QuoteDB(object): authors = [list(c) for c in authors if len(c) > 1] return authors - def store(self, author, content, timestamp=None): + def store(self, timestamp=None, **kwargs): """ Enregistre une nouvelle quote, sauf si elle existe déjà. Force l'auteur à utiliser la même casse si un auteur de casse différente existait déjà. Renvoie ``True`` si elle a été ajoutée, ``False`` si elle existait. """ - newquote = Quote(self._collapse_author(author), content, timestamp) + kwargs["author"] = self._collapse_author(kwargs["author"]) + kwargs["timestamp"] = timestamp + newquote = Quote(**kwargs) if not newquote in self.quotelist: self.search self.quotelist.append(newquote) @@ -178,7 +190,7 @@ def dump(quotedb, dump_file=None): """Pour exporter les quotes dans un format readable vers un fichier.""" if dump_file is None: dump_file = config.quote_dump_file - t = "\n".join(["%s %s" % (q.timestamp.strftime("%F_%T"), q) for q in quotedb.quotelist]) + "\n" + t = "\n".join([q.full_str() for q in quotedb.quotelist]) + "\n" with open(dump_file, "w") as f: f.write(t)