Merge lp:~bac/lazr.lifecycle/snapshot into lp:lazr.lifecycle

Proposed by Brad Crittenden
Status: Merged
Merged at revision: not available
Proposed branch: lp:~bac/lazr.lifecycle/snapshot
Merge into: lp:lazr.lifecycle
Diff against target: 159 lines (+46/-12)
5 files modified
src/lazr/lifecycle/NEWS.txt (+6/-0)
src/lazr/lifecycle/docs/snapshot.txt (+12/-5)
src/lazr/lifecycle/interfaces.py (+9/-0)
src/lazr/lifecycle/snapshot.py (+18/-6)
src/lazr/lifecycle/version.txt (+1/-1)
To merge this branch: bzr merge lp:~bac/lazr.lifecycle/snapshot
Reviewer Review Type Date Requested Status
Gary Poster code Approve
Review via email: mp+15612@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Brad Crittenden (bac) wrote :

Add the IDoNotSnapshot marker interface and add support for ignoring fields that provide it.

When taking snapshots by using the 'providing' argument, the user can choose to selectively ignore some fields by:

alsoProvides(ignored_field, IDoNotSnapshot)

lp:~bac/lazr.lifecycle/snapshot updated
37. By Brad Crittenden

Changes from review: provide doNotSnapshot wrapper method, make version 1.1.

Revision history for this message
Gary Poster (gary) wrote :

I like this.

As we discussed on IRC, I think this should be 1.1, not 1.0.1.

Please update the CHANGES file too.

We also agreed that adding a sugar function called doNotSnapshot would be nice, so that the field in the interface could look like this...

  ignore_me = doNotSnapshot(Attribute('ignored attribute'))

...rather than this.

  ignore_me = Attribute('ignored attribute')
  alsoProvides(ignore_me, IDoNotSnapshot)

The mechanism would stay the same: it's just sugar.

Thank you!

Gary

review: Approve (code)
lp:~bac/lazr.lifecycle/snapshot updated
38. By Brad Crittenden

Updated NEWS and reworded a docstring.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/lazr/lifecycle/NEWS.txt'
2--- src/lazr/lifecycle/NEWS.txt 2009-08-31 14:37:56 +0000
3+++ src/lazr/lifecycle/NEWS.txt 2009-12-03 18:31:09 +0000
4@@ -2,6 +2,12 @@
5 NEWS for lazr.lifecycle
6 =======================
7
8+1.1 (2009-12-03)
9+================
10+
11+- Add IDoNotSnapshot and doNotSnapshot to allow the exclusion of
12+certain fields.
13+
14 1.0 (2009-08-31)
15 ================
16
17
18=== modified file 'src/lazr/lifecycle/docs/snapshot.txt'
19--- src/lazr/lifecycle/docs/snapshot.txt 2009-03-05 17:51:50 +0000
20+++ src/lazr/lifecycle/docs/snapshot.txt 2009-12-03 18:31:09 +0000
21@@ -5,15 +5,17 @@
22 initial state of the object before the modifications. The Snapshot class can
23 be used to easily represent such states:
24
25- >>> from zope.interface import Interface, implements, Attribute
26+ >>> from zope.interface import (
27+ ... alsoProvides, Attribute, Interface, implements)
28 >>> from zope.schema import List, TextLine, Text
29- >>> from lazr.lifecycle.snapshot import Snapshot
30+ >>> from lazr.lifecycle.snapshot import doNotSnapshot, Snapshot
31
32 >>> class IFoo(Interface):
33 ... title = TextLine(title=u'My Title')
34 ... description = Text(title=u'Description')
35 ... remotes = List(title=u'remotes')
36 ... totals = Attribute('totals')
37+ ... ignore_me = doNotSnapshot(Attribute('ignored attribute'))
38
39 >>> class Foo:
40 ... implements(IFoo)
41@@ -64,7 +66,7 @@
42 ...
43 SnapshotCreationError: ...
44
45-If no names argument is supplied, the snapshot will contain all
46+If no names argument is supplied, the snapshot will contain all
47 IFields of the specified interface(s).
48
49 >>> snapshot = Snapshot(foo, providing=IFoo)
50@@ -73,7 +75,7 @@
51 >>> hasattr(snapshot, 'description')
52 True
53
54-(Totals is not a Fields so isn't copied over).
55+Totals is not a Fields so isn't copied over.
56
57 >>> hasattr(snapshot, 'totals')
58 False
59@@ -82,6 +84,12 @@
60 >>> snapshot.remotes == ["OK"]
61 True
62
63+Fields can be explicitly ignored if they provide the IDoNotSnapshot
64+interface.
65+
66+ >>> hasattr(snapshot, 'ignore_me')
67+ False
68+
69 We can also give more than one interface to provide as an iterable. If
70 we don't specify any names, all the names in the given interfaces will
71 be copied:
72@@ -166,4 +174,3 @@
73
74 >>> getSiteManager().unregisterAdapter(snapshot_iterable)
75 True
76-
77
78=== modified file 'src/lazr/lifecycle/interfaces.py'
79--- src/lazr/lifecycle/interfaces.py 2009-03-24 16:17:11 +0000
80+++ src/lazr/lifecycle/interfaces.py 2009-12-03 18:31:09 +0000
81@@ -18,6 +18,7 @@
82
83 __metaclass__ = type
84 __all__ = [
85+ 'IDoNotSnapshot',
86 'IObjectCreatedEvent',
87 'IObjectDeletedEvent',
88 'IObjectModifiedEvent',
89@@ -57,3 +58,11 @@
90 lookup. The snapshot value is what should be returned from the adapter
91 lookup.
92 """
93+
94+
95+class IDoNotSnapshot(Interface):
96+ """Marker interface for attributes to exclude from the snapshot.
97+
98+ Some attributes such as collections should not be included as part of the
99+ snapshot as they may be huge and not intrinsic to the object.
100+ """
101
102=== modified file 'src/lazr/lifecycle/snapshot.py'
103--- src/lazr/lifecycle/snapshot.py 2009-03-24 16:17:11 +0000
104+++ src/lazr/lifecycle/snapshot.py 2009-12-03 18:31:09 +0000
105@@ -21,21 +21,32 @@
106
107 __metaclass__ = type
108
109-__all__ = ['SnapshotCreationError',
110- 'Snapshot',
111- ]
112+__all__ = [
113+ 'doNotSnapshot',
114+ 'SnapshotCreationError',
115+ 'Snapshot',
116+ ]
117
118 from zope.component import queryAdapter
119 from zope.interface.interfaces import IInterface
120-from zope.interface import directlyProvides
121+from zope.interface import alsoProvides, directlyProvides
122 from zope.schema.interfaces import IField
123+from zope.schema import Field
124 from zope.security.proxy import removeSecurityProxy
125
126-from lazr.lifecycle.interfaces import ISnapshotValueFactory
127+from lazr.lifecycle.interfaces import (
128+ IDoNotSnapshot, ISnapshotValueFactory)
129+
130
131 _marker = object()
132
133
134+def doNotSnapshot(field):
135+ """Simple wrapper method to provide `IDoNotSnapshot`."""
136+ alsoProvides(field, IDoNotSnapshot)
137+ return field
138+
139+
140 class SnapshotCreationError(Exception):
141 """Something went wrong while creating a snapshot."""
142
143@@ -66,7 +77,8 @@
144 for iface in providing:
145 for name in iface.names(all=True):
146 field = iface[name]
147- if IField.providedBy(field):
148+ if (IField.providedBy(field) and
149+ not IDoNotSnapshot.providedBy(field)):
150 names.add(name)
151
152 for name in names:
153
154=== modified file 'src/lazr/lifecycle/version.txt'
155--- src/lazr/lifecycle/version.txt 2009-08-29 16:09:41 +0000
156+++ src/lazr/lifecycle/version.txt 2009-12-03 18:31:09 +0000
157@@ -1,1 +1,1 @@
158-1.0
159+1.1

Subscribers

People subscribed via source and target branches