Added missing import
[memo.git] / Memo.py
blob55f79fb8e30f899110b93a0d868ad2f921d5310d
1 import os
2 import string
3 import time
5 import gobject
7 from pretty_time import month_name, str_time
9 def memo_from_node(node):
10 assert node.localName == 'memo'
12 def flag(attr):
13 v = node.getAttribute(attr)
14 if v == 'True': return True
15 if v == 'False': return False
16 try:
17 return bool(int(v))
18 except:
19 return 0
21 state = node.getAttribute('state')
22 if state is None or state == "":
23 if flag('silent'):
24 state = Memo.DONE
25 else:
26 state = Memo.READY
27 time, = node.getElementsByTagName('time')
28 message, = node.getElementsByTagName('message')
29 nosound = False
30 soundfile = None
31 soundnode = node.getElementsByTagName('sound')
32 if len(soundnode) == 1:
33 if len(soundnode[0].childNodes) == 1:
34 soundfile = soundnode[0].childNodes[0].nodeValue
35 if soundnode[0].hasAttribute('disabled'):
36 nosound = True
38 message = ''.join([n.nodeValue for n in message.childNodes])
40 return Memo(float(time.childNodes[0].nodeValue),
41 message, flag('at'), state, flag('hidden'), soundfile, nosound)
43 class Memo(gobject.GObject):
45 # Constants for memo 'state' attribute:
46 READY = 'ready'
47 EARLY = 'early'
48 DONE = 'done'
50 # 'time' is seconds since epoch
51 # 'at' is TRUE if the time of day matters
52 def __init__(self, time, message, at, state = READY, hidden = 0, \
53 soundfile = None, nosound = False):
54 self.__gobject_init__()
56 assert at == 0 or at == 1
57 assert state == Memo.READY or state == Memo.EARLY or state == Memo.DONE
58 assert hidden == 0 or hidden == 1
60 self.time = int(time)
61 self.message = message.strip()
62 self.at = at
63 self.state = state
64 self.hidden = hidden
65 self.brief = self.message.split('\n', 1)[0]
66 self.soundfile = soundfile
67 self.nosound = nosound
69 def str_when(self):
70 now_y, now_m, now_d = time.localtime(time.time() + 5 * 60)[:3]
71 now_m = now_m - 1
73 year, month, day, hour, min = time.localtime(self.time)[:5]
74 month = month - 1
76 if year != now_y:
77 return '%s-%d' % (month_name[month][:3], year)
79 if month != now_m or day != now_d:
80 return '%02d-%s' % (day, month_name[month][:3])
82 if self.at:
83 return str_time(hour, min)
85 return _('Today')
87 def comes_after(self, other):
88 return self.time > other.time
90 def save(self, parent):
91 doc = parent.ownerDocument
93 node = doc.createElement('memo')
94 node.setAttribute('at', str(self.at))
95 node.setAttribute('state', self.state)
96 node.setAttribute('hidden', str(self.hidden))
97 parent.appendChild(node)
99 time = doc.createElement('time')
100 time.appendChild(doc.createTextNode(str(self.time)))
101 node.appendChild(time)
103 message = doc.createElement('message')
104 message.appendChild(doc.createTextNode(self.message))
105 node.appendChild(message)
107 if self.nosound or (self.soundfile is not None and self.soundfile != ""):
108 sound = doc.createElement('sound')
109 if self.nosound:
110 sound.setAttribute('disabled', "True")
111 else:
112 sound.appendChild(doc.createTextNode(self.soundfile))
113 node.appendChild(sound)
115 def set_hidden(self, hidden):
116 assert hidden == 0 or hidden == 1
118 self.hidden = hidden