]> gitweb.pimeys.fr Git - bots/parrot.git/blob - quotes.py
Demander "link" ou "url" marche aussi bien que "display"
[bots/parrot.git] / quotes.py
1 #!/usr/bin/env python
2 # -*- encoding: utf-8 -*-
3
4 """ Gestion des quotes """
5
6 import datetime
7 import time
8 import re
9 import json
10 import random
11
12 import config
13
14 quote_matcher = re.compile(config.quote_regexp, flags=re.UNICODE)
15 quote_matcher_with_timestamp = re.compile(config.quote_regexp_with_timestamp, flags=re.UNICODE)
16 spaces_matcher = re.compile(u"\s", flags=re.U)
17
18 def equivalence_partition(iterable, relation):
19 """ Partitionne l'itérable en classes d'équivalences. """
20 classes = []
21 for o in iterable:
22 # find the class it is in
23 found = False
24 for c in classes:
25 if relation( iter(c).next(), o ): # is it equivalent to this class?
26 c.add( o )
27 found = True
28 break
29 if not found: # it is in a new class
30 classes.append( set( [ o ] ) )
31 return classes
32
33 def get_now():
34 """ Renvoie la date actuelle """
35 return datetime.datetime(*time.localtime()[:6])
36
37 def sanitize_author(raw):
38 """Proprifie l'auteur : enlève les espaces insécables."""
39 return spaces_matcher.sub(u" ", raw)
40
41 class Quote(object):
42 """ Une citation """
43 def __init__(self, author, content, timestamp=None, place=None, quoter=None):
44 if timestamp is None:
45 timestamp = get_now()
46 elif isinstance(timestamp, basestring):
47 timestamp = datetime.datetime(*time.strptime(timestamp, u"%Y-%m-%d_%H:%M:%S")[:6])
48 self.author = sanitize_author(author)
49 self.content = content
50 self.timestamp = timestamp
51 self.place = place
52 self.quoter = quoter
53
54 def jsonize(self):
55 d = {"author" : self.author, "content" : self.content,
56 "timestamp" : self.timestamp.strftime(u"%F_%T"),
57 "place" : self.place, "quoter" : self.quoter}
58 return d
59
60 def __unicode__(self):
61 """ Retourne la quote affichable """
62 return config.quote_template % self.__dict__
63 def __str__(self):
64 return unicode(self).encode("utf-8")
65
66 def full_str(self):
67 """ Retourne une chaîne représentant la totalité des infos de la quote,
68 tout en étant parsable et human-readable. """
69 place = self.place if self.place and self.place.strip() != u"" else u""
70 quoter = self.quoter if self.quoter and self.quoter.strip() != u"" else u""
71 return (u"%s %s | %s | %s" % (self.timestamp.strftime("%F_%T"), config.quote_template % self.__dict__
72 , place, quoter)).encode("utf-8")
73
74 def __eq__(self, otherquote):
75 """ Vérifie si cette phrase n'a pas déjà été dite par la même personne.
76 Indépendamment de la date et de la casse. """
77 return [self.author.lower(), self.content.lower()] == [otherquote.author.lower(), otherquote.content.lower()]
78
79
80 def parse(text, date=None):
81 """ Parse le ``text`` et renvoie une quote ou None. """
82 if date == None:
83 date = get_now()
84 get = quote_matcher.match(text)
85 if not get is None:
86 d = get.groupdict()
87 return Quote(d["author"], d["content"], date)
88
89 def load_file(filename):
90 """ Récupère les quotes depuis le fichier """
91 with open(filename) as f:
92 jsonquotes = json.load(f)
93 quotes = [Quote(**q) for q in jsonquotes]
94 return quotes
95
96 def save_file(quotes, filename):
97 """ Enregistre les quotes dans le fichier """
98 with open(filename, "w") as f:
99 raws = [q.jsonize() for q in quotes]
100 json.dump(raws, f)
101
102 class QuoteDB(object):
103 """ Stocke et distribue des quotes. """
104 def __init__(self):
105 self.quotelist = []
106
107 def load(self):
108 """ Charge le fichier de quotes dans la DB """
109 self.quotelist = load_file(config.quote_file)
110
111 def save(self):
112 """ Sauvegarde la DB dans le fichier de quotes """
113 save_file(self.quotelist, config.quote_file)
114
115 def _collapse_author(self, author):
116 """ Renvoie ``author`` avec la casse déjà utilisée si il a déjà été quoté
117 sinon, le renvoie sans le modifier. """
118 authors = list(set([q.author for q in self.quotelist if q.author.lower() == author.lower()]))
119 if len(authors) > 1:
120 print "Warning : authors %s" % authors
121 if authors:
122 return authors[0]
123 else:
124 return author
125
126 def get_clash_authors(self):
127 """ Renvoie une liste de liste d'auteurs qui sont enregistrés avec des casses différentes. """
128 authors = list(set([q.author for q in self.quotelist]))
129 authors = equivalence_partition(authors, lambda x,y: x.lower() == y.lower())
130 authors = [list(c) for c in authors if len(c) > 1]
131 return authors
132
133 def store(self, timestamp=None, **kwargs):
134 """ Enregistre une nouvelle quote, sauf si elle existe déjà.
135 Force l'auteur à utiliser la même casse si un auteur de casse différente existait déjà.
136 Renvoie ``True`` si elle a été ajoutée, ``False`` si elle existait. """
137 kwargs["author"] = self._collapse_author(kwargs["author"])
138 kwargs["timestamp"] = timestamp
139 newquote = Quote(**kwargs)
140 if not newquote in self.quotelist:
141 self.quotelist.append(newquote)
142 return True
143 return False
144
145 def __repr__(self):
146 return repr(self.quotelist)
147
148 def random(self):
149 """ Sort une quote aléatoire """
150 return random.choice(self.quotelist)
151
152 def quotesfrom(self, author):
153 """ Sort toutes les quotes de ``author`` """
154 return [q for q in self.quotelist if q.author == author]
155 def randomfrom(self, author):
156 """ Sort une quote aléatoire de ``author`` """
157 return random.choice(self.quotesfrom(author))
158
159 def search(self, inquote=None, author=None, regexp=False):
160 """Fait une recherche dans les quotes."""
161 if regexp:
162 if inquote is None:
163 inquote = ".*"
164 if author is None:
165 author = ".*"
166 qreg = re.compile(inquote, flags=re.UNICODE)
167 areg = re.compile(author, flags=re.UNICODE)
168 l = [q for q in self.quotelist if qreg.match(q.content) and areg.match(q.author)]
169 else:
170 if inquote is None:
171 inquote = ""
172 if author is None:
173 author = ""
174 l = [q for q in self.quotelist if inquote in q.content and author in q.author]
175 return l
176
177 def search_authors(self, author=None, regexp=False):
178 """Renvoie la liste des auteurs contenant ``author`` ou qui matchent la regexp."""
179 if regexp:
180 if author is None:
181 author = ".*"
182 areg = re.compile(author, flags=re.UNICODE)
183 l = list(set([q.author for q in self.quotelist if areg.match(q.author)]))
184 else:
185 if author is None:
186 author = ""
187 l = list(set([q.author for q in self.quotelist if author in q.author]))
188 return l
189
190 def dump(quotedb, dump_file=None):
191 """Pour exporter les quotes dans un format readable vers un fichier."""
192 if dump_file is None:
193 dump_file = config.quote_dump_file
194 t = "\n".join([q.full_str() for q in quotedb.quotelist]) + "\n"
195 with open(dump_file, "w") as f:
196 f.write(t)
197
198 def restore(dump_file=None):
199 """Crée un DB de quotes en parsant le contenu d'un fichier de dump."""
200 if dump_file is None:
201 dump_file = config.quote_dump_file
202 with open(dump_file) as f:
203 t = f.read()
204 t = t.decode("utf-8") # Oui, ça peut fail, mais on ne doit alors pas continuer
205 l = [m.groupdict() for m in quote_matcher_with_timestamp.finditer(t)]
206 # On instancie les quotes grâce aux dicos qui ont déjà la bonne tronche
207 l = [Quote(**q) for q in l]
208 newquotedb = QuoteDB()
209 newquotedb.quotelist = l
210 return newquotedb