Changeset 115:20f6691fd32f
- Timestamp:
- 02/21/10 17:57:49 (7 months ago)
- Author:
- Roger Gammans <rgammans@…>
- Branch:
- default
- Message:
-
Generalise reference attribute type.
- Update FindBestType? to search through bases classes for a match too.
- Improve attribute test.
- Files:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r104
|
r115
|
|
| 82 | 82 | """ |
| 83 | 83 | """ |
| 84 | | #Handle base class and more sets in the key. |
| 85 | | return (sorted(TypeLookup[atype],key=operator.itemgetter(1),reverse= True ))[0][0] |
| | 84 | #Handle base class and more sets in the key get a list |
| | 85 | # of classes to use for candidate handlers. |
| | 86 | candidates = list() |
| | 87 | try: |
| | 88 | typelist = atype.__mro__ |
| | 89 | except: |
| | 90 | typelist = list(atype) |
| | 91 | |
| | 92 | #Collect the handlers for the functions. |
| | 93 | for t in typelist: |
| | 94 | if t in TypeLookup: |
| | 95 | candidates += TypeLookup[t] |
| | 96 | |
| | 97 | modlogger.debug("candiate attrval's %s" % candidates) |
| | 98 | rlist = sorted(candidates,key=operator.itemgetter(1), |
| | 99 | reverse= True ) |
| | 100 | return rlist[0][0] |
| 86 | 101 | |
| 87 | 102 | |
| … |
… |
|
| 144 | 159 | return str(self.__class__)+"(\""+self.get_raw()+"\")" |
| 145 | 160 | |
| | 161 | |
| | 162 | def __str__(self): |
| | 163 | return self.get_raw() |
| | 164 | |
| 146 | 165 | def get_raw(self, obj = None): |
| 147 | 166 | """ |
-
|
r104
|
r115
|
|
| 220 | 220 | |
| 221 | 221 | |
| 222 | | class MMAttributeValue_MMObjectRef(MMAttributeValue): |
| 223 | | """ |
| 224 | | """ |
| 225 | | typename = "objectref" |
| 226 | | contain_prefs = { MMObject: 100 } |
| | 222 | class MMAttributeValue_MMRef(MMAttributeValue): |
| | 223 | """ |
| | 224 | """ |
| | 225 | typename = "ref" |
| | 226 | contain_prefs = { MMBase: 50 } |
| 227 | 227 | |
| 228 | 228 | def __init__(self,*args,**kwargs): |
| 229 | | super(MMAttributeValue_MMObjectRef, self).__init__(*args,**kwargs) |
| 230 | | if isinstance(self.value,MMObject): |
| | 229 | super(MMAttributeValue_MMRef, self).__init__(*args,**kwargs) |
| | 230 | if isinstance(self.value,MMBase): |
| 231 | 231 | #Get string represenation of the object. |
| 232 | 232 | #TODO Store object here so it cached. |
| 233 | 233 | self.parts['obj'] = repr(self.value) |
| 234 | | |
| | 234 | |
| 235 | 235 | if not self._validate(): raise Error() |
| 236 | 236 | #All ok. |
-
|
r53
|
r115
|
|
| 32 | 32 | return self.x |
| 33 | 33 | |
| | 34 | class fakeParent: |
| | 35 | def __init__(self): |
| | 36 | self.updated = False |
| | 37 | def Updated(self): |
| | 38 | return self.updated |
| | 39 | |
| | 40 | def __setitem__(self,name,val): |
| | 41 | self.updated = True |
| | 42 | def resetUpdate(self): |
| | 43 | self.updated = False |
| 34 | 44 | class attribTest(unittest.TestCase): |
| 35 | 45 | |
| … |
… |
|
| 42 | 52 | # Test MMObject fetching |
| 43 | 53 | # Test handling of parse errors. |
| | 54 | # Test AttributeValue export resolution. |
| | 55 | def testAttrValExport(self): |
| | 56 | attr=MMAttribute("document","test\n----\n\n\nA Message",None) |
| | 57 | self.assertEquals("test\n----\n\n\nA Message",attr.get_raw()) |
| | 58 | |
| | 59 | def testAttrParentStuff(self): |
| | 60 | p = fakeParent() |
| | 61 | attr=MMAttribute("document","test\n----\n\n\nA Message",p) |
| | 62 | self.assertTrue(p is attr.get_owner()) |
| | 63 | v = attr.get_value() |
| | 64 | |
| | 65 | attr.set_value("diff") |
| | 66 | self.assertTrue(p.Updated()) |
| | 67 | v1 = attr.get_value() |
| | 68 | self.assertEquals("diff",str(v1)) |
| | 69 | |
| 44 | 70 | def getTestNames(): |
| 45 | | return [ 'attribTest.attribTest' ] |
| | 71 | return [ 'attribTest.attribTest' ] |
| 46 | 72 | |
| 47 | 73 | if __name__ == '__main__': |
-
|
r96
|
r115
|
|
| 76 | 76 | self.assertEqual(self.objrefB.get_object(self.objA) , self.objB ) |
| 77 | 77 | |
| 78 | | |
| 79 | 78 | def makeRef(self): |
| 80 | | self.objrefA = MMAttributeValue_MMObjectRef( value = self.objA ) |
| 81 | | self.objrefB = MMAttributeValue_MMObjectRef( value = self.objB ) |
| | 79 | self.objrefA = MMAttributeValue_MMRef( value = self.objA ) |
| | 80 | self.objrefB = MMAttributeValue_MMRef( value = self.objB ) |
| 82 | 81 | |
| 83 | 82 | |