#!/usr/bin/env python import urwid class FuzzyAlarmClock: def __init__(self,device): self.device = device def set_time_to_now(self): pass def read_alarms(self): return [ [ True, True, True, True, True, True, True, True, 0, 0, 10, 23 ], [ False, False, False, False, False, False, False, False, 10, 20, 10, 23 ], [ True, True, True, False, True, True, False, True, 0, 0, 10, 23 ], [ True, True, False, True, True, False, True, False, 12, 1, 10, 23 ], ] class ModEdit(urwid.IntEdit): def __init__(self,mod,caption="",default=None,offset=0): self.mod = mod self.offset = offset super(ModEdit,self).__init__(caption,default) def insert_text(self,text): super(ModEdit,self).insert_text(text) self.set_edit_text(str((self.value() - self.offset)% self.mod+self.offset)) class Alarm: def __init__(self,aid,fac): self.w_aid = urwid.Text(str(aid)) self.w_repeat = urwid.CheckBox("") self.w_mon = urwid.CheckBox("") self.w_tue = urwid.CheckBox("") self.w_wed = urwid.CheckBox("") self.w_thu = urwid.CheckBox("") self.w_fri = urwid.CheckBox("") self.w_sat = urwid.CheckBox("") self.w_sun = urwid.CheckBox("") self.w_month = ModEdit(12,offset=1) self.w_day = ModEdit(31,offset=1) self.w_hour = ModEdit(24,default=0) self.w_minute = ModEdit(60,default=0) self.widgets = urwid.Columns([self.w_aid, self.w_repeat, self.w_mon, self.w_tue, self.w_wed, self.w_thu, self.w_fri, self.w_sat, self.w_sun, self.w_month, self.w_day, self.w_hour, self.w_minute, ], dividechars=1) def set_values(self,values): self.w_repeat.set_state(values[0]) self.w_mon.set_state(values[1]) self.w_tue.set_state(values[2]) self.w_wed.set_state(values[3]) self.w_thu.set_state(values[4]) 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]) class UI: def __init__(self,fac): self.fac = fac self.alarm_number=4 self.frame = self.build_ui() self.loop = urwid.MainLoop(self.frame,self.palette,unhandled_input=self.read_commands) def build_ui(self): self.palette = [ ('header', 'light gray', 'dark blue'), ('footer', 'dark cyan', 'black') ] self.alarms = [] for i in xrange(self.alarm_number): self.alarms.append(Alarm(i,self.fac)) alarm_headers = urwid.Columns([ urwid.Text('#'), urwid.Text('rep'), urwid.Text('Mon'), urwid.Text('Tue'), urwid.Text('Wed'), urwid.Text('Thu'), urwid.Text('Fri'), urwid.Text('Sat'), urwid.Text('Sun'), urwid.Text('mm'), urwid.Text('dd'), urwid.Text('HH'), urwid.Text('MM'), ],dividechars=1) self.main_body = urwid.ListBox([alarm_headers]+[al.widgets for al in self.alarms]) 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_map = urwid.AttrMap(header,'header') ui = urwid.Frame(self.main_body,footer=footer_map,header=header_map) return ui def read_commands(self,cmd): {'n': self.fac.set_time_to_now, 'r': self.read_alarms, 's': self.set_alarms, '?': self.print_help, 'q': self.quit }.get(cmd,self.do_nothing)() def do_nothing(self): pass def read_alarms(self): alarms = self.fac.read_alarms() for i, al in enumerate(alarms): self.alarms[i].set_values(al) def set_alarms(self): pass def print_help(self): pass def quit(self): raise urwid.ExitMainLoop() def run(self): self.loop.run() def main(): fac = FuzzyAlarmClock('/dev/ttyUSB0') ui = UI(fac) ui.run() if __name__ == '__main__': main()