diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-08-11 15:24:13 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-08-11 22:33:54 -0400 |
| commit | 2a079cdc76b3a0f5b4f37299d280d328586e2f7e (patch) | |
| tree | d7769b2c30ec0fdbdced9514bfbd7936fd9e489e /lib/sqlalchemy/util | |
| parent | f5e57f7c311288d892894edcc44d901b5bfbb3d1 (diff) | |
| download | sqlalchemy-2a079cdc76b3a0f5b4f37299d280d328586e2f7e.tar.gz | |
Rewrite pool reset_on_return parsing using a util function
Choosing a util.symbol() based on a user parameter is about to have
another use case added as part of #4623, so add a generalized solution
ahead of it.
Change-Id: I420631f81af2ffc655995b9cce9ff2ac618c16d7
Diffstat (limited to 'lib/sqlalchemy/util')
| -rw-r--r-- | lib/sqlalchemy/util/langhelpers.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py index 0e0e3f4df..12fc5c0e8 100644 --- a/lib/sqlalchemy/util/langhelpers.py +++ b/lib/sqlalchemy/util/langhelpers.py @@ -1368,6 +1368,41 @@ class symbol(object): finally: symbol._lock.release() + @classmethod + def parse_user_argument( + cls, arg, choices, name, resolve_symbol_names=False + ): + """Given a user parameter, parse the parameter into a chosen symbol. + + The user argument can be a string name that matches the name of a + symbol, or the symbol object itself, or any number of alternate choices + such as True/False/ None etc. + + :param arg: the user argument. + :param choices: dictionary of symbol object to list of possible + entries. + :param name: name of the argument. Used in an :class:`.ArgumentError` + that is raised if the parameter doesn't match any available argument. + :param resolve_symbol_names: include the name of each symbol as a valid + entry. + + """ + # note using hash lookup is tricky here because symbol's `__hash__` + # is its int value which we don't want included in the lookup + # explicitly, so we iterate and compare each. + for sym, choice in choices.items(): + if arg is sym: + return sym + elif resolve_symbol_names and arg == sym.name: + return sym + elif arg in choice: + return sym + + if arg is None: + return None + + raise exc.ArgumentError("Invalid value for '%s': %r" % (name, arg)) + _creation_order = 1 |
