comparison roundup/backends/__init__.py @ 5464:22eecc8a8bd4

fixed backend detection for Python 3
author Christof Meerwald <cmeerw@cmeerw.org>
date Sat, 28 Jul 2018 14:12:06 +0100
parents ebd6df1126a2
children 7f00fc5958ca
comparison
equal deleted inserted replaced
5463:1c9a7310e4f0 5464:22eecc8a8bd4
25 # If get_backend raises an ImportError with appropriate 25 # If get_backend raises an ImportError with appropriate
26 # module name, have_backend quietly returns False. 26 # module name, have_backend quietly returns False.
27 # Otherwise the error is reraised. 27 # Otherwise the error is reraised.
28 _modules = { 28 _modules = {
29 'mysql': ('MySQLdb',), 29 'mysql': ('MySQLdb',),
30 'postgresql': ('psycopg',), 30 'postgresql': ('psycopg2',),
31 'sqlite': ('pysqlite', 'pysqlite2', 'sqlite3', '_sqlite3', 'sqlite'), 31 'sqlite': ('pysqlite', 'pysqlite2', 'sqlite3', '_sqlite3', 'sqlite'),
32 } 32 }
33 33
34 def get_backend(name): 34 def get_backend(name):
35 '''Get a specific backend by name.''' 35 '''Get a specific backend by name.'''
47 '''Is backend "name" available?''' 47 '''Is backend "name" available?'''
48 try: 48 try:
49 get_backend(name) 49 get_backend(name)
50 return 1 50 return 1
51 except ImportError as e: 51 except ImportError as e:
52 for name in _modules.get(name, (name,)): 52 if hasattr(e, 'name'):
53 if str(e).startswith('No module named %s'%name): 53 modname = e.name
54 return 0 54 else:
55 modname = e.args[0][16:] if e.args[0].startswith('No module named ') else None
56
57 if modname and (modname in _modules.get(name, (name,))):
58 return 0
55 raise 59 raise
56 return 0 60 return 0
57 61
58 def list_backends(): 62 def list_backends():
59 '''List all available backend names. 63 '''List all available backend names.

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