Merge lp:~statik/graphite/cleanup-assertions into lp:~graphite-dev/graphite/main

Proposed by Elliot Murphy
Status: Merged
Approved by: chrismd
Approved revision: not available
Merged at revision: not available
Proposed branch: lp:~statik/graphite/cleanup-assertions
Merge into: lp:~graphite-dev/graphite/main
Diff against target: 85 lines (+37/-10)
1 file modified
whisper/whisper.py (+37/-10)
To merge this branch: bzr merge lp:~statik/graphite/cleanup-assertions
Reviewer Review Type Date Requested Status
chrismd Approve
Review via email: mp+18780@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Elliot Murphy (statik) wrote :

Hi! Here is my initial attempt at converting from asserts to exceptions.

I left one assert that really seemed to be about catching a should-never-happen logic error, the rest I converted to a series of exceptions. Let me know what you think, I'm happy to make any changes.

243. By Elliot Murphy

Fix a bunch of logig that I inverted when converting from asserts.

Revision history for this message
Elliot Murphy (statik) wrote :

Oops, how embarrassing. I inverted some of the logic when converting from asserts. Fixed and pushed, I believe the diff will be updated on the launchpad page but of course the diff that was emailed out is still wrong.

Revision history for this message
chrismd (chrismd) wrote :

Looks good to me, merging into trunk.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'whisper/whisper.py'
2--- whisper/whisper.py 2009-11-02 06:47:51 +0000
3+++ whisper/whisper.py 2010-02-07 18:35:23 +0000
4@@ -53,6 +53,23 @@
5
6 debug = startBlock = endBlock = lambda *a,**k: None
7
8+
9+class WhisperException(Exception):
10+ """Base class for whisper exceptions."""
11+
12+
13+class InvalidConfiguration(WhisperException):
14+ """Invalid configuration."""
15+
16+
17+class InvalidTimeInterval(WhisperException):
18+ """Invalid time interval."""
19+
20+
21+class TimestampNotCovered(WhisperException):
22+ """Timestamp not covered by any archives in this database."""
23+
24+
25 def enableDebug():
26 global open, debug, startBlock, endBlock
27 class open(file):
28@@ -136,21 +153,28 @@
29 xFilesFactor specifies the fraction of data points in a propagation interval that must have known values for a propagation to occur
30 """
31 #Validate archive configurations...
32- assert archiveList, "You must specify at least one archive configuration!"
33+ if not archiveList:
34+ raise InvalidConfiguration("You must specify at least one archive configuration!")
35 archiveList.sort(key=lambda a: a[0]) #sort by precision (secondsPerPoint)
36 for i,archive in enumerate(archiveList):
37 if i == len(archiveList) - 1: break
38 next = archiveList[i+1]
39- assert archive[0] < next[0],\
40- "You cannot configure two archives with the same precision %s,%s" % (archive,next)
41- assert (next[0] % archive[0]) == 0,\
42- "Higher precision archives' precision must evenly divide all lower precision archives' precision %s,%s" % (archive[0],next[0])
43+ if not (archive[0] < next[0]):
44+ raise InvalidConfiguration("You cannot configure two archives "
45+ "with the same precision %s,%s" % (archive,next))
46+ if (next[0] % archive[0]) != 0:
47+ raise InvalidConfiguration("Higher precision archives' precision "
48+ "must evenly divide all lower precision archives' precision %s,%s" \
49+ % (archive[0],next[0]))
50 retention = archive[0] * archive[1]
51 nextRetention = next[0] * next[1]
52- assert nextRetention > retention,\
53- "Lower precision archives must cover larger time intervals than higher precision archives %s,%s" % (archive,next)
54+ if not (nextRetention > retention):
55+ raise InvalidConfiguration("Lower precision archives must cover "
56+ "larger time intervals than higher precision archives %s,%s" \
57+ % (archive,next))
58 #Looks good, now we create the file and write the header
59- assert not os.path.exists(path), "File %s already exists!" % path
60+ if os.path.exists(path):
61+ raise InvalidConfiguration("File %s already exists!" % path)
62 fh = open(path,'wb')
63 if LOCK: fcntl.flock( fh.fileno(), fcntl.LOCK_EX )
64 lastUpdate = struct.pack( timestampFormat, int(time.time()) )
65@@ -251,7 +275,9 @@
66 if timestamp is None: timestamp = now
67 timestamp = int(timestamp)
68 diff = now - timestamp
69- assert diff < header['maxRetention'] and diff >= 0, "Timestamp not covered by any archives in this database"
70+ if not ((diff < header['maxRetention']) and diff >= 0):
71+ raise TimestampNotCovered("Timestamp not covered by any archives in "
72+ "this database.")
73 for i,archive in enumerate(header['archives']): #Find the highest-precision archive that covers timestamp
74 if archive['retention'] < diff: continue
75 lowerArchives = header['archives'][i+1:] #We'll pass on the update to these lower precision archives later
76@@ -445,7 +471,8 @@
77 if fromTime < oldestTime:
78 fromTime = oldestTime
79
80- assert fromTime < untilTime, "Invalid time interval"
81+ if not (fromTime < untilTime):
82+ raise InvalidTimeInterval("Invalid time interval")
83 if untilTime > now:
84 untilTime = now
85 if untilTime < fromTime: