Skip to content

Commit

Permalink
Remove _OS_METHODS variable to make it easier to add custom methods
Browse files Browse the repository at this point in the history
Having _OS_METHODS meant we couldn't do `_OS_MODULE.METHODS += (MyMethod,)` and that users would have
to update the variable in multiple places. Now, we just access the methods via _OS_MODULE and the user
only has to change it in one place
  • Loading branch information
Crozzers committed Oct 8, 2024
1 parent 3066915 commit 011a847
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 10 deletions.
9 changes: 2 additions & 7 deletions screen_brightness_control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import time
import traceback
from dataclasses import dataclass, field
from types import ModuleType
from typing import Callable, Any, Dict, List, Optional, Tuple, Type, Union, FrozenSet, ClassVar
from ._version import __author__, __version__ # noqa: F401
from .exceptions import NoValidDisplayError, format_exc
Expand Down Expand Up @@ -320,7 +319,7 @@ def get_methods(name: Optional[str] = None) -> Dict[str, Type[BrightnessMethod]]
print('Associated monitors:', sbc.list_monitors(method=method_name))
```
'''
methods = {i.__name__.lower(): i for i in _OS_METHODS}
methods: Dict[str, type[BrightnessMethod]] = {i.__name__.lower(): i for i in _OS_MODULE.METHODS}

if name is None:
return methods
Expand Down Expand Up @@ -366,7 +365,7 @@ class Display():
'''The serial number of the display or (if serial is not available) an ID assigned by the OS'''

_logger: logging.Logger = field(init=False, repr=False)
_fade_thread_dict: ClassVar[Dict[FrozenSet[Tuple[Any, Any]], threading.Thread]] = {}
_fade_thread_dict: ClassVar[Dict[FrozenSet[Any], threading.Thread]] = {}
'''A dictionary mapping display identifiers to latest fade threads for stopping fades.'''

def __post_init__(self):
Expand Down Expand Up @@ -761,16 +760,12 @@ def __brightness(
raise ScreenBrightnessError(msg)


_OS_MODULE: ModuleType
_OS_METHODS: Tuple[Type[BrightnessMethod], ...]
if platform.system() == 'Windows':
from . import windows
_OS_MODULE = windows
_OS_METHODS = windows.METHODS
elif platform.system() == 'Linux':
from . import linux
_OS_MODULE = linux
_OS_METHODS = linux.METHODS
else:
_logger.warning(
f'package imported on unsupported platform ({platform.system()})')
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
@pytest.fixture(autouse=True)
def mock_os_module(monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr(sbc, '_OS_MODULE', os_module_mock)
monkeypatch.setattr(sbc, '_OS_METHODS', os_module_mock.METHODS)
return os_module_mock


Expand Down
4 changes: 2 additions & 2 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def test_returns_dict_of_brightness_methods(self, subtests):
methods = sbc.get_methods()
assert isinstance(methods, dict)
# check all methods included
assert tuple(methods.values()) == sbc._OS_METHODS
assert tuple(methods.values()) == sbc._OS_MODULE.METHODS
# check names match up
for name, method_class in methods.items():
with subtests.test(method=name):
Expand All @@ -206,7 +206,7 @@ def test_returns_dict_of_brightness_methods(self, subtests):
class TestNameKwarg:
def test_non_str_raises_type_error(self):
with pytest.raises(TypeError, match=r'name must be of type str.*'):
sbc.get_methods(sbc._OS_METHODS[0]) # type: ignore
sbc.get_methods(sbc._OS_MODULE.METHODS[0]) # type: ignore

def test_raises_value_error_on_invalid_lookup(self):
with pytest.raises(ValueError, match=r'invalid method.*'):
Expand Down

0 comments on commit 011a847

Please sign in to comment.