#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import wx
import wx.lib.masked as masked
from mx import DateTime
from library import i18n
_ = i18n.i18n()
class NoAlarmError(ValueError): pass
class F_Alarm(wx.Dialog):
""" Frame for add or modify an alarm
"""
def __init__(self, parent, call_back, fax_date=None):
""" Pass me a date. if any
"""
super(F_Alarm, self).__init__(parent, title=_("date_hour"))
self._call_back = call_back
p = wx.Panel(self)
szall = wx.BoxSizer(wx.VERTICAL)
sz = wx.FlexGridSizer(2, 2, 5, 5)
self._ctrl_date = wx.DatePickerCtrl(p, size=(120,-1),
style=wx.DP_DROPDOWN | wx.DP_SHOWCENTURY)
self._ctrl_hour = masked.TimeCtrl(p, display_seconds=False)
btnsizer = wx.StdDialogButtonSizer()
bt_save = wx.Button(p, wx.ID_OK)
btcancel = wx.Button(p, wx.ID_CANCEL)
btnsizer.AddButton(bt_save)
btnsizer.AddButton(btcancel)
bt_save.SetDefault()
btnsizer.Realize()
#bt_save = wx.Button(p, label=_("bt_save"))
bt_save.Bind(wx.EVT_BUTTON, self._on_bt_save)
flags = wx.ALL | wx.ALIGN_CENTER
sz.Add(wx.StaticText(p, label=_("date")), 0, flags, 10)
sz.Add(self._ctrl_date, 0, flags, 10)
sz.Add(wx.StaticText(p, label=_("hour")), 0, flags, 10)
sz.Add(self._ctrl_hour, 0, flags, 10)
#sz.Add((0,0))
#sz.Add(bt_save, 0, wx.ALL, 10)
szall.Add(sz, 0, wx.ALL, 7)
szall.Add(btnsizer, 0, wx.ALL, 7)
p.SetSizerAndFit(szall)
self.SetClientSize( szall.GetSize() )
if fax_date:
fax_date = DateTime.localtime(fax_date)
else:
fax_date = DateTime.now()
self._set_date_time(fax_date)
self.CenterOnParent()
self.ShowModal()
def _set_date_time(self, value):
"""
"""
dt = wx.DateTime.Now()
dt.SetYear(value.year); dt.SetMonth(value.month -1); dt.SetDay(value.day)
self._ctrl_hour.SetValue(value)
self._ctrl_date.SetValue(dt)
def _on_bt_save(self, evt):
"""
"""
self._call_back(self._ctrl_date.GetValue(), self._ctrl_hour.GetValue(as_wxDateTime=True))
self.Close()
class FaxAlarm(object):
""" Do the work for show fax alarms
"""
SECTION = "fax_alarm"
def __init__(self, save_alarm):
""" Pass me a config parse object
"""
self._save_alarm = save_alarm
def get_alarm(self, id_fax=None):
""" Return the alarm for id_fax if id_fax have value. If id_fax, but not alarm,
raise NoAlarmError
If not id_fax, return all the alarms
"""
if not id_fax:
return [x for x in self._save_alarm.options(self.SECTION)]
if id_fax and not self._save_alarm.has_option(id_fax, self.SECTION):
raise NoAlarmError("No such fax alarm %s" % id_fax)
else:
return self._save_alarm.getint(id_fax, self.SECTION)
def set_alarm(self, id_fax, value):
""" Set the alarm
"""
self._save_alarm.set(id_fax, str(value), self.SECTION)
def has_alarm(self, id_fax):
""" Return if the id_fax has an alarm
"""
if ( self._save_alarm.has_option(id_fax, self.SECTION)
and self._save_alarm.get(id_fax, self.SECTION)):
return True
else:
return None
def clean_old_alarms(self):
""" Clean the alarms older that 30 days
"""
old_value = time.time() - (30 * 24 * 60 * 60)
for fax_id in self.get_alarm():
if self.get_alarm(fax_id) < old_value:
self._save_alarm.remove_option(fax_id, self.SECTION)
def get_expired(self):
""" Return the expired fax, if any
"""
fax_lst = []
now = time.time()
for fax_id in self.get_alarm():
if self.get_alarm(fax_id) < now:
fax_lst.append(fax_id)
return fax_lst
def clean_no_present(self, buff, col_id):
""" Clean the no more present fax
"""
return
"""
for row_data in buff:
fax_id = buff[col_id]
try:
if self.get_alarm(fax_id) and not fax_id in ( x[col_id] for x in row_data)
except NoAlarmError:
#no alarm for this
continue
self._save_alarm.remove_option(fax_id, self.SECTION)
"""
def delete_alarm(self, fax_id):
""" Delete the alarm
"""
self._save_alarm.remove_option(fax_id, self.SECTION)
def f_add_alarm(self, parent, fax_id):
""" Frame that add a new fax_id
"""
self._working_fax_id = fax_id
F_Alarm(parent, self._call_back_ok)
def f_modify_alarm(self, parent, fax_id):
""" Frame that modify a fax_id alarm
"""
self._working_fax_id = fax_id
F_Alarm(parent, self._call_back_ok, self.get_alarm(fax_id))
def _call_back_ok(self, v_date, v_hour):
""" Set the date and time
"""
self.set_alarm( self._working_fax_id,
time.mktime( (v_date.GetYear(), v_date.GetMonth() +1, v_date.GetDay(),
v_hour.GetHour(), v_hour.GetMinute(), 0, -1, -1, -1) )
)
|