or that kind of stuff"""
import time, datetime
-import re
import os
import sys
-import urllib
import subprocess
import json
os.chdir('/home/vincent/scripts/today/')
self.green = "\e[1;32m"
else:
self.endcolor=self.blue=self.red=self.green=""
+ #: Serveur distant où aller récupérer les checks
+ self.distant_server = "pimeys"
+ #: path de today-server.py sur le serveur distant
+ self.path_today_server = "/home/vincent/scripts/today/today-server.py"
#: Fichier contenant les anniversaires
self.birthdays_file = "birthdays.txt"
#: Fichier contenant les évènements à venir
with open(config.last_seen_file) as f:
return json.loads(f.read())
-def update_last_seen(what, value):
+def update_last_seen(newdict):
"""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
+ lasts.update(newdict)
with open(config.last_seen_file, "w") as f:
f.write(json.dumps(lasts))
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("#")]
+ rawdata = [l.strip().decode("utf8") for l in f.readlines()[1:] if not l.strip().startswith("#") and not l.strip() == '']
data = []
for l in rawdata:
date, truc = l.split("\t",1)
cmd = "~/bin/dtc %s %s --json" % (args[0], args[1])
else:
return None
- proc = subprocess.Popen(["ssh", "pimeys", cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ proc = subprocess.Popen(["ssh", config.distant_server, cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
out += err
quotes = json.loads(out)
if quotes:
last_id = max([q["id"] for q in quotes])
- update_lasts("dtc", last_id)
+ update_last_seen({"dtc" : last_id})
textquotes = format_quotes(quotes)
return textquotes
def update_xkcd(newid):
- update_last_seen("xkcd", int(newid))
+ update_last_seen({"xkcd" : int(newid)})
def check_all():
"""Vérifie si il y a des derniers trucs non lus/vus."""
- cmd = "/home/vincent/scripts/today/today-server.py whatsup"
- proc = subprocess.Popen(["ssh", "pimeys", cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ cmd = "%s whatsup" % (config.path_today_server,)
+ proc = subprocess.Popen(["ssh", config.distant_server, cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
news = json.loads(out)
seen = get_last_seen()
f.write(contents[ifile])
f.close()
+def sync():
+ """Synchronise les last_seen avec le serveur distant qui en garde une copie,
+ 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],
+ stdin = subprocess.PIPE, stdout=subprocess.PIPE,
+ close_fds = True)
+ lasts_raw = json.dumps(lasts)
+ proc.stdin.write(lasts_raw)
+ proc.stdin.close()
+ out = proc.stdout.read()
+ newdict = json.loads(out)
+ update_last_seen(newdict)
+ print "Nouvel état : %r" % newdict
+
+
#: Les actions effectuées lors d'un appel sans paramètres
AUTOMATED_ACTIONS = {
"dtc" : get_dtc,
"ping" : ping,
"show" : affiche,
+ "sync" : sync,
"init" : initialize,
}