Merge lp:~lifeless/python-oops/pprint into lp:python-oops

Proposed by Robert Collins
Status: Merged
Approved by: James Westby
Approved revision: 36
Merged at revision: 35
Proposed branch: lp:~lifeless/python-oops/pprint
Merge into: lp:python-oops
Diff against target: 167 lines (+81/-1)
7 files modified
.testr.conf (+4/-0)
Makefile (+13/-0)
NEWS (+4/-0)
README (+7/-0)
oops/__init__.py (+2/-0)
oops/publishers.py (+22/-1)
oops/tests/test_publishers.py (+29/-0)
To merge this branch: bzr merge lp:~lifeless/python-oops/pprint
Reviewer Review Type Date Requested Status
James Westby (community) Approve
Review via email: mp+119069@code.launchpad.net

Description of the change

Make it easy to show oopses on the console. Also add missing build infrastructure like .testr.conf.

To post a comment you must log in.
Revision history for this message
James Westby (james-w) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file '.testr.conf'
2--- .testr.conf 1970-01-01 00:00:00 +0000
3+++ .testr.conf 2012-08-10 01:06:20 +0000
4@@ -0,0 +1,4 @@
5+[DEFAULT]
6+test_command=PYTHONPATH=. bin/py -m subunit.run $LISTOPT $IDOPTION oops.tests.test_suite
7+test_id_option=--load-list $IDFILE
8+test_list_option=--list
9
10=== added file 'Makefile'
11--- Makefile 1970-01-01 00:00:00 +0000
12+++ Makefile 2012-08-10 01:06:20 +0000
13@@ -0,0 +1,13 @@
14+all:
15+
16+bin/buildout: buildout.cfg versions.cfg setup.py download-cache eggs
17+ ./bootstrap.py \
18+ --setup-source=download-cache/ez_setup.py \
19+ --download-base=download-cache/dist --eggs=eggs
20+
21+
22+download-cache:
23+ bzr checkout --lightweight lp:lp-source-dependencies download-cache
24+
25+eggs:
26+ mkdir eggs
27
28=== modified file 'NEWS'
29--- NEWS 2012-06-26 21:23:59 +0000
30+++ NEWS 2012-08-10 01:06:20 +0000
31@@ -6,6 +6,10 @@
32 NEXT
33 ----
34
35+* New publishers pprint_to_stream. Very useful for interactive observing of
36+ Oops reports - just hook it up to your source (e.g. via oops_amqp) and
37+ off you go. (Robert Collins)
38+
39 0.0.11
40 ------
41
42
43=== modified file 'README'
44--- README 2012-06-26 21:28:11 +0000
45+++ README 2012-08-10 01:06:20 +0000
46@@ -107,6 +107,9 @@
47 >>> report['id']
48 'id 1'
49
50+* The pprint_to_stream publisher will print out reports to a stream after
51+pprinting them. This can be very helpful for interactive use.
52+
53 * The related project oops_datedir_repo contains a local disk based repository which
54 can be used as a publisher.
55
56@@ -133,3 +136,7 @@
57 For instance::
58
59 $ bin/py -m testtools.run oops.tests.test_suite
60+
61+Alternative you can use the testr command from testrepository::
62+
63+ $ testr run
64
65=== modified file 'oops/__init__.py'
66--- oops/__init__.py 2012-06-26 21:29:11 +0000
67+++ oops/__init__.py 2012-08-10 01:06:20 +0000
68@@ -30,6 +30,7 @@
69 __all__ = [
70 'Config',
71 'convert_result_to_list',
72+ 'pprint_to_stream',
73 'publish_new_only',
74 'publish_to_many',
75 'publish_with_fallback',
76@@ -38,6 +39,7 @@
77 from oops.config import Config
78 from oops.publishers import (
79 convert_result_to_list,
80+ pprint_to_stream,
81 publish_new_only,
82 publish_to_many,
83 publish_with_fallback,
84
85=== modified file 'oops/publishers.py'
86--- oops/publishers.py 2012-06-26 20:57:41 +0000
87+++ oops/publishers.py 2012-08-10 01:06:20 +0000
88@@ -18,9 +18,30 @@
89 __metaclass__ = type
90
91 __all__ = [
92- 'publish_new_only',
93+ 'pprint_to_stream',
94+ 'publish_with_fallback',
95+ 'publish_to_many',
96 ]
97
98+from hashlib import md5
99+from pprint import pformat
100+
101+
102+def pprint_to_stream(stream):
103+ """Pretty print reports to stream.
104+
105+ Reports will be given an id by hashing the report if none is present.
106+ """
107+ def pprinter(report):
108+ report = dict(report)
109+ output = pformat(report)
110+ if not report.get('id'):
111+ report['id'] = md5(output).hexdigest()
112+ output = pformat(report)
113+ stream.write(output)
114+ return [report['id']]
115+ return pprinter
116+
117
118 def publish_new_only(publisher):
119 """Wraps a publisher with a check that the report has not had an id set.
120
121=== modified file 'oops/tests/test_publishers.py'
122--- oops/tests/test_publishers.py 2012-06-26 20:52:24 +0000
123+++ oops/tests/test_publishers.py 2012-08-10 01:06:20 +0000
124@@ -17,10 +17,15 @@
125
126 __metaclass__ = type
127
128+from hashlib import md5
129+from pprint import pformat
130+from StringIO import StringIO
131+
132 import testtools
133
134 from oops import (
135 convert_result_to_list,
136+ pprint_to_stream,
137 publish_new_only,
138 publish_to_many,
139 publish_with_fallback,
140@@ -168,3 +173,27 @@
141 def trueish(report):
142 return "aaa"
143 self.assertEqual(["aaa"], convert_result_to_list(trueish)({}))
144+
145+
146+class TestPPrintToStream(testtools.TestCase):
147+
148+ def test_inherits_id_when_set(self):
149+ output = StringIO()
150+ publisher = pprint_to_stream(output)
151+ published = publisher({'foo': 'bar', 'id': 'quux'})
152+ self.assertEqual(['quux'], published)
153+
154+ def test_returns_pprint_hash(self):
155+ output = StringIO()
156+ publisher = pprint_to_stream(output)
157+ published = publisher({'foo': 'bar'})
158+ pprint_value = pformat({'foo': 'bar'})
159+ self.assertEqual([md5(pprint_value).hexdigest()], published)
160+
161+ def test_outputs_pprint(self):
162+ output = StringIO()
163+ publisher = pprint_to_stream(output)
164+ publisher({'foo': 'bar'})
165+ self.assertEqual(
166+ "{'foo': 'bar', 'id': 'dd63dafcbd4d5b28badfcaf86fb6fcdb'}",
167+ output.getvalue())

Subscribers

People subscribed via source and target branches

to all changes: