Skip to content

Commit

Permalink
Move class-specific behaviors into the classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Sep 20, 2024
1 parent c5c06ce commit ef69469
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
15 changes: 3 additions & 12 deletions keyring/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,11 @@ def do_get(self):
getattr(self, f'_emit_{self.output_format}')(credential)

def _emit_json(self, credential: credentials.Credential):
if isinstance(credential, credentials.AnonymousCredential):
print(json.dumps(dict(password=credential.password)))
return
print(
json.dumps(dict(username=credential.username, password=credential.password))
)
print(json.dumps(credential._vars()))

def _emit_plain(self, credential: credentials.Credential):
if (
not isinstance(credential, credentials.AnonymousCredential)
and credential.username
):
print(credential.username)
print(credential.password)
for val in credential._vars().values():
print(val)

def _get_creds(self) -> credentials.Credential | None:
return get_credential(self.service, self.username)
Expand Down
6 changes: 6 additions & 0 deletions keyring/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def username(self) -> str: ...
@abc.abstractproperty
def password(self) -> str: ...

def _vars(self) -> dict[str, str]:
return dict(username=self.username, password=self.password)


class SimpleCredential(Credential):
"""Simple credentials implementation"""
Expand All @@ -38,6 +41,9 @@ def __init__(self, password: str):
def username(self) -> str:
raise ValueError("Anonymous credential has no username")

def _vars(self) -> dict[str, str]:
return dict(password=self.password)


class EnvironCredential(Credential):
"""
Expand Down

0 comments on commit ef69469

Please sign in to comment.