comparison roundup/backends/__init__.py @ 2925:e54c95de4c6a

fix: second call to get_backend() succeeded... ...even if the backend is not available. get_backend() (and hence has_backend and list_backends) registers backward-compatible globals for available backends and uses these globals to skip import if already done. fix vim modeline
author Alexander Smishlajev <a1s@users.sourceforge.net>
date Thu, 18 Nov 2004 15:50:37 +0000
parents adec352e2ce0
children 6610a5e3c1d5
comparison
equal deleted inserted replaced
2924:df4a3355ee8f 2925:e54c95de4c6a
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" 14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, 15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 # 17 #
18 # $Id: __init__.py,v 1.29 2004-11-03 01:34:21 richard Exp $ 18 # $Id: __init__.py,v 1.30 2004-11-18 15:50:37 a1s Exp $
19 19
20 '''Container for the hyperdb storage backend implementations. 20 '''Container for the hyperdb storage backend implementations.
21 ''' 21 '''
22 __docformat__ = 'restructuredtext' 22 __docformat__ = 'restructuredtext'
23 23
24 import sys
25
26 # These names are used to suppress import errors.
27 # If get_backend raises an ImportError with appropriate
28 # module name, have_backend quietly returns False.
29 # Otherwise the error is reraised.
24 _modules = { 30 _modules = {
25 'mysql': 'MySQLdb', 31 'mysql': 'MySQLdb',
26 'postgresql': 'psycopg', 32 'postgresql': 'psycopg',
27 } 33 }
28 34
29 def get_backend(name): 35 def get_backend(name):
30 '''Get a specific backend by name.''' 36 '''Get a specific backend by name.'''
31 return __import__('back_%s'%name, globals()) 37 vars = globals()
38 # if requested backend has been imported yet, return current instance
39 if vars.has_key(name):
40 return vars[name]
41 # import the backend module
42 module_name = 'back_%s' % name
43 try:
44 module = __import__(module_name, vars)
45 except:
46 # import failed, but the (empty) module is already
47 # placed in sys.modules and package globals;
48 # next import would success although the module is unusable
49 del sys.modules['.'.join((__name__, module_name))]
50 del vars[module_name]
51 raise
52 else:
53 vars[name] = module
54 return module
32 55
33 def have_backend(name): 56 def have_backend(name):
34 '''Is backend "name" available?''' 57 '''Is backend "name" available?'''
35 module = _modules.get(name, name)
36 try: 58 try:
37 get_backend(name) 59 get_backend(name)
38 return 1 60 return 1
39 except ImportError, e: 61 except ImportError, e:
40 if not str(e).startswith('No module named %s'%module): 62 global _modules
63 if not str(e).startswith('No module named %s' % _modules[name]):
41 raise 64 raise
42 return 0 65 return 0
43 66
44 def list_backends(): 67 def list_backends():
45 '''List all available backend names.''' 68 '''List all available backend names.
69
70 This function has side-effect of registering backward-compatible
71 globals for all available backends.
72
73 '''
46 l = [] 74 l = []
47 for name in 'anydbm', 'mysql', 'sqlite', 'metakit', 'postgresql': 75 for name in 'anydbm', 'mysql', 'sqlite', 'metakit', 'postgresql':
48 if have_backend(name): 76 if have_backend(name):
49 l.append(name) 77 l.append(name)
50 return l 78 return l
51 79
52 # vim: set filetype=python ts=4 sw=4 et si 80 # vim: set filetype=python sts=4 sw=4 et si :

Roundup Issue Tracker: http://roundup-tracker.org/