Skip to content
Snippets Groups Projects

Fix #4: lazy password evaluation

Merged Agate requested to merge 4-lazy-password into master
2 files
+ 48
1
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 30
1
@@ -88,6 +88,35 @@ RAW_DECORATOR = click.option(
"--raw", is_flag=True, help="Directly output JSON returned by the happy"
)
class lazy_credential():
"""
A proxy object to request access to the proxy object at the later possible point,
cf #4
"""
def __init__(self, *args):
self.args = args
self._cached_value = None
@property
def value(self):
if self._cached_value:
return self._cached_value
v = keyring.get_password(*self.args)
self._cached_value = v
return v
def __str__(self):
return str(self.value)
def __eq__(self, other):
return self.value == other
def __repr__(self):
return str(self.value)
def __bool__(self):
return bool(self.value)
def set_server(ctx, url, token):
ctx.ensure_object(dict)
@@ -96,7 +125,7 @@ def set_server(ctx, url, token):
ctx.obj["SERVER_NETLOC"] = parsed.netloc
ctx.obj["SERVER_PROTOCOL"] = parsed.scheme
try:
token = token or keyring.get_password(url, "_")
token = token or lazy_credential(url, "_")
except ValueError as e:
raise click.ClickException("Error while retrieving password from keyring: {}. Your password may be incorrect.".format(e.args[0]))
except Exception as e:
Loading