vendor/Five/1.2b-r20590

view sizeconfigure.py @ 0:3673ed425f80

Vendor import of Five 1.2b+ (r20590)
author fguillaume
date Fri, 02 Dec 2005 20:25:42 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2004, 2005 Zope Corporation and Contributors.
4 # All Rights Reserved.
5 #
6 # This software is subject to the provisions of the Zope Public License,
7 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11 # FOR A PARTICULAR PURPOSE.
12 #
13 ##############################################################################
14 """Use structured monkey-patching to enable ``ISized`` adapters for
15 Zope 2 objects.
17 $Id: sizeconfigure.py 14449 2005-07-09 21:12:22Z philikon $
18 """
19 from zope.app.size.interfaces import ISized
20 from Products.Five.fiveconfigure import isFiveMethod
22 # holds classes that were monkeyed with; for clean up
23 _monkied = []
25 def get_size(self):
26 size = ISized(self, None)
27 if size is not None:
28 unit, amount = size.sizeForSorting()
29 if unit == 'byte':
30 return amount
31 method = getattr(self, '__five_original_get_size', None)
32 if method is not None:
33 return self.__five_original_get_size()
35 get_size.__five_method__ = True
37 def classSizable(class_):
38 """Monkey the class to be sizable through Five"""
39 # tuck away the original method if necessary
40 if hasattr(class_, "get_size") and not isFiveMethod(class_.get_size):
41 class_.__five_original_get_size = class_.get_size
42 class_.get_size = get_size
43 # remember class for clean up
44 _monkied.append(class_)
46 def sizable(_context, class_):
47 _context.action(
48 discriminator = ('five:sizable', class_),
49 callable = classSizable,
50 args=(class_,)
51 )
53 # clean up code
54 from Products.Five.fiveconfigure import killMonkey
55 from zope.testing.cleanup import addCleanUp
57 def unsizable(class_):
58 """Restore class's initial state with respect to being sizable"""
59 killMonkey(class_, 'get_size', '__five_original_get_size')
61 def cleanUp():
62 for class_ in _monkied:
63 unsizable(class_)
65 addCleanUp(cleanUp)
66 del addCleanUp