New release.
[memo.git] / Memo.py
blob21e4541d34190345f50e7e3819c1269a20ca3fd0
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 time, = node.getElementsByTagName('time')
22 message, = node.getElementsByTagName('message')
24 message = ''.join([n.nodeValue for n in message.childNodes])
26 return Memo(float(time.childNodes[0].nodeValue),
27 message, flag('at'), flag('silent'), flag('hidden'))
29 class Memo(gobject.GObject):
30 # 'time' is seconds since epoch
31 # 'at' is TRUE if the time of day matters
32 def __init__(self, time, message, at, silent = 0, hidden = 0):
33 self.__gobject_init__()
35 assert at == 0 or at == 1
36 assert silent == 0 or silent == 1
37 assert hidden == 0 or hidden == 1
39 self.time = int(time)
40 self.message = message.strip()
41 self.at = at
42 self.silent = silent
43 self.hidden = hidden
44 self.brief = self.message.split('\n', 1)[0]
46 def str_when(self):
47 now_y, now_m, now_d = time.localtime(time.time() + 5 * 60)[:3]
48 now_m = now_m - 1
50 year, month, day, hour, min = time.localtime(self.time)[:5]
51 month = month - 1
53 if year != now_y:
54 return '%s-%d' % (month_name[month][:3], year)
56 if month != now_m or day != now_d:
57 return '%02d-%s' % (day, month_name[month][:3])
59 if self.at:
60 return str_time(hour, min)
62 return _('Today')
64 def comes_after(self, other):
65 return self.time > other.time
67 def save(self, parent):
68 doc = parent.ownerDocument
70 node = doc.createElement('memo')
71 node.setAttribute('at', str(self.at))
72 node.setAttribute('silent', str(self.silent))
73 node.setAttribute('hidden', str(self.hidden))
74 parent.appendChild(node)
76 time = doc.createElement('time')
77 time.appendChild(doc.createTextNode(str(self.time)))
78 node.appendChild(time)
80 message = doc.createElement('message')
81 message.appendChild(doc.createTextNode(self.message))
82 node.appendChild(message)
84 def set_hidden(self, hidden):
85 assert hidden == 0 or hidden == 1
87 self.hidden = hidden