On 23 May 2006 at 21:01, Luís Bruno wrote: > > class DatabaseMiddleware(object): > > > > def __init__(self, application, global_conf, > > db_uri=None, echo=False, **kw): > > This looks very much like the paste.filter_app_factory protocol, don't > you agree? You might as well move the assert in the factory method > here, I guess.
It does, but paste deploy looks for a function, not a class.. So the factory has to be a function. > > > class Manager(object): > > > > def __init__(self, *args, **kw): > > self.aborted = False > > global_connect(*args, **kw ) > > self.engine = schema.default_engine.get_engine() > > self.engine.begin() > > This is where you've lost me; does this connect to the database on > every request? Since you've global_connect()'ed in the middleware's > __init__(), I thought you wouldn't need to do anything else than a > begin(). I'm using scgi, so default_engine.get_engine() looks in thread_local storage for the db connection. The first webrequest is running in a different thread than main, so without the global_connect() call, get_engine() dies with a key error. anyway, global_connect() caches engines by uri, so it's not too bad. > Maybe I'm not getting it, but wouldn't you need to provide each > Table() or Mapper() to the response generator? I *think* the MetaData > in the -schema branch has a list of tables, so I don't need to provide > anything else; <del>but I don't think you can get away with just an > engine.</del> ah, yes; you can use engine.tables[name]. Never mind. In my case, the table meta data is generated w/o any engine being specified, so the proxy engine is used then.. The filter just puts the manager instance into the environment, lower down app code does whatever it needs to do with sqlalchemy. In my case, I don't yet need the objectstore access in my app, but I will. So, here's an example app with REST exposure of some data. Module dbtables has my table meta-data (still a work inprogress) def __call__(self, environ, start_response): """Generate the response""" transaction_manager = environ['p2pserver.transaction_manager'] table_name = request.path_info_pop(environ) table = table_name and getattr(dbtables, table_name, None) or None if not table: return httpexceptions.HTTPBadRequest(detail="Invalid tablename %r" % table_name)(environ, start_response) request_method = environ['REQUEST_METHOD'] if request_method == 'GET': form = variable_decode(request.parse_formvars(environ)) print "form", form id = form.get('id') criteria = form.get('criteria', {}) print "criteria", criteria if id: # expect a single record record = table.get(int(id)) results = { 'table_name':table_name, 'id':id, 'record':record or None, } else: args = [] if criteria: for k, v in criteria.items(): args.append(table.c[k] == v) records = table.select(*args).data results = { 'table_name':table_name, 'records':records, 'criteria':criteria or None, } results['columns'] = table.c.keys() else: form = {} results = { 'error':'no request', } response = xml_string(object=results, name="results", policy=self.policy) response = DataApp(response, content_type='text/xml') return response(environ, start_response) -- Brad Clements, [EMAIL PROTECTED] (315)268-1000 http://www.murkworks.com AOL-IM or SKYPE: BKClements ------------------------------------------------------- All the advantages of Linux Managed Hosting--Without the Cost and Risk! Fully trained technicians. The highest number of Red Hat certifications in the hosting industry. Fanatical Support. Click to learn more http://sel.as-us.falkag.net/sel?cmd=lnk&kid7521&bid$8729&dat1642 _______________________________________________ Sqlalchemy-users mailing list Sqlalchemy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users