class Schema: """A collection of table definitions designed to work together. One Python process can manipulate different Databases that share the same Schema. """ tableClasses=NotImplementedError defaultLangs = ('en',) def __init__(self, session=None, checkIntegrityOnStartup=False, tempDir=".", langs=None): if session is None: session=Session() else: assert isinstance(session,Session) self.session=session self.tempDir=tempDir self.checkIntegrityOnStartup = checkIntegrityOnStartup self._initDone= False self._datasourceRenderer= None self._contextRenderer= None if langs is None: langs="en de fr nl et" self._possibleLangs = tuple(langs.split()) self._tables = [] self.tables = AttrDict() self.initialize() ## def setupOptionParser(self,parser): ## Application.setupOptionParser(self,parser) ## #def call_set(option, opt_str, value, parser,**kw): ## # self.set(**kw) ## parser.add_option("-c", ## "--check", ## help="perform integrity check on startup", ## action="store_true", ## dest="checkIntegrityOnStartup", ## default=False, ## ) ## def applyOptions(self,options,args): ## Application.applyOptions(self,options,args) ## #self.checkIntegrityOnStartup = options.checkIntegrityOnStartup ## if len(args) == 1: ## self.filename = args[0] ## else: ## self.filename=os.path.join(self.tempDir, ## self.name+".db") def addTable(self,instanceClass,**kw): #print tableClass assert not self._initDone,\ "Too late to declare new table %s in %r" \ % (instanceClass,self) table=Table(instanceClass,**kw) #table = tableClass(None,**kw) #assert isinstance(table,Table),\ # repr(table)+" is not a Table" name = table.getTableName() if self.tables.has_key(name): oldTable=self.tables.get(name) self._tables[oldTable._id] = table table.registerInSchema(self,oldTable._id) setattr(self.tables,name,table) else: #if name == "Partners": # print "new table Partners in %s" % self.tables.keys() table.registerInSchema(self,len(self._tables)) self._tables.append(table) self.tables.define(name,table) return table ## def addPlugin(self,plugin): ## assert isinstance(plugin,SchemaPlugin),\ ## repr(plugin)+" is not a SchemaPlugin" ## assert not self._initDone,\ ## "Too late to declare new plugins in " + repr(self) ## plugin.registerInSchema(self,len(self._plugins)) ## self._plugins.append(plugin) ## name = plugin.getName() ## self.plugins.define(name,plugin) ## def addForm(self,form): ## assert isinstance(form,FormTemplate), \ ## repr(form)+" is not a FormTemplate" ## assert not self._startupDone,\ ## "Too late to declare new forms in " + repr(self) ## form.registerInSchema(self,len(self.forms)) ## name = form.getFormName() ## self.forms.define(name,form) ## #assert not self._forms.has_key(name) ## #self._forms[name] = form def initialize(self): """ initialize will be called exactly once, after having declared all tables of the database. """ assert not self._initDone if self._initDone: return self.setupSchema() #self.console.debug( # "Initializing %d tables..." % len(self._tables)) # loop 1 for table in self._tables: table.init1() # loop 2 todo = list(self._tables) while len(todo): tryagain = [] somesuccess = False for table in todo: # print "setupTable", table.getName() try: table.init2() somesuccess = True except StartupDelay, e: #self.console.debug("StartupDelay:"+repr(e)) tryagain.append(table) if not somesuccess: "not supported: primary key with pointer to self" raise "Schema.initialize() failed" todo = tryagain # loop 2bis #for table in self._tables: # table.defineQuery(None) # loop 3 for table in self._tables: table.init3() # loop 4 for table in self._tables: table.init4() self.onInitialize() self._initDone = True
class Schema: """A collection of table definitions designed to work together. One Python process can manipulate different Databases that share the same Schema. """ tableClasses = NotImplementedError defaultLangs = ('en', ) def __init__(self, session=None, checkIntegrityOnStartup=False, tempDir=".", langs=None): if session is None: session = Session() else: assert isinstance(session, Session) self.session = session self.tempDir = tempDir self.checkIntegrityOnStartup = checkIntegrityOnStartup self._initDone = False self._datasourceRenderer = None self._contextRenderer = None if langs is None: langs = "en de fr nl et" self._possibleLangs = tuple(langs.split()) self._tables = [] self.tables = AttrDict() self.initialize() ## def setupOptionParser(self,parser): ## Application.setupOptionParser(self,parser) ## #def call_set(option, opt_str, value, parser,**kw): ## # self.set(**kw) ## parser.add_option("-c", ## "--check", ## help="perform integrity check on startup", ## action="store_true", ## dest="checkIntegrityOnStartup", ## default=False, ## ) ## def applyOptions(self,options,args): ## Application.applyOptions(self,options,args) ## #self.checkIntegrityOnStartup = options.checkIntegrityOnStartup ## if len(args) == 1: ## self.filename = args[0] ## else: ## self.filename=os.path.join(self.tempDir, ## self.name+".db") def addTable(self, instanceClass, **kw): #print tableClass assert not self._initDone,\ "Too late to declare new table %s in %r" \ % (instanceClass,self) table = Table(instanceClass, **kw) #table = tableClass(None,**kw) #assert isinstance(table,Table),\ # repr(table)+" is not a Table" name = table.getTableName() if self.tables.has_key(name): oldTable = self.tables.get(name) self._tables[oldTable._id] = table table.registerInSchema(self, oldTable._id) setattr(self.tables, name, table) else: #if name == "Partners": # print "new table Partners in %s" % self.tables.keys() table.registerInSchema(self, len(self._tables)) self._tables.append(table) self.tables.define(name, table) return table ## def addPlugin(self,plugin): ## assert isinstance(plugin,SchemaPlugin),\ ## repr(plugin)+" is not a SchemaPlugin" ## assert not self._initDone,\ ## "Too late to declare new plugins in " + repr(self) ## plugin.registerInSchema(self,len(self._plugins)) ## self._plugins.append(plugin) ## name = plugin.getName() ## self.plugins.define(name,plugin) ## def addForm(self,form): ## assert isinstance(form,FormTemplate), \ ## repr(form)+" is not a FormTemplate" ## assert not self._startupDone,\ ## "Too late to declare new forms in " + repr(self) ## form.registerInSchema(self,len(self.forms)) ## name = form.getFormName() ## self.forms.define(name,form) ## #assert not self._forms.has_key(name) ## #self._forms[name] = form def initialize(self): """ initialize will be called exactly once, after having declared all tables of the database. """ assert not self._initDone if self._initDone: return self.setupSchema() #self.console.debug( # "Initializing %d tables..." % len(self._tables)) # loop 1 for table in self._tables: table.init1() # loop 2 todo = list(self._tables) while len(todo): tryagain = [] somesuccess = False for table in todo: # print "setupTable", table.getName() try: table.init2() somesuccess = True except StartupDelay, e: #self.console.debug("StartupDelay:"+repr(e)) tryagain.append(table) if not somesuccess: "not supported: primary key with pointer to self" raise "Schema.initialize() failed" todo = tryagain # loop 2bis #for table in self._tables: # table.defineQuery(None) # loop 3 for table in self._tables: table.init3() # loop 4 for table in self._tables: table.init4() self.onInitialize() self._initDone = True