X-Git-Url: http://gitweb.pimeys.fr/?a=blobdiff_plain;f=today.py;h=c08f8e6e155742af049d4db8893135de209f13b7;hb=b51baa9dd11477238323855734eeefd82145d5e5;hp=9120e6d9b5ea29f26fc008d39ff61ba680fc5941;hpb=a65f7ca93329cf3df3259bc4c574728d3de3db18;p=today.git diff --git a/today.py b/today.py index 9120e6d..c08f8e6 100755 --- a/today.py +++ b/today.py @@ -6,6 +6,7 @@ or that kind of stuff""" import time, datetime +import re import os import sys import subprocess @@ -32,7 +33,7 @@ class Config(object): #: Fichier contenant les évènements à venir self.timers_file = "timers.txt" #: Fichier contenant les fêtes à souhaiter - self.saints_file = "saints.txt" + self.saints_file = "saints.json" #: Fichier contenant les ids des derniers trucs vus/lus self.last_seen_file = "lasts" #: Fichier contenant le timestamp de dernière exécution @@ -157,6 +158,53 @@ def get_birthdays(*search): birthdays = add_title(u"Anniversaires", birthdays) return birthdays +def _parse_saint(s): + """Renvoie la liste des fêtes contenue dans un jour""" + l = s.split(",") + ll = [] + for st in l: + if st[0] == "&": + ll.append(["St ", st[1:]]) + elif st[0] == "!": + ll.append(["Ste ", st[1:]]) + return ll + +def _get_firstnames(): + """Récupère la liste des noms des gens connus""" + birthdays = parse_datefile(config.birthdays_file) + firstnames = [b[1].split()[0] for b in birthdays] + return firstnames + +def get_saints(): + """Renvoie la liste des fêtes à souhaiter aujourd'hui et demain""" + sourcesaints = json.load(open(config.saints_file)) + now = get_now() + saints = {} + saints["today"] = _parse_saint(sourcesaints[now.month - 1][now.day - 1]) + nbdays = len(sourcesaints[now.month - 1]) + if now.day == nbdays: # il faut regarder le mois suivant + if now.month == 12: # il faut regarder l'année suivante + saints["tomorrow"] = _parse_saint(sourcesaints[0][0]) + else: + saints["tomorrow"] = _parse_saint(sourcesaints[now.month][0]) + else: + saints["tomorrow"] = _parse_saint(sourcesaints[now.month - 1][now.day]) + firstnames = _get_firstnames() + towish = {"today" : [], "tomorrow" : []} + for day in ["today", "tomorrow"]: + ssaints = saints[day] + for (sexe, saint) in ssaints: + if exists([firstname.lower() in saint.lower().split() for firstname in firstnames]): + towish[day].append(sexe + saint) + ttowish = [] + if towish["today"]: + ttowish.append(u"Aujourd'hui :\n" + "\n".join(towish["today"])) + if towish["tomorrow"]: + ttowish.append(u"Demain :\n" + "\n".join(towish["tomorrow"])) + saints = "\n".join(ttowish) + saints = add_title(u"Fêtes à souhaiter", saints) + return saints + def check_birthdays(): """Compte combien il y a d'anniversaires à afficher""" birthdays = get_birthdays() @@ -166,6 +214,11 @@ def check_birthdays(): n = 0 return n +def check_saints(): + """Compte combien il y a de fêtes à afficher""" + saints = get_saints() + return len(re.findall("\nSt", saints)) + def format_quotes(liste): """Formate les quotes de dicos à texte""" t = (u"\n" + u"_"*80 + u"\n").join([u"%(id)s (%(date)s)\n%(quote)s" % q for q in liste]) @@ -182,7 +235,7 @@ def get_dtc(*args): cmd = "~/bin/dtc %s %s --json" % (args[0], args[1]) else: return None - proc = subprocess.Popen(["ssh", config.distant_server, cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + proc = subprocess.Popen(["ssh", "-4", config.distant_server, cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate() out += err quotes = json.loads(out) @@ -233,16 +286,18 @@ THINGS = { "grenier" : u"Épisodes du joueur du grenier", "birthdays" : u"Anniversaires à souhaiter", + "saints" : u"Fêtes à souhaiter", } def check_all(): """Vérifie si il y a des derniers trucs non lus/vus.""" cmd = "%s whatsup" % (config.path_today_server,) - proc = subprocess.Popen(["ssh", config.distant_server, cmd], stdout=subprocess.PIPE) + proc = subprocess.Popen(["ssh", "-4", config.distant_server, cmd], stdout=subprocess.PIPE) out, err = proc.communicate() news = json.loads(out) seen = get_last_seen() news["birthdays"] = check_birthdays() + news["saints"] = check_saints() checks = [] for (thing, comm) in THINGS.iteritems(): n = news[thing] - seen.get(thing, 0) @@ -316,7 +371,7 @@ def initialize(): config.last_seen_file] contents = [u"# Anniversaires\n#jj/mm/aaaa Prénom Nom\n", u"# Évènements à venir\n#jj/mm/aaaa Évènement\n", - u"# Fêtes\n#jj/mm/aaaa Saint\n", + json.dumps([[""]*31]*12).decode(), u"{}"] for ifile in range(len(files)): namefile = files[ifile] @@ -332,7 +387,7 @@ def sync(): le maximum de chaque truc vu est gardé des deux côtés.""" lasts = get_last_seen() cmd = "%s sync" % (config.path_today_server,) - proc = subprocess.Popen(["ssh", config.distant_server, cmd], + proc = subprocess.Popen(["ssh", "-4", config.distant_server, cmd], stdin = subprocess.PIPE, stdout=subprocess.PIPE, close_fds = True) lasts_raw = json.dumps(lasts) @@ -349,6 +404,7 @@ def sync(): AUTOMATED_ACTIONS = { "timers" : get_timers, "birth" : get_birthdays, + "saints" : get_saints, "check" : check_all, }