-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out wingreg usage to simplify.
Querying the registry in each caller made the test mocks quite messy. Factor that out so we can clean up the implementation and the tests. This also removes the "is standalone" and "is steam" APIs since those don't seem useful and they complicated the implementation. The other cleanup this makes easy is switching to the contextmanager OpenKey, which fixes the bug I found when writing the test in the previous PR.
- Loading branch information
Showing
3 changed files
with
185 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"""Wrapper around the stdlib winreg that fails gracefully on non-Windows.""" | ||
import logging | ||
from typing import Any, Callable, Optional, TypeVar | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
def read_current_user_value( | ||
key: str, value: str, ctor: Callable[[Any], T] = lambda x: x | ||
) -> Optional[T]: | ||
try: | ||
import winreg | ||
except ImportError: | ||
logging.getLogger("pydcs").error( | ||
"Cannot read registry keys on non-Windows OS, returning None" | ||
) | ||
return None | ||
|
||
try: | ||
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, key) as hkey: | ||
# QueryValueEx returns a tuple of (value, type ID). | ||
return ctor(winreg.QueryValueEx(hkey, value)[0]) | ||
except FileNotFoundError: | ||
return None |
Oops, something went wrong.