From: Vincent Le Gallic Date: Mon, 8 Apr 2013 01:19:03 +0000 (+0200) Subject: sync côté client X-Git-Url: http://gitweb.pimeys.fr/?a=commitdiff_plain;h=bddd179c766be89b43b9dab27fc76846b5e5d680;p=today.git sync côté client --- diff --git a/.gitignore b/.gitignore index 89c0c47..3c0814d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.pyc lasts +lasts_sync birthdays.txt timers.txt saints.txt diff --git a/today.py b/today.py index d73d915..503cf50 100755 --- a/today.py +++ b/today.py @@ -6,10 +6,8 @@ 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/') @@ -25,6 +23,10 @@ class Config(object): self.green = "" 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 @@ -72,13 +74,10 @@ def get_last_seen(): 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)) @@ -87,7 +86,7 @@ def parse_datefile(namefile): 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) @@ -183,24 +182,24 @@ def get_dtc(*args): 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() @@ -293,6 +292,23 @@ def initialize(): 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 = { @@ -307,6 +323,7 @@ OTHER_ACTIONS = { "dtc" : get_dtc, "ping" : ping, "show" : affiche, + "sync" : sync, "init" : initialize, }