class _ObjKeyStore(object): def __init__(self, name="KeyStore"): object.__init__(self) self._store = CfgKeyStore(name) for label in CfgKeyStore.__slots__['Labels']: labelName = "%s%s" % ( str(label)[0].upper(), # capitalize first letter str(label)[1:]) self.__dict__['isIn%s' % labelName] = self.__isInDict(label) self.__dict__['add%s' % labelName] = self.__addToDict(label) self.__dict__['addManyTypes%s' % labelName] = self.__addDictToDict(label) self.__dict__['write%s' % labelName] = self.__writePy(label) self.__dict__['read%s' % labelName] = self.__readPy(label) return # def __isInDict(self, label): # fct = lambda key: self.__all[label].has_key(key) # class __helper: # def __call__(self,key): # return fct(key) # return __helper() def __getitem__(self, k): return self._store[k] def __setitem__(self, k, v): return self._store.__setitem__(k, v) def keys(self): return self._store.keys() def clear(self, label=None): return self._store.clear(label) def __isInDict(self, label): def isIn(d, k, v): return d.has_item("%s#%s" % (k, v)) fct = lambda key, value: isIn(self._store[label], key, value) class __helper: def __call__(self, key, value): return fct(key, value) return __helper() def __addToDict(self, label): def fill(d, k, v): d.add({k: v}) return fct = lambda key, value: fill(self._store[label], key, value) class __helper: def __call__(self, key, value): return fct(key, value) return __helper() def __addDictToDict(self, label): # merge the dictionaries def fill(d, thedict): d.add(thedict) return fct = lambda thedict: fill(self._store[label], thedict) class __helper: def __call__(self, thedict): return fct(thedict) return __helper() def __writePy(self, label): fct = lambda fileName: self._store.write(fileName, label) class __helper: def __call__(self, fileName): return fct(fileName) return __helper() def __readPy(self, label): fct = lambda fileName: self._store.read(fileName, label) class __helper: def __call__(self, fileName): return fct(fileName) return __helper() def isInInput(self, type="", key="*"): flag = self.isInTransient(type, key) or self.isInInputFile(type, key) if not flag: from RecExConfig.RecConfFlags import recConfFlags if recConfFlags.AllowBackNavigation() and self.isInInputBackNav( type, key): flag = True return flag def Print(self): print self._store def __repr__(self): return "%r" % self._store def __str__(self): return "%s" % self._store pass # _ObjKeyStore
def test4Persistency(self): """Test persistency of CfgKeyStore""" ks = CfgKeyStore("MyStore") self.failUnless(ks.streamESD() == []) self.failUnless(ks.streamAOD() == []) self.failUnless(ks.transient() == []) ks.streamESD.add({ 'McEventCollection': ['GEN_EVENT', 'GEN_AOD', 'TruthEvent'], }) self.failUnless(len(ks.streamESD()) == 3) self.failUnless(len(ks.streamAOD()) == 0) self.failUnless(len(ks.transient()) == 3) import os outFileName = 'esd_%s.py' % str(os.getpid()) ks.write(outFileName, 'streamESD') del ks ks = CfgKeyStore("MyStore") self.failUnless(ks.streamESD() == []) self.failUnless(ks.streamAOD() == []) self.failUnless(ks.transient() == []) ks.read(outFileName, 'streamAOD') self.failUnless(len(ks.streamESD()) == 0) self.failUnless(len(ks.streamAOD()) == 3) self.failUnless(len(ks.transient()) == 3) del ks ks = CfgKeyStore("MyStore") self.failUnless(ks.streamESD() == []) self.failUnless(ks.streamAOD() == []) self.failUnless(ks.transient() == []) ks.read(outFileName, 'streamAOD') self.failUnless(len(ks.streamESD()) == 0) self.failUnless(len(ks.streamAOD()) == 3) self.failUnless(len(ks.transient()) == 3) del ks ks = CfgKeyStore("MyStore") self.failUnless(ks.streamESD() == []) self.failUnless(ks.streamAOD() == []) self.failUnless(ks.transient() == []) ks.read(outFileName, 'streamESD') self.failUnless(len(ks.streamESD()) == 3) self.failUnless(len(ks.streamAOD()) == 0) self.failUnless(len(ks.transient()) == 3) del ks ks = CfgKeyStore("MyStore") self.failUnless(ks.streamESD() == []) self.failUnless(ks.streamAOD() == []) self.failUnless(ks.transient() == []) ks.read(outFileName, 'streamESD') ks.read(outFileName, 'streamAOD') self.failUnless(len(ks.streamESD()) == 3) self.failUnless(len(ks.streamAOD()) == 3) self.failUnless(len(ks.transient()) == 3) os.remove(outFileName) os.remove(outFileName + 'c') import shelve outFileName = 'esd_%s.dat' % str(os.getpid()) del ks ks = CfgKeyStore("MyStore") ks.streamESD.add({ 'McEventCollection': ['GEN_EVENT', 'GEN_AOD', 'TruthEvent'], }) self.failUnless(len(ks.streamESD()) == 3) self.failUnless(len(ks.streamAOD()) == 0) self.failUnless(len(ks.transient()) == 3) print 'outFileName:', outFileName db = shelve.open(outFileName, 'c') db['store'] = ks db.close() del ks db = shelve.open(outFileName, 'r') ks = db['store'] db.close() self.failUnless(len(ks.streamESD()) == 3) self.failUnless(len(ks.streamAOD()) == 0) self.failUnless(len(ks.transient()) == 3) ## MacOSX silently appends the .db suffix to shelve files. I haven't found a ## mechanism for determining the actual filename, which is why I've added the ## MacOSX-specific hack. from sys import platform if platform != 'darwin': os.remove(outFileName) else: os.remove(outFileName + '.db')
def test4Persistency( self ): """Test persistency of CfgKeyStore""" ks = CfgKeyStore( "MyStore" ) self.failUnless( ks.streamESD() == [] ) self.failUnless( ks.streamAOD() == [] ) self.failUnless( ks.transient() == [] ) ks.streamESD.add( { 'McEventCollection' : [ 'GEN_EVENT', 'GEN_AOD', 'TruthEvent' ], } ) self.failUnless( len(ks.streamESD()) == 3 ) self.failUnless( len(ks.streamAOD()) == 0 ) self.failUnless( len(ks.transient()) == 3 ) import os outFileName = 'esd_%s.py' % str(os.getpid()) ks.write( outFileName, 'streamESD' ) del ks ks = CfgKeyStore( "MyStore" ) self.failUnless( ks.streamESD() == [] ) self.failUnless( ks.streamAOD() == [] ) self.failUnless( ks.transient() == [] ) ks.read( outFileName, 'streamAOD' ) self.failUnless( len(ks.streamESD()) == 0 ) self.failUnless( len(ks.streamAOD()) == 3 ) self.failUnless( len(ks.transient()) == 3 ) del ks ks = CfgKeyStore( "MyStore" ) self.failUnless( ks.streamESD() == [] ) self.failUnless( ks.streamAOD() == [] ) self.failUnless( ks.transient() == [] ) ks.read( outFileName, 'streamAOD' ) self.failUnless( len(ks.streamESD()) == 0 ) self.failUnless( len(ks.streamAOD()) == 3 ) self.failUnless( len(ks.transient()) == 3 ) del ks ks = CfgKeyStore( "MyStore" ) self.failUnless( ks.streamESD() == [] ) self.failUnless( ks.streamAOD() == [] ) self.failUnless( ks.transient() == [] ) ks.read( outFileName, 'streamESD' ) self.failUnless( len(ks.streamESD()) == 3 ) self.failUnless( len(ks.streamAOD()) == 0 ) self.failUnless( len(ks.transient()) == 3 ) del ks ks = CfgKeyStore( "MyStore" ) self.failUnless( ks.streamESD() == [] ) self.failUnless( ks.streamAOD() == [] ) self.failUnless( ks.transient() == [] ) ks.read( outFileName, 'streamESD' ) ks.read( outFileName, 'streamAOD' ) self.failUnless( len(ks.streamESD()) == 3 ) self.failUnless( len(ks.streamAOD()) == 3 ) self.failUnless( len(ks.transient()) == 3 ) os.remove( outFileName ) os.remove( outFileName+'c' ) import shelve outFileName = 'esd_%s.dat' % str(os.getpid()) del ks ks = CfgKeyStore( "MyStore" ) ks.streamESD.add( { 'McEventCollection' : [ 'GEN_EVENT', 'GEN_AOD', 'TruthEvent' ], } ) self.failUnless( len(ks.streamESD()) == 3 ) self.failUnless( len(ks.streamAOD()) == 0 ) self.failUnless( len(ks.transient()) == 3 ) print 'outFileName:',outFileName db = shelve.open( outFileName, 'c' ) db['store'] = ks db.close() del ks db = shelve.open( outFileName, 'r' ) ks = db['store'] db.close() self.failUnless( len(ks.streamESD()) == 3 ) self.failUnless( len(ks.streamAOD()) == 0 ) self.failUnless( len(ks.transient()) == 3 ) ## MacOSX silently appends the .db suffix to shelve files. I haven't found a ## mechanism for determining the actual filename, which is why I've added the ## MacOSX-specific hack. from sys import platform if platform != 'darwin' : os.remove( outFileName ) else: os.remove( outFileName+'.db')