self.green = "\e[1;32m"
else:
self.endcolor=self.blue=self.red=self.green=""
+ #: Fichier contenant les anniversaires
+ self.birthdays_file = "birthdays.txt"
+ #: Fichier contenant les évènements à venir
+ self.timers_file = "timers.txt"
+ #: Fichier contenant les fêtes à souhaiter
+ self.saints_file = "saints.txt"
+ #: Fichier contenant les ids des derniers trucs vus/lus
+ self.last_seen_file = "lasts"
if "--color" in sys.argv:
sys.argv.remove("--color")
def get_last_seen():
"""Récupère la liste des derniers trucs vus/lus"""
- with open("lasts") as f:
+ with open(config.last_seen_file) as f:
return json.loads(f.read())
-def update_lasts(what, value):
+def update_last_seen(what, value):
"""Met à jour un des derniers trucs vus/lus"""
lasts = get_last_seen()
if not what in lasts.keys():
print """%r n'est pas un "truc vu/lu" valide""" % (what,)
return
lasts[what] = value
- with open("lasts", "w") as f:
+ with open(config.last_seen_file, "w") as f:
f.write(json.dumps(lasts))
+def parse_datefile(namefile):
+ """Ouvre et parse un fichier avec des lignes de la forme
+ jj/mm/aaaa Truc
+ """
+ with open(namefile) as f:
+ rawdata = [l.strip().decode("utf8") for l in f.readlines()[1:] if not l.strip().startswith("#")]
+ data = []
+ for l in rawdata:
+ date, truc = l.split("\t",1)
+ date = datetime.datetime(*time.strptime(date,"%d/%m/%Y")[:7])
+ data.append([date, truc])
+ return data
+
+
def get_timers():
"""Obtenir la liste des évènements à venir (J-n)"""
now = get_now()
- f=open("timers.txt", "r")
- data = [l.strip("\n").decode("utf8") for l in f.readlines()[1:]]
- f.close()
+ data = parse_datefile(config.timers_file)
timers = []
- for l in data:
- date, event = l.split("\t",1)
- date = datetime.datetime(*time.strptime(date,"%d/%m/%Y")[:7])
+ for [date, event] in data:
delta = date - now + datetime.timedelta(1)
if delta > datetime.timedelta(0):
timers.append([event,delta.days])
"""Obtenir la liste des anniversaires à venir,
ou la liste des anniversaires correspondants à une recherche"""
now = get_now()
- text = open("birthdays.txt","r").readlines()
- liste = [l.decode("utf8").split() for l in text if not l.startswith("#")]
- liste = [[l[0], " ".join(l[1:])] for l in liste if l!=[]]
- liste = [ [datetime.datetime(*time.strptime(l[0],"%d/%m/%Y")[:7]),
- l[1]]
- for l in liste]
+ liste = parse_datefile(config.birthdays_file)
birthdays = []
if len(search) == 0:
# Simple demande d'anniversaires courants
def get_dtc(*args):
"""Récupère les quotes DTC non lues"""
if len(args) == 0:
- last_dtc = get_last_seen()["dtc"]
+ last_dtc = get_last_seen().get("dtc", 0)
cmd = "~/bin/dtc %s + --json" % (last_dtc+1,)
elif len(args) == 1:
cmd = "~/bin/dtc %s --json" % args[0]
return textquotes
def update_xkcd(newid):
- update_lasts("xkcd", int(newid))
+ update_last_seen("xkcd", int(newid))
def check_all():
news = json.loads(out)
seen = get_last_seen()
n_birth = check_birthdays()
- n_dtc = news["dtc"] - seen["dtc"]
- n_xkcd = news["xkcd"] - seen["xkcd"]
+ n_dtc = news["dtc"] - seen.get("dtc", 0)
+ n_xkcd = news["xkcd"] - seen.get("xkcd", 0)
l = [["Anniversaires", n_birth],
["Quotes DTC", n_dtc],
["XKCD non lus", n_xkcd]]
_there_is_something_in_today(False)
return out
+def initialize():
+ """Crée les fichiers (vides) nécessaires au fonctionnement du script"""
+ files = [config.birthdays_file, config.timers_file, config.saints_file,
+ config.last_seen_file]
+ contents = ["# Anniversaires\n#jj/mm/aaaa Prénom Nom\n",
+ "# Évènements à venir\n#jj/mm/aaaa Évènement\n",
+ "# Fêtes\n#jj/mm/aaaa Saint\n",
+ "{}"]
+ for ifile in range(len(files)):
+ namefile = files[ifile]
+ if os.path.isfile(namefile):
+ print "%s exists, skipping." % (namefile,)
+ else:
+ f = open(namefile, "w")
+ f.write(contents[ifile])
+ f.close()
+
+
#: Les actions effectuées lors d'un appel sans paramètres
AUTOMATED_ACTIONS = {
"timers" : get_timers,
"dtc" : get_dtc,
"ping" : ping,
"show" : affiche,
+
+ "init" : initialize,
}
#: Toutes les actions