George Vilches wrote:
> Russell Keith-Magee wrote:
>> On 8/30/07, George Vilches <[EMAIL PROTECTED]> wrote:
>>> Folks,
>>>
>>> Now that the database backend refactoring has landed, and DB
>>> functionality is really easy to extend, how does everyone feel about the
>>> possibility of allowing people to specify their own database backends
>>> within their projects (i.e., without modifying the Django source tree in
>>> any way?)  I see this as an excellent way for people to increase their
>> The broad idea seems reasonable to me. There's no point having an
>> easily pluggable database engine if you can't plug in your own
>> database :-)
>>
>> Regarding the approach - I'm inclined to prefer #1. It's simple and
>> easy to explain, and I don't see that there is that much potential for
>> side effects. The only clash I can forsee is if you had your own
>> toplevel module that mirrored the backend names of Django - this
>> doesn't really strike me as something that will be a common problem,
>> and if it is, the solution is easy (rename the clashing external
>> module).
> 
> Then in this spirit, my patch (against r6049) is at the end of the 
> message.  Notice that the only real change is:

And I'm amending this slightly due to something I missed in how the 
other modules are imported, we have to adjust the import path for the 
introspection/creation/etc. modules as well.  Patch at bottom, as before.

The only two changes is that there is now an "import_path" string which 
contains the working prefix to the modules (either "django.db.backends" 
or empty), and each import after the base uses the one that's appropriate.

Thanks,
George

Index: django/db/__init__.py
===================================================================
--- django/db/__init__.py       (revision 6049)
+++ django/db/__init__.py       (working copy)
@@ -8,24 +8,29 @@
      settings.DATABASE_ENGINE = 'dummy'

  try:
-    backend = __import__('django.db.backends.%s.base' % 
settings.DATABASE_ENGINE, {}, {}, [''])
+    import_path = 'django.db.backends.'
+    backend = __import__('%s%s.base' % (import_path, 
settings.DATABASE_ENGINE), {}, {}, [''])
  except ImportError, e:
-    # The database backend wasn't found. Display a helpful error message
-    # listing all possible database backends.
-    from django.core.exceptions import ImproperlyConfigured
-    import os
-    backend_dir = os.path.join(__path__[0], 'backends')
-    available_backends = [f for f in os.listdir(backend_dir) if not 
f.startswith('_') and not f.startswith('.') and not f.endswith('.py') 
and not f.endswith('.pyc')]
-    available_backends.sort()
-    if settings.DATABASE_ENGINE not in available_backends:
-        raise ImproperlyConfigured, "%r isn't an available database 
backend. Available options are: %s" % \
-            (settings.DATABASE_ENGINE, ", ".join(map(repr, 
available_backends)))
-    else:
-        raise # If there's some other error, this must be an error in 
Django itself.
+    try:
+        import_path = ''
+        backend = __import__('%s%s.base' % (import_path, 
settings.DATABASE_ENGINE), {}, {}, [''])
+    except ImportError, e_user:
+        # The database backend wasn't found. Display a helpful error 
message
+        # listing all possible database backends.
+        from django.core.exceptions import ImproperlyConfigured
+        import os
+        backend_dir = os.path.join(__path__[0], 'backends')
+        available_backends = [f for f in os.listdir(backend_dir) if not 
f.startswith('_') and not f.startswith('.') and not f.endswith('.py') 
and not f.endswith('.pyc')]
+        available_backends.sort()
+        if settings.DATABASE_ENGINE not in available_backends:
+            raise ImproperlyConfigured, "%r isn't an available database 
backend. Available options are: %s" % \
+                (settings.DATABASE_ENGINE, ", ".join(map(repr, 
available_backends)))
+        else:
+            raise # If there's some other error, this must be an error 
in Django itself.

-get_introspection_module = lambda: 
__import__('django.db.backends.%s.introspection' % 
settings.DATABASE_ENGINE, {}, {}, [''])
-get_creation_module = lambda: 
__import__('django.db.backends.%s.creation' % settings.DATABASE_ENGINE, 
{}, {}, [''])
-runshell = lambda: __import__('django.db.backends.%s.client' % 
settings.DATABASE_ENGINE, {}, {}, ['']).runshell()
+get_introspection_module = lambda: __import__('%s%s.introspection' % 
(import_path, settings.DATABASE_ENGINE), {}, {}, [''])
+get_creation_module = lambda: __import__('%s%s.creation' % 
(import_path, settings.DATABASE_ENGINE), {}, {}, [''])
+runshell = lambda: __import__('%s%s.client' % (import_path, 
settings.DATABASE_ENGINE), {}, {}, ['']).runshell()

  connection = backend.DatabaseWrapper(**settings.DATABASE_OPTIONS)
  DatabaseError = backend.DatabaseError


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to