Mercurial > p > roundup > code
comparison roundup/backends/__init__.py @ 8259:d7cc63d7a857
chore(ruff): var name changes; unused import; formatting
Handle vars variable hiding builting vars().
Use more descripting variable name.
Remove unused sys import.
Add double lines where needed.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sat, 04 Jan 2025 20:50:59 -0500 |
| parents | 95f91b6f0386 |
| children |
comparison
equal
deleted
inserted
replaced
| 8258:5c5723cd721a | 8259:d7cc63d7a857 |
|---|---|
| 17 | 17 |
| 18 '''Container for the hyperdb storage backend implementations. | 18 '''Container for the hyperdb storage backend implementations. |
| 19 ''' | 19 ''' |
| 20 __docformat__ = 'restructuredtext' | 20 __docformat__ = 'restructuredtext' |
| 21 | 21 |
| 22 import sys | |
| 23 | |
| 24 # These names are used to suppress import errors. | 22 # These names are used to suppress import errors. |
| 25 # If get_backend raises an ImportError with appropriate | 23 # If get_backend raises an ImportError with appropriate |
| 26 # module name, have_backend quietly returns False. | 24 # module name, have_backend quietly returns False. |
| 27 # Otherwise the error is reraised. | 25 # Otherwise the error is reraised. |
| 28 _modules = { | 26 _modules = { |
| 29 'mysql': ('MySQLdb',), | 27 'mysql': ('MySQLdb',), |
| 30 'postgresql': ('psycopg2',), | 28 'postgresql': ('psycopg2',), |
| 31 'sqlite': ('sqlite3', '_sqlite3'), | 29 'sqlite': ('sqlite3', '_sqlite3'), |
| 32 } | 30 } |
| 33 | 31 |
| 32 | |
| 34 def get_backend(name): | 33 def get_backend(name): |
| 35 '''Get a specific backend by name.''' | 34 '''Get a specific backend by name.''' |
| 36 vars = globals() | 35 global_vars = globals() |
| 37 # if requested backend has been imported yet, return current instance | 36 # if requested backend has been imported yet, return current instance |
| 38 if name in vars: | 37 if name in global_vars: |
| 39 return vars[name] | 38 return global_vars[name] |
| 40 # import the backend module | 39 # import the backend module |
| 41 module_name = 'back_%s' % name | 40 module_name = 'back_%s' % name |
| 42 module = __import__(module_name, vars, level=1) | 41 module = __import__(module_name, global_vars, level=1) |
| 43 vars[name] = module | 42 global_vars[name] = module |
| 44 return module | 43 return module |
| 44 | |
| 45 | 45 |
| 46 def have_backend(name): | 46 def have_backend(name): |
| 47 '''Is backend "name" available?''' | 47 '''Is backend "name" available?''' |
| 48 try: | 48 try: |
| 49 get_backend(name) | 49 get_backend(name) |
| 60 if modname and (modname in _modules.get(name, (name,))): | 60 if modname and (modname in _modules.get(name, (name,))): |
| 61 return 0 | 61 return 0 |
| 62 raise | 62 raise |
| 63 return 0 | 63 return 0 |
| 64 | 64 |
| 65 | |
| 65 def list_backends(): | 66 def list_backends(): |
| 66 '''List all available backend names. | 67 '''List all available backend names. |
| 67 | 68 |
| 68 This function has side-effect of registering backward-compatible | 69 This function has side-effect of registering backward-compatible |
| 69 globals for all available backends. | 70 globals for all available backends. |
| 73 test/memorydb and injecting into roundup.backends. So the normal user | 74 test/memorydb and injecting into roundup.backends. So the normal user |
| 74 can never configure memorydb but it makes using the tests easier | 75 can never configure memorydb but it makes using the tests easier |
| 75 because we do not need to monkey-patch list_backends. | 76 because we do not need to monkey-patch list_backends. |
| 76 | 77 |
| 77 ''' | 78 ''' |
| 78 l = [] | 79 backend_list = [] |
| 79 for name in 'anydbm', 'mysql', 'sqlite', 'postgresql', 'memorydb': | 80 for name in 'anydbm', 'mysql', 'sqlite', 'postgresql', 'memorydb': |
| 80 if have_backend(name): | 81 if have_backend(name): |
| 81 l.append(name) | 82 backend_list.append(name) |
| 82 return l | 83 return backend_list |
| 83 | 84 |
| 84 # vim: set filetype=python sts=4 sw=4 et si : | 85 # vim: set filetype=python sts=4 sw=4 et si : |
