Skip to content

Commit 8e11e2b

Browse files
author
Johannes Gijsbers
committed
Add 'safeget' method to hyperdb, including tests...
...and use it to simplify code in roundupdb.
1 parent 535faa6 commit 8e11e2b

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed

roundup/hyperdb.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: hyperdb.py,v 1.92 2003-11-16 18:41:40 jlgijsbers Exp $
18+
# $Id: hyperdb.py,v 1.93 2003-11-16 19:59:10 jlgijsbers Exp $
1919

2020
"""
2121
Hyperdatabase implementation, especially field types.
@@ -572,6 +572,12 @@ def index(self, nodeid):
572572
'''
573573
raise NotImplementedError
574574

575+
def safeget(self, nodeid, propname, default=None):
576+
try:
577+
return self.get(nodeid, propname)
578+
except (KeyError, IndexError):
579+
return default
580+
575581
class HyperdbValueError(ValueError):
576582
''' Error converting a raw value into a Hyperdb value '''
577583
pass

roundup/roundupdb.py

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: roundupdb.py,v 1.93 2003-11-06 19:01:57 jlgijsbers Exp $
18+
# $Id: roundupdb.py,v 1.94 2003-11-16 19:59:09 jlgijsbers Exp $
1919

2020
__doc__ = """
2121
Extending hyperdb with types specific to issue-tracking.
@@ -133,10 +133,8 @@ def nosymessage(self, nodeid, msgid, oldvalues, whichnosy='nosy',
133133
If 'msgid' is None, the message gets sent only to the nosy
134134
list, and it's called a 'System Message'.
135135
"""
136-
authid, recipients = None, []
137-
if msgid:
138-
authid = self.db.msg.get(msgid, 'author')
139-
recipients = self.db.msg.get(msgid, 'recipients')
136+
authid = self.db.msg.safeget(msgid, 'author')
137+
recipients = self.db.msg.safeget(msgid, 'recipients', [])
140138

141139
sendto = []
142140
seen_message = dict([(recipient, 1) for recipient in recipients])
@@ -192,36 +190,30 @@ def send_message(self, nodeid, msgid, note, sendto, from_address=None):
192190
users = self.db.user
193191
messages = self.db.msg
194192
files = self.db.file
195-
196-
inreplyto, messageid = None, None
197-
if msgid:
198-
inreplyto = messages.get(msgid, 'inreplyto')
199-
messageid = messages.get(msgid, 'messageid')
200-
201-
# make up a messageid if there isn't one (web edit)
202-
if not messageid:
203-
# this is an old message that didn't get a messageid, so
204-
# create one
205-
messageid = "<%s.%s.%s%s@%s>"%(time.time(), random.random(),
206-
self.classname, nodeid,
207-
self.db.config.MAIL_DOMAIN)
208-
messages.set(msgid, messageid=messageid)
193+
194+
inreplyto = messages.safeget(msgid, 'inreplyto')
195+
messageid = messages.safeget(msgid, 'messageid')
196+
197+
# make up a messageid if there isn't one (web edit)
198+
if not messageid:
199+
# this is an old message that didn't get a messageid, so
200+
# create one
201+
messageid = "<%s.%s.%s%s@%s>"%(time.time(), random.random(),
202+
self.classname, nodeid,
203+
self.db.config.MAIL_DOMAIN)
204+
messages.set(msgid, messageid=messageid)
209205

210206
# send an email to the people who missed out
211207
cn = self.classname
212208
title = self.get(nodeid, 'title') or '%s message copy'%cn
213209

214-
authid, authname, authaddr = None, '', ''
215-
if msgid:
216-
authid = messages.get(msgid, 'author')
217-
authname = users.get(authid, 'realname')
218-
if not authname:
219-
authname = users.get(authid, 'username')
220-
authaddr = users.get(authid, 'address')
221-
if authaddr:
222-
authaddr = " <%s>" % straddr( ('',authaddr) )
223-
else:
224-
authaddr = ''
210+
authid = messages.safeget(msgid, 'author')
211+
authname = users.safeget(authid, 'realname')
212+
if not authname:
213+
authname = users.safeget(authid, 'username', '')
214+
authaddr = users.safeget(authid, 'address', '')
215+
if authaddr:
216+
authaddr = " <%s>" % straddr( ('',authaddr) )
225217

226218
# make the message body
227219
m = ['']
@@ -241,8 +233,7 @@ def send_message(self, nodeid, msgid, note, sendto, from_address=None):
241233
m.append('')
242234

243235
# add the content
244-
if msgid:
245-
m.append(messages.get(msgid, 'content'))
236+
m.append(messages.safeget(msgid, 'content', ''))
246237

247238
# add the change note
248239
if note:

test/db_test_base.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: db_test_base.py,v 1.8 2003-11-14 00:11:19 richard Exp $
18+
# $Id: db_test_base.py,v 1.9 2003-11-16 19:59:06 jlgijsbers Exp $
1919

2020
import unittest, os, shutil, errno, imp, sys, time, pprint
2121

@@ -798,6 +798,18 @@ def testImportExport(self):
798798
ae(self.db.user.get('3', 'username'), 'blop')
799799
ae(self.db.issue.get('2', 'title'), 'issue two')
800800

801+
def testSafeGet(self):
802+
# existent nodeid, existent property
803+
self.assertEqual(self.db.user.safeget('1', 'username'), 'admin')
804+
# existent nodeid, nonexistent property
805+
self.assertEqual(self.db.user.safeget('1', 'nonexistent'), None)
806+
# nonexistent nodeid, existent property
807+
self.assertEqual(self.db.user.safeget('999', 'username'), None)
808+
# nonexistent nodeid, nonexistent property
809+
self.assertEqual(self.db.user.safeget('999', 'nonexistent'), None)
810+
# different default
811+
self.assertEqual(self.db.issue.safeget('999', 'nosy', []), [])
812+
801813
class ROTest(MyTestCase):
802814
def setUp(self):
803815
# remove previous test, ignore errors

0 commit comments

Comments
 (0)