From f7375bb6090f32c3feb3d71e196ed01ee19fecc8 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Fri, 23 Jan 2009 15:49:16 -0700 Subject: Added stuff for managing connections and new Executioner backend base class --- ipalib/request.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'ipalib/request.py') diff --git a/ipalib/request.py b/ipalib/request.py index 6ad7ad35..812e526d 100644 --- a/ipalib/request.py +++ b/ipalib/request.py @@ -25,6 +25,7 @@ Per-request thread-local data. import threading import locale import gettext +from base import ReadOnly, lock from constants import OVERRIDE_ERROR @@ -32,6 +33,38 @@ from constants import OVERRIDE_ERROR context = threading.local() +class Connection(ReadOnly): + """ + Base class for connection objects stored on `request.context`. + """ + + def __init__(self, *args, **kw): + self.conn = self.create(*args, **kw) + lock(self) + + def create(self, *args, **kw): + """ + Create and return the connection (implement in subclass). + """ + raise NotImplementedError('%s.create()' % self.__class__.__name__) + + def close(self): + """ + Close the connection (implement in subclass). + """ + raise NotImplementedError('%s.close()' % self.__class__.__name__) + + +def destroy_context(): + """ + Delete all attributes on thread-local `request.context`. + """ + for (name, value) in context.__dict__.items(): + if isinstance(value, Connection): + value.close() + delattr(context, name) + + def ugettext(message): if hasattr(context, 'ugettext'): return context.ugettext(message) -- cgit