Skip to content

Commit

Permalink
common: make get_redis_connection to accept dict(opts)
Browse files Browse the repository at this point in the history
The check like:

    if hasattr(opts, "redis_port"):
        kwargs["port"] = opts.redis_port

Can not work with dict() objects.  The hasattr() actually doesn't work
this way.

See: praiskup/resalloc#154
  • Loading branch information
praiskup committed Mar 15, 2024
1 parent cbcd216 commit 08df431
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions common/copr_common/redis_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ def get_redis_connection(opts):
:rtype: StrictRedis
"""
kwargs = {}
if hasattr(opts, "redis_db"):
kwargs["db"] = opts.redis_db
if hasattr(opts, "redis_host"):
kwargs["host"] = opts.redis_host
if hasattr(opts, "redis_port"):
kwargs["port"] = opts.redis_port
if hasattr(opts, "redis_password"):
kwargs["password"] = opts.redis_password
for key in ["db", "host", "port", "password"]:
config_key = "redis_" + key

# dict-like objects
if isinstance(opts, dict):
if config_key in opts:
kwargs[key] = opts[config_key]
continue

# class-like objects
if hasattr(opts, config_key):
kwargs[key] = getattr(opts, config_key)
continue

return StrictRedis(encoding="utf-8", decode_responses=True, **kwargs)

0 comments on commit 08df431

Please sign in to comment.