From 17a591cc0961a9e2a576b1830847662ee7c21fb5 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Sat, 22 Apr 2017 18:12:25 +0200 Subject: client: save and load alarms from hardcoded file --- client/ur_fuzzy_alarm_clock.py | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/client/ur_fuzzy_alarm_clock.py b/client/ur_fuzzy_alarm_clock.py index a202b35..5364e52 100755 --- a/client/ur_fuzzy_alarm_clock.py +++ b/client/ur_fuzzy_alarm_clock.py @@ -2,6 +2,7 @@ import datetime import io +import json import logging import sys @@ -115,10 +116,10 @@ class Alarm: self.w_fri.set_state(values[5]) self.w_sat.set_state(values[6]) self.w_sun.set_state(values[7]) - self.w_month.set_edit_text(values[8]) - self.w_day.set_edit_text(values[9]) - self.w_hour.set_edit_text(values[10]) - self.w_minute.set_edit_text(values[11]) + self.w_month.set_edit_text(str(values[8])) + self.w_day.set_edit_text(str(values[9])) + self.w_hour.set_edit_text(str(values[10])) + self.w_minute.set_edit_text(str(values[11])) def get_values(self): return [ @@ -177,7 +178,12 @@ class UI: footer = urwid.Text('a, b, c') footer_map = urwid.AttrMap(footer,'footer') - header = urwid.Text('q: Quit r: Read s: Set n: time=Now ?:Help') + header = urwid.Text( + 'q: Quit ' + + 'r: Read s: Set n: time=Now ' + + 'f: to File l: Load ' + + '?:Help' + ) header_map = urwid.AttrMap(header,'header') ui = urwid.Frame(self.main_body,footer=footer_map,header=header_map) @@ -188,7 +194,9 @@ class UI: 'r': self.read_alarms, 's': self.set_alarms, '?': self.print_help, - 'q': self.quit + 'q': self.quit, + 'f': self.save_alarms, + 'l': self.load_alarms, }.get(cmd,self.do_nothing)() def do_nothing(self): @@ -204,6 +212,24 @@ class UI: for i,alarm in enumerate(self.alarms): self.fac.set_alarm(i,alarm.get_values()) + def save_alarms(self, fname='saved_alarms.json'): + alarms = [a.get_values() for a in self.alarms] + + with open(fname,'w') as fp: + json.dump(alarms, fp) + + def load_alarms(self, fname='saved_alarms.json'): + try: + with open(fname) as fp: + alarms = json.load(fp) + except FileNotFoundError: + logging.warning('No such file: {fname}'.format(fname=fname)) + return False + # TODO: do some validation on the data + for i, a in enumerate(self.alarms): + pass + a.set_values(alarms[i]) + def print_help(self): pass -- cgit v1.2.3