Mercurial > p > roundup > code
annotate roundup/backends/__init__.py @ 7752:b2dbab2b34bc
fix(refactor): multiple fixups using ruff linter; more testing.
Converting to using the ruff linter and its rulesets. Fixed a number
of issues.
admin.py:
sort imports
use immutable tuples as default value markers for parameters where a
None value is valid.
reduced some loops to list comprehensions for performance
used ternary to simplify some if statements
named some variables to make them less magic
(e.g. _default_savepoint_setting = 1000)
fixed some tests for argument counts < 2 becomes != 2 so 3 is an
error.
moved exception handlers outside of loops for performance where
exception handler will abort loop anyway.
renamed variables called 'id' or 'dir' as they shadow builtin
commands.
fix translations of form _("string %s" % value) -> _("string %s") %
value so translation will be looked up with the key before
substitution.
end dicts, tuples with a trailing comma to reduce missing comma
errors if modified
simplified sorted(list(self.setting.keys())) to
sorted(self.setting.keys()) as sorted consumes whole list.
in if conditions put compared variable on left and threshold condition
on right. (no yoda conditions)
multiple noqa: suppression
removed unneeded noqa as lint rulesets are a bit different
do_get - refactor output printing logic: Use fast return if not
special formatting is requested; use isinstance with a tuple
rather than two isinstance calls; cleaned up flow and removed
comments on algorithm as it can be easily read from the code.
do_filter, do_find - refactor output printing logic. Reduce
duplicate code.
do_find - renamed variable 'value' that was set inside a loop. The
loop index variable was also named 'value'.
do_pragma - added hint to use list subcommand if setting was not
found. Replaced condition 'type(x) is bool' with 'isinstance(x,
bool)' for various types.
test_admin.py
added testing for do_list
better test coverage for do_get includes: -S and -d for multilinks,
error case for -d with non-link.
better testing for do_find including all output modes
better testing for do_filter including all output modes
fixed expected output for do_pragma that now includes hint to use
pragma list if setting not found.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Fri, 01 Mar 2024 14:53:18 -0500 |
| parents | 7f00fc5958ca |
| children | 95f91b6f0386 |
| rev | line source |
|---|---|
|
213
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
1 # |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/) |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
3 # This module is free software, and you may redistribute it and/or modify |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
4 # under the same terms as Python, so long as this copyright message and |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
5 # disclaimer are retained in their original form. |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
6 # |
| 214 | 7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR |
|
213
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
10 # POSSIBILITY OF SUCH DAMAGE. |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
11 # |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
|
1244
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1190
diff
changeset
|
17 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
18 '''Container for the hyperdb storage backend implementations. |
|
1244
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1190
diff
changeset
|
19 ''' |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1873
diff
changeset
|
20 __docformat__ = 'restructuredtext' |
|
213
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
159
diff
changeset
|
21 |
|
2925
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
22 import sys |
|
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
23 |
|
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
24 # These names are used to suppress import errors. |
|
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
25 # If get_backend raises an ImportError with appropriate |
|
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
26 # module name, have_backend quietly returns False. |
|
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
27 # Otherwise the error is reraised. |
|
2856
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
28 _modules = { |
|
3718
0d561b24ceff
support sqlite3
Richard Jones <richard@users.sourceforge.net>
parents:
3503
diff
changeset
|
29 'mysql': ('MySQLdb',), |
|
5464
22eecc8a8bd4
fixed backend detection for Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5425
diff
changeset
|
30 'postgresql': ('psycopg2',), |
|
4269
ab4563e97d22
corrections for python2.3 compatibility:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3941
diff
changeset
|
31 'sqlite': ('pysqlite', 'pysqlite2', 'sqlite3', '_sqlite3', 'sqlite'), |
|
2856
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
32 } |
|
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
33 |
|
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
34 def get_backend(name): |
|
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
35 '''Get a specific backend by name.''' |
|
2925
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
36 vars = globals() |
|
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
37 # if requested backend has been imported yet, return current instance |
|
4357
13b3155869e0
Beginnings of a big code cleanup / modernisation to make 2to3 happy
Richard Jones <richard@users.sourceforge.net>
parents:
4269
diff
changeset
|
38 if name in vars: |
|
2925
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
39 return vars[name] |
|
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
40 # import the backend module |
|
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
41 module_name = 'back_%s' % name |
|
5425
ebd6df1126a2
Python 3 preparation: update __import__ call for relative import.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5248
diff
changeset
|
42 module = __import__(module_name, vars, level=1) |
|
4687
4960a2c21590
Python 2.3 workarounds cleanup
anatoly techtonik <techtonik@gmail.com>
parents:
4570
diff
changeset
|
43 vars[name] = module |
|
4960a2c21590
Python 2.3 workarounds cleanup
anatoly techtonik <techtonik@gmail.com>
parents:
4570
diff
changeset
|
44 return module |
|
159
764db91c0dea
Checks for ability to import the specific back-end module.
Richard Jones <richard@users.sourceforge.net>
parents:
46
diff
changeset
|
45 |
|
2856
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
46 def have_backend(name): |
|
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
47 '''Is backend "name" available?''' |
|
1809
bd127cafe3a8
Simplify backend importing, by moving the imports into the backend modules.
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1609
diff
changeset
|
48 try: |
|
2856
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
49 get_backend(name) |
|
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
50 return 1 |
|
5248
198b6e810c67
Use Python-3-compatible 'as' syntax for except statements
Eric S. Raymond <esr@thyrsus.com>
parents:
5005
diff
changeset
|
51 except ImportError as e: |
|
5464
22eecc8a8bd4
fixed backend detection for Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5425
diff
changeset
|
52 if hasattr(e, 'name'): |
|
22eecc8a8bd4
fixed backend detection for Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5425
diff
changeset
|
53 modname = e.name |
|
22eecc8a8bd4
fixed backend detection for Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5425
diff
changeset
|
54 else: |
|
22eecc8a8bd4
fixed backend detection for Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5425
diff
changeset
|
55 modname = e.args[0][16:] if e.args[0].startswith('No module named ') else None |
|
22eecc8a8bd4
fixed backend detection for Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5425
diff
changeset
|
56 |
|
6365
7f00fc5958ca
Make memorydb persistent across re-open
Ralf Schlatterbeck <rsc@runtux.com>
parents:
5464
diff
changeset
|
57 # It's always ok if memorydb is not found |
|
7f00fc5958ca
Make memorydb persistent across re-open
Ralf Schlatterbeck <rsc@runtux.com>
parents:
5464
diff
changeset
|
58 if modname.endswith('back_memorydb'): |
|
7f00fc5958ca
Make memorydb persistent across re-open
Ralf Schlatterbeck <rsc@runtux.com>
parents:
5464
diff
changeset
|
59 return 0 |
|
5464
22eecc8a8bd4
fixed backend detection for Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5425
diff
changeset
|
60 if modname and (modname in _modules.get(name, (name,))): |
|
22eecc8a8bd4
fixed backend detection for Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5425
diff
changeset
|
61 return 0 |
|
3718
0d561b24ceff
support sqlite3
Richard Jones <richard@users.sourceforge.net>
parents:
3503
diff
changeset
|
62 raise |
|
2856
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
63 return 0 |
|
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
64 |
|
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
65 def list_backends(): |
|
2925
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
66 '''List all available backend names. |
|
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
67 |
|
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
68 This function has side-effect of registering backward-compatible |
|
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
69 globals for all available backends. |
|
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
70 |
|
6365
7f00fc5958ca
Make memorydb persistent across re-open
Ralf Schlatterbeck <rsc@runtux.com>
parents:
5464
diff
changeset
|
71 Note: Since memorydb does not live in the backends directory, it will |
|
7f00fc5958ca
Make memorydb persistent across re-open
Ralf Schlatterbeck <rsc@runtux.com>
parents:
5464
diff
changeset
|
72 never be found in the default setup. It *can* be enabled by preloading |
|
7f00fc5958ca
Make memorydb persistent across re-open
Ralf Schlatterbeck <rsc@runtux.com>
parents:
5464
diff
changeset
|
73 test/memorydb and injecting into roundup.backends. So the normal user |
|
7f00fc5958ca
Make memorydb persistent across re-open
Ralf Schlatterbeck <rsc@runtux.com>
parents:
5464
diff
changeset
|
74 can never configure memorydb but it makes using the tests easier |
|
7f00fc5958ca
Make memorydb persistent across re-open
Ralf Schlatterbeck <rsc@runtux.com>
parents:
5464
diff
changeset
|
75 because we do not need to monkey-patch list_backends. |
|
7f00fc5958ca
Make memorydb persistent across re-open
Ralf Schlatterbeck <rsc@runtux.com>
parents:
5464
diff
changeset
|
76 |
|
2925
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
77 ''' |
|
2856
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
78 l = [] |
|
6365
7f00fc5958ca
Make memorydb persistent across re-open
Ralf Schlatterbeck <rsc@runtux.com>
parents:
5464
diff
changeset
|
79 for name in 'anydbm', 'mysql', 'sqlite', 'postgresql', 'memorydb': |
|
2856
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
80 if have_backend(name): |
|
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
81 l.append(name) |
|
adec352e2ce0
don't try to import all backends in backends.__init__ unless we *want* to
Richard Jones <richard@users.sourceforge.net>
parents:
2812
diff
changeset
|
82 return l |
|
854
3cdfa5d86cec
Added metakit backend to the db tests...
Richard Jones <richard@users.sourceforge.net>
parents:
733
diff
changeset
|
83 |
|
2925
e54c95de4c6a
fix: second call to get_backend() succeeded...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2856
diff
changeset
|
84 # vim: set filetype=python sts=4 sw=4 et si : |
