Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support glob expansion with fans #9

Merged
merged 1 commit into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/afancontrol/configparser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import configparser
import glob
from typing import Any, Generic, Iterator, Optional, Type, TypeVar, Union, overload

T = TypeVar("T", bound=str)
Expand Down Expand Up @@ -127,3 +128,12 @@ def getboolean(self, option: str, *, fallback=_UNSET) -> Union[bool, F]:
"[%s] %r option is expected to be set" % (self.__section.name, option)
)
return res


def expand_glob(path: str):
matches = glob.glob(path)
if not matches:
return path # a FileNotFoundError will be raised on a first read attempt
if len(matches) == 1:
return matches[0]
raise ValueError("Expected glob to expand to a single path, got %r" % (matches,))
11 changes: 6 additions & 5 deletions src/afancontrol/pwmfan/linux.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
from typing import NewType

from afancontrol.configparser import ConfigParserSection
from afancontrol.configparser import ConfigParserSection, expand_glob
from afancontrol.pwmfan.base import (
BaseFanPWMRead,
BaseFanPWMWrite,
Expand All @@ -18,7 +18,7 @@ class LinuxFanSpeed(BaseFanSpeed):
__slots__ = ("_fan_input",)

def __init__(self, fan_input: FanInputDevice) -> None:
self._fan_input = Path(fan_input)
self._fan_input = Path(expand_glob(fan_input))

@classmethod
def from_configparser(cls, section: ConfigParserSection) -> BaseFanSpeed:
Expand All @@ -35,7 +35,7 @@ class LinuxFanPWMRead(BaseFanPWMRead):
min_pwm = PWMValue(0)

def __init__(self, pwm: PWMDevice) -> None:
self._pwm = Path(pwm)
self._pwm = Path(expand_glob(pwm))

@classmethod
def from_configparser(cls, section: ConfigParserSection) -> BaseFanPWMRead:
Expand All @@ -51,8 +51,9 @@ class LinuxFanPWMWrite(BaseFanPWMWrite):
read_cls = LinuxFanPWMRead

def __init__(self, pwm: PWMDevice) -> None:
self._pwm = Path(pwm)
self._pwm_enable = Path(pwm + "_enable")
base = expand_glob(pwm)
self._pwm = Path(base)
self._pwm_enable = Path(base + "_enable")

@classmethod
def from_configparser(cls, section: ConfigParserSection) -> BaseFanPWMWrite:
Expand Down
13 changes: 2 additions & 11 deletions src/afancontrol/temp/file.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import glob
import re
from pathlib import Path
from typing import Optional, Tuple

from afancontrol.configparser import ConfigParserSection
from afancontrol.configparser import ConfigParserSection, expand_glob
from afancontrol.temp.base import Temp, TempCelsius


def _expand_glob(path: str):
matches = glob.glob(path)
if not matches:
return path # a FileNotFoundError will be raised on a first read attempt
if len(matches) == 1:
return matches[0]
raise ValueError("Expected glob to expand to a single path, got %r" % (matches,))


class FileTemp(Temp):
def __init__(
Expand All @@ -33,7 +24,7 @@ def __init__(
# /sys/devices/pci0000:00/0000:00:01.3/[...]/hwmon/hwmon*/temp1_input
# The `hwmon*` might change after reboot, but it is always a single
# directory within the device.
temp_path = _expand_glob(temp_path + "_input")
temp_path = expand_glob(temp_path + "_input")
temp_path = re.sub(r"_input$", "", temp_path)

self._temp_input = Path(temp_path + "_input")
Expand Down