Skip to content

Commit

Permalink
refactor: class method order
Browse files Browse the repository at this point in the history
  • Loading branch information
loqusion committed Mar 14, 2024
1 parent 91e51f8 commit 29356f0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
28 changes: 14 additions & 14 deletions src/hyprshade/cli/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ def is_in_shader_paths(self):
raise RuntimeError("ShaderWithMeta.is_in_shader_paths is not set")
return is_in_shader_paths

@classmethod
def get_shaders_list(cls) -> list[ShaderWithMeta]:
current = cls._current()
shaders = list(map(cls, ls_dirs(Shader.dirs.all())))
if current:
i = cls._bisect(shaders, current)
if shaders[i] == current:
shaders[i]._is_current = True
shaders[i]._is_in_shader_paths = True
else:
current._is_in_shader_paths = False
shaders.insert(i, current)
return shaders

@classmethod
def _current(cls) -> ShaderWithMeta | None:
shader = super().current()
Expand All @@ -52,20 +66,6 @@ def _bisect(a: list[ShaderWithMeta], x: ShaderWithMeta) -> int:
return i
return first_index

@staticmethod
def get_shaders_list() -> list[ShaderWithMeta]:
current = ShaderWithMeta._current()
shaders = list(map(ShaderWithMeta, ls_dirs(Shader.dirs.all())))
if current:
i = ShaderWithMeta._bisect(shaders, current)
if shaders[i] == current:
shaders[i]._is_current = True
shaders[i]._is_in_shader_paths = True
else:
current._is_in_shader_paths = False
shaders.insert(i, current)
return shaders


@click.command(short_help="List available screen shaders")
@click.option("-l", "--long", is_flag=True, help="Long listing format")
Expand Down
12 changes: 6 additions & 6 deletions src/hyprshade/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ def convert(
):
return Shader(value)

@staticmethod
def _shader_names() -> Iterable[str]:
return unique_justseen(
sorted(map(stripped_basename, ls_dirs(Shader.dirs.all())))
)

def shell_complete(
self, ctx: click.Context, param: click.Parameter, incomplete: str
):
Expand All @@ -93,3 +87,9 @@ def shell_complete(
return click.Path().shell_complete(ctx, param, incomplete)

return [CompletionItem(name) for name in ShaderParamType._shader_names()]

@staticmethod
def _shader_names() -> Iterable[str]:
return unique_justseen(
sorted(map(stripped_basename, ls_dirs(Shader.dirs.all())))
)
42 changes: 21 additions & 21 deletions src/hyprshade/config/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,16 @@ def __init__(self, path_: str | None = None):
self._dict = Config._load(path_)
self._validate()

@staticmethod
def _load(path_: str) -> ConfigDict:
with open(path_, "rb") as f:
return cast(ConfigDict, tomllib.load(f))
def partition(self) -> tuple[list[ShaderConfig], DefaultShadeConfig | None]:
no_default, yes_default = partition(
lambda s: s.get("default", False), self._dict["shades"]
)
rest = cast(list[ShaderConfig], list(no_default))
default = cast(DefaultShadeConfig, nth(yes_default, 0))

@staticmethod
def get_path() -> str | None:
candidates = [
os.getenv("HYPRSHADE_CONFIG"),
path.join(hypr_config_home(), "hyprshade.toml"),
path.join(hyprshade_config_home(), "config.toml"),
]
return first_true((c for c in candidates if c is not None), pred=path.isfile)
assert nth(yes_default, 0) is None, "There should be only one default shade"

return rest, default

def _validate(self) -> None:
shaders = self._dict.get("shades", [])
Expand All @@ -54,16 +51,19 @@ def _validate(self) -> None:
f"Non-default shader '{shader['name']}' must define `start_time`",
)

def partition(self) -> tuple[list[ShaderConfig], DefaultShadeConfig | None]:
no_default, yes_default = partition(
lambda s: s.get("default", False), self._dict["shades"]
)
rest = cast(list[ShaderConfig], list(no_default))
default = cast(DefaultShadeConfig, nth(yes_default, 0))

assert nth(yes_default, 0) is None, "There should be only one default shade"
@staticmethod
def _load(path_: str) -> ConfigDict:
with open(path_, "rb") as f:
return cast(ConfigDict, tomllib.load(f))

return rest, default
@staticmethod
def get_path() -> str | None:
candidates = [
os.getenv("HYPRSHADE_CONFIG"),
path.join(hypr_config_home(), "hyprshade.toml"),
path.join(hyprshade_config_home(), "config.toml"),
]
return first_true((c for c in candidates if c is not None), pred=path.isfile)


class ConfigError(Exception):
Expand Down
12 changes: 6 additions & 6 deletions src/hyprshade/config/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def __init__(self, config: Config):
else None
)

@classmethod
def from_config(cls, path_: str | None = None) -> Schedule:
from .core import Config

return cls(Config(path_))

def scheduled_shader(self, t: time) -> Shader | None:
for shader, (start_time, end_time) in self._resolved_entries():
if is_time_between(t, start_time, end_time):
Expand All @@ -65,9 +71,3 @@ def _resolved_entries(self) -> Iterator[tuple[Shader, TimeRange]]:
start_time = entry.start_time
end_time = entry.end_time or next_entry.start_time
yield entry.shader, (start_time, end_time)

@staticmethod
def from_config(path_: str | None = None) -> Schedule:
from .core import Config

return Schedule(Config(path_))

0 comments on commit 29356f0

Please sign in to comment.