def testMakeAncestor1(self): cls = MixIn(self.pyClass, self.mixIn1, makeAncestor=True) self.assertEqual(cls.__bases__, (self.mixIn1, self.base)) self.assertFalse(hasattr(cls, 'mixInsForPyClass')) self.assertFalse(hasattr(self.mixIn1, 'mixInSuperFoo')) self.assertEqual(cls.attr0, 'this is Base.attr0') self.assertEqual(cls.attr1, 'this is PyClass.attr1') self.assertEqual(cls.attr2, 'this is PyClass.attr2') self.assertEqual(cls.attr3, 'this is PyClass.attr3') self.assertEqual(cls.attr4, 'this is MixIn1.attr4') self.assertEqual(cls.cFoo(), 'this is PyClass.cFoo') self.assertEqual(cls.sFoo(), 'this is PyClass.sFoo') obj = cls() self.assertEqual(obj.foo(), 'this is PyClass.foo')
def testMakeAncestor2(self): cls = MixIn(self.pyClass, self.mixIn2, makeAncestor=True) self.assertEqual(cls.__bases__, (self.mixIn2, self.base)) self.assertFalse(hasattr(cls, 'mixInsForPyClass')) self.assertFalse(hasattr(self.mixIn2, 'mixInSuperFoo')) self.assertEqual(cls.attr0, 'this is Base.attr0') self.assertEqual(cls.attr1, 'this is PyClass.attr1') self.assertEqual(cls.attr2, 'this is PyClass.attr2') self.assertEqual(cls.attr3, 'this is PyClass.attr3') self.assertEqual(cls.attr5, 'this is MixIn2.attr5') self.assertEqual(cls.attr6, 'this is MixIn2.attr6') self.assertEqual(cls.cFoo(), 'this is PyClass.cFoo') self.assertEqual(cls.sFoo(), 'this is PyClass.sFoo') obj = cls() self.assertEqual(obj.foo(), 'this is PyClass.foo') self.assertEqual(obj.bar(), 'this is MixIn2.bar') self.assertEqual(obj.baz(), 'this is MixIn2Base.baz')
def testMixIn1And2(self): cls = self.pyClass MixIn(cls, self.mixIn1) MixIn(cls, self.mixIn2) self.assertEqual(cls.__bases__, (self.base, )) self.assertEqual(cls.mixInsForPyClass, [self.mixIn1, self.mixIn2Base, self.mixIn2]) self.assertFalse(hasattr(self.mixIn1, 'mixInSuperFoo')) self.assertFalse(hasattr(self.mixIn2, 'mixInSuperFoo')) self.assertEqual(cls.attr0, 'this is Base.attr0') self.assertEqual(cls.attr1, 'this is PyClass.attr1') self.assertEqual(cls.attr2, 'this is PyClass.attr2') self.assertEqual(cls.attr3, 'this is MixIn1.attr3') self.assertEqual(cls.attr4, 'this is MixIn1.attr4') self.assertEqual(cls.attr5, 'this is MixIn2.attr5') self.assertEqual(cls.attr6, 'this is MixIn2.attr6') self.assertEqual(cls.cFoo(), 'this is MixIn1.cFoo') self.assertEqual(cls.sFoo(), 'this is MixIn1.sFoo') obj = cls() self.assertEqual(obj.foo(), 'this is MixIn1.foo') self.assertEqual(obj.bar(), 'this is MixIn2.bar') self.assertEqual(obj.baz(), 'this is MixIn2Base.baz')
def testMixIn1(self): cls = self.pyClass MixIn(cls, self.mixIn1) self.assertEqual(cls.__bases__, (self.base, )) self.assertEqual(cls.mixInsForPyClass, [self.mixIn1]) self.assertFalse(hasattr(self.mixIn1, 'mixInSuperFoo')) self.assertEqual(cls.attr0, 'this is Base.attr0') self.assertEqual(cls.attr1, 'this is PyClass.attr1') self.assertEqual(cls.attr2, 'this is PyClass.attr2') self.assertEqual(cls.attr3, 'this is MixIn1.attr3') self.assertEqual(cls.attr4, 'this is MixIn1.attr4') self.assertEqual(cls.cFoo(), 'this is MixIn1.cFoo') self.assertEqual(cls.sFoo(), 'this is MixIn1.sFoo') obj = cls() self.assertEqual(obj.foo(), 'this is MixIn1.foo')
def installMixInsForModule(self, module, verbose=0): # @@ 2000-10-18 ce: perhaps MixIns should be applied to the actual MiddleKit.Core class and not the custom one that possibly was passed into model. This would help with "invoking super" which may be a non-trivial operation in a mix-in of a generator module. coreClassNames = self._model.coreClassNames() if verbose: print '>>', module for name in dir(module): generatorThing = getattr(module, name) if type(generatorThing) is ClassType: # See if a class with the same name exists in MiddleKit.Core import MiddleKit.Core as Core if name in coreClassNames: baseClass = self._model.coreClass(name) if baseClass is not generatorThing: if verbose: print '>> mixing %s into %s' % (generatorThing, baseClass) assert type(baseClass) is ClassType assert type(generatorThing) is ClassType MixIn(baseClass, generatorThing)
def testMixIn2WithSuper(self): cls, mixIn = self.pyClass, self.mixIn2 MixIn(cls, mixIn, mixInSuperMethods=True) self.assertEqual(cls.__bases__, (self.base, )) self.assertEqual(cls.mixInsForPyClass, [self.mixIn2Base, mixIn]) self.assertFalse(hasattr(mixIn, 'mixInSuperFoo')) self.assertFalse(hasattr(mixIn, 'mixInSuperCFoo')) self.assertFalse(hasattr(mixIn, 'mixInSuperSFoo')) self.assertEqual(cls.attr0, 'this is Base.attr0') self.assertEqual(cls.attr1, 'this is PyClass.attr1') self.assertEqual(cls.attr2, 'this is PyClass.attr2') self.assertEqual(cls.attr3, 'this is PyClass.attr3') self.assertEqual(cls.attr5, 'this is MixIn2.attr5') self.assertEqual(cls.attr6, 'this is MixIn2.attr6') self.assertEqual(cls.cFoo(), 'this is PyClass.cFoo') self.assertEqual(cls.sFoo(), 'this is PyClass.sFoo') obj = cls() self.assertEqual(obj.foo(), 'this is PyClass.foo') self.assertEqual(obj.bar(), 'this is MixIn2.bar') self.assertEqual(obj.baz(), 'this is MixIn2Base.baz')
def testMixIn1WithSuper(self): cls, mixIn = self.pyClass, self.mixIn1 MixIn(cls, mixIn, mixInSuperMethods=True) self.assertEqual(cls.__bases__, (self.base, )) self.assertEqual(cls.mixInsForPyClass, [mixIn]) self.assertTrue(hasattr(mixIn, 'mixInSuperFoo')) self.assertTrue(hasattr(mixIn, 'mixInSuperCFoo')) self.assertTrue(hasattr(mixIn, 'mixInSuperSFoo')) self.assertEqual(cls.attr0, 'this is Base.attr0') self.assertEqual(cls.attr1, 'this is PyClass.attr1') self.assertEqual(cls.attr2, 'this is PyClass.attr2') self.assertEqual(cls.attr3, 'this is MixIn1.attr3') self.assertEqual(cls.attr4, 'this is MixIn1.attr4') self.assertEqual(cls.cFoo(), 'this is MixIn1.cFoo') self.assertEqual(cls.sFoo(), 'this is MixIn1.sFoo') self.assertEqual(mixIn.mixInSuperCFoo(), 'this is PyClass.cFoo') self.assertEqual(mixIn.mixInSuperSFoo(), 'this is PyClass.sFoo') obj = cls() self.assertEqual(obj.foo(), 'this is MixIn1.foo') superFoo = mixIn.mixInSuperFoo self.assertEqual(superFoo(self), 'this is PyClass.foo')
def InstallMixIns(): from MiscUtils.MixIn import MixIn theGlobals = globals() names = 'ObjectStore Klass MiddleObject Attr ObjRefAttr ListAttr'.split() places = 'Core Run'.split() for name in names: mixed = False for place in places: nameSpace = {} try: exec 'from MiddleKit.%s.%s import %s' % ( place, name, name) in nameSpace except ImportError: pass else: pyClass = nameSpace[name] mixIn = theGlobals[name] MixIn(pyClass, mixIn) mixed = True continue assert mixed, 'Could not mix-in %s.' % name
class KlassMixIn(object): def sqlSerialColumnName(self): name = getattr(self, '_sqlIdColumnName', None) if name is None: _ClassName = self.name() ClassName = _ClassName[0].upper() + _ClassName[1:] className = _ClassName[0].lower() + _ClassName[1:] names = locals() name = self.klasses().model().setting('SQLSerialColumnName', 'serialNum') % names self._sqlIdColumnName = name return name from MiscUtils.MixIn import MixIn from MiddleKit.Core.Klass import Klass MixIn(Klass, KlassMixIn)
"'%s' is not a valid default for boolean column '%s'" % (value, self.name())) return value class DateTimeAttr(object): def sqlType(self): # In accord with newer PostgreSQL versions, # we use timestamps *without* time zone by default. # This also makes DateTime more compatible with the MySQL variant. # It is recommended to simply store all DateTimes as UTC. return 'timestamp' class ObjRefAttr(object): def sqlType(self): return 'bigint /* %s */' % self['Type'] class PrimaryKey(object): def sampleValue(self, value): # keep track of the highest serial number for each klass, # so that we can update the database sequences if int(value) > self._klass._maxSerialNum: self._klass._maxSerialNum = int(value) return value # PrimaryKey is not a core class, so we have to mix this in manually. MixIn(PrimaryKeyBase, PrimaryKey)
def setUserClass(self, userClass): """Overridden to mix in UserMixIn to the class that is passed in.""" MixIn(userClass, UserMixIn) UserManager.setUserClass(self, userClass)
def setUserClass(self, userClass): """ Overridden to mix in UserMixIn to the class that is passed in. """ from MiscUtils.MixIn import MixIn MixIn(userClass, UserMixIn) self.baseOfUserManagerToFile.setUserClass(self, userClass)
return 'update %s set deleted=Now() where %s=%d;' % ( klass.sqlTableName(), klass.sqlIdName(), self.serialNum()) else: return 'delete from %s where %s=%d;' % ( klass.sqlTableName(), klass.sqlIdName(), self.serialNum()) def referencingObjectsAndAttrsFetchKeywordArgs(self, backObjRefAttr): return { 'refreshAttrs': 1, 'clauses': 'WHERE %s=%s' % (backObjRefAttr.sqlColumnName(), self.sqlObjRef()) } MixIn(MiddleObject, MiddleObjectMixIn) # Normally we don't have to invoke MixIn()--it's done automatically. # However, that only works when augmenting MiddleKit.Core classes # (MiddleObject belongs to MiddleKit.Run). class Klass: _fetchSQLStart = None # help out the caching mechanism in fetchSQLStart() _insertSQLStart = None # help out the caching mechanism in fetchSQLStart() def sqlTableName(self): """ Returns the name of the SQL table for this class. Returns self.name(). Subclasses may wish to override to provide special quoting that