Skip to content

Commit

Permalink
parameterize the name of the keyring service key under which to store…
Browse files Browse the repository at this point in the history
… the memorizations in progress, so I don't end up typing my actual iCloud password on stream

This commit was sponsored by Alex Scammon, hacklschorsch, Brian Grohe,
and my other patrons.  If you want to join them, you can support my
work at https://glyph.im/patrons/.
  • Loading branch information
glyph committed Jan 21, 2025
1 parent aa34e9c commit b4f535a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
24 changes: 17 additions & 7 deletions src/pinpal/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

from __future__ import annotations

from dataclasses import dataclass
from json import dumps, loads

from os import environ
from os.path import expanduser

from keyring import get_password, set_password
Expand All @@ -14,10 +14,13 @@

timecache = expanduser("~/.pinpal-timestamp")

DEFAULT_SERVICE_NAME = environ.get("PINPAL_KEYRING", "pinpal")


@dataclass
class PinPalApp:
memorizations: list[Memorization | Memorization2]
keyringServiceName: str

def save(self) -> None:
"""
Expand All @@ -32,18 +35,27 @@ def save(self) -> None:
)
)
set_password(
"pinpal", "storage", dumps([each.tojson() for each in self.memorizations])
self.keyringServiceName,
"storage",
dumps([each.tojson() for each in self.memorizations]),
)

@classmethod
def load(cls) -> PinPalApp | None:
def new(cls, keyringServiceName: str=DEFAULT_SERVICE_NAME) -> PinPalApp:
"""
Construct a new, blank PinPalApp
"""
return cls([], keyringServiceName)

@classmethod
def load(cls, keyringServiceName: str=DEFAULT_SERVICE_NAME) -> PinPalApp | None:
"""
Load it from somewhere persistent.
"""
stored = get_password("pinpal", "storage")
stored = get_password(keyringServiceName, "storage")
if stored is None:
return None
self = PinPalApp([load(each) for each in loads(stored)])
self = PinPalApp([load(each) for each in loads(stored)], keyringServiceName)
return self


Expand All @@ -52,5 +64,3 @@ def load(x: dict[str, object]) -> Memorization | Memorization2:
return Memorization2.fromjson(x)
else:
return Memorization.fromjson(x)


2 changes: 1 addition & 1 deletion src/pinpal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def main() -> None:
doSelfTest()

app = (
PinPalApp([])
PinPalApp.new()
if (subCommand == "clear") or (maybeApp := PinPalApp.load()) is None
else maybeApp
)
Expand Down
2 changes: 1 addition & 1 deletion src/pinpal/macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MemorizationDataSource(NSObject):
def awakeFromNib(self) -> None:
loaded = PinPalApp.load()
if loaded is None:
loaded = PinPalApp([])
loaded = PinPalApp.new()
self.pinPalApp = loaded

def tableViewSelectionDidChange_(self, notification: NSObject) -> None:
Expand Down

0 comments on commit b4f535a

Please sign in to comment.