Changeset 9689 for main/waeup.kofa/trunk/src/waeup/kofa
- Timestamp:
- 20 Nov 2012, 00:09:00 (12 years ago)
- Location:
- main/waeup.kofa/trunk/src/waeup/kofa/utils
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/utils/helpers.py
r9593 r9689 384 384 return html 385 385 386 def attrs_to_fields(cls ):386 def attrs_to_fields(cls, omit=[]): 387 387 """Turn the attributes of a class into FieldProperty instances. 388 388 389 389 With Python >= 2.6 we can even use this function as a class decorator. 390 391 `omit` is a list of field names that should _not_ be turned into 392 field properties. This is useful for properties and the like. 390 393 """ 391 394 iface = list(implementedBy(cls))[0] 392 395 for field_name in getFieldNames(iface): 396 if field_name in omit: 397 continue 393 398 setattr(cls, field_name, FieldProperty(iface[field_name])) 394 399 return cls -
main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_helpers.py
r9372 r9689 28 28 from cStringIO import StringIO 29 29 from zope import schema 30 from zope.interface import Interface, Attribute, implements 30 from zope.interface import Interface, Attribute, implements, implementer 31 31 from zope.security.testing import Principal, Participation 32 32 from zope.security.management import newInteraction, endInteraction … … 52 52 self.assertEqual(result4, 30) 53 53 return 54 55 def test_attrs_to_fields_properties(self): 56 # we can omit single fields in order to retrieve properties 57 class IMyInterface(Interface): 58 attr1 = schema.Int( 59 title = u'Attribute 1', readonly = True, default = 110, 60 ) 61 62 @helpers.attrs_to_fields 63 @implementer(IMyInterface) 64 class MyClass1(object): 65 @property 66 def attr1(self): 67 return 42 68 69 @implementer(IMyInterface) 70 class MyClass2(object): 71 @property 72 def attr1(self): 73 return 42 74 MyClass2 = helpers.attrs_to_fields(MyClass2, omit=['attr1']) 75 76 obj1 = MyClass1() 77 obj2 = MyClass2() 78 79 self.assertEqual(obj1.attr1, 110) 80 self.assertEqual(obj2.attr1, 42) 81 return 82 54 83 55 84 class RemoveFileOrDirectoryTestCase(unittest.TestCase): … … 461 490 contents, '\r\n') 462 491 return 492 463 493 464 494
Note: See TracChangeset for help on using the changeset viewer.