Commit | Line | Data |
---|---|---|
c638d827 CR |
1 | # |
2 | # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/) | |
3 | # This module is free software, and you may redistribute it and/or modify | |
4 | # under the same terms as Python, so long as this copyright message and | |
5 | # disclaimer are retained in their original form. | |
6 | # | |
7 | # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR | |
8 | # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING | |
9 | # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE | |
10 | # POSSIBILITY OF SUCH DAMAGE. | |
11 | # | |
12 | # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, | |
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" | |
15 | # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, | |
16 | # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
17 | # | |
18 | # $Id: __init__.py,v 1.40 2007-11-07 20:47:12 richard Exp $ | |
19 | ||
20 | '''Container for the hyperdb storage backend implementations. | |
21 | ''' | |
22 | __docformat__ = 'restructuredtext' | |
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. | |
30 | _modules = { | |
31 | 'mysql': ('MySQLdb',), | |
32 | 'postgresql': ('psycopg',), | |
33 | 'tsearch2': ('psycopg',), | |
34 | 'sqlite': ('pysqlite', 'pysqlite2', 'sqlite3', '_sqlite3', 'sqlite'), | |
35 | } | |
36 | ||
37 | def get_backend(name): | |
38 | '''Get a specific backend by name.''' | |
39 | vars = globals() | |
40 | # if requested backend has been imported yet, return current instance | |
41 | if name in vars: | |
42 | return vars[name] | |
43 | # import the backend module | |
44 | module_name = 'back_%s' % name | |
45 | try: | |
46 | module = __import__(module_name, vars) | |
47 | except: | |
48 | # import failed, but in versions prior to 2.4, a (broken) | |
49 | # module is left in sys.modules and package globals; | |
50 | # subsequent imports would succeed and get the broken module. | |
51 | # This no longer happens in Python 2.4 and later. | |
52 | if sys.version_info < (2, 4): | |
53 | del sys.modules['.'.join((__name__, module_name))] | |
54 | del vars[module_name] | |
55 | raise | |
56 | else: | |
57 | vars[name] = module | |
58 | return module | |
59 | ||
60 | def have_backend(name): | |
61 | '''Is backend "name" available?''' | |
62 | if name == 'tsearch2': | |
63 | # currently not working | |
64 | return 0 | |
65 | try: | |
66 | get_backend(name) | |
67 | return 1 | |
68 | except ImportError, e: | |
69 | for name in _modules.get(name, (name,)): | |
70 | if str(e).startswith('No module named %s'%name): | |
71 | return 0 | |
72 | raise | |
73 | return 0 | |
74 | ||
75 | def list_backends(): | |
76 | '''List all available backend names. | |
77 | ||
78 | This function has side-effect of registering backward-compatible | |
79 | globals for all available backends. | |
80 | ||
81 | ''' | |
82 | l = [] | |
83 | for name in 'anydbm', 'mysql', 'sqlite', 'postgresql': | |
84 | if have_backend(name): | |
85 | l.append(name) | |
86 | return l | |
87 | ||
88 | # vim: set filetype=python sts=4 sw=4 et si : |