Skip to content

Commit

Permalink
refactor: make custom property instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Jomik committed Dec 7, 2024
1 parent 3b72920 commit 74e6300
Showing 1 changed file with 55 additions and 27 deletions.
82 changes: 55 additions & 27 deletions scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import time
import traceback
from dataclasses import dataclass
from typing import Callable, Optional, TypedDict, TypeVar, final
from typing import Callable, Generic, Optional, TypedDict, TypeVar, final

import chelper
import msgproto
Expand Down Expand Up @@ -88,6 +88,11 @@ class SettingsContainer:

def __init__(self, config: ConfigWrapper) -> None:
self._config = config
self._invoke_getters()

def _invoke_getters(self):
for attr_name in dir(self):
getattr(self, attr_name)

def get_config(self) -> ConfigWrapper:
return self._config
Expand All @@ -104,23 +109,52 @@ def with_gcode_command(self, gcmd: GCodeCommand):
S = TypeVar("S", bound=SettingsContainer)


@final
class Setting(Generic[T, S]):
_value = sentinel

def __init__(
self,
config_name: str,
get_config_value: Callable[[ConfigWrapper], T],
get_gcmd_value: Callable[[GCodeCommand, T], T],
*,
deprecate: bool,
) -> None:
super().__init__()
self._get_config_value = get_config_value
self._get_gcmd_value = get_gcmd_value
self._config_name = config_name
self._deprecate = deprecate

def __get__(self, instance: S, _) -> T:
if self._value is sentinel:
self._value = self._get_config_value(instance.get_config())
if self._deprecate:
instance.get_config().deprecate(self._config_name)
gcmd = instance.get_gcode_command()
if gcmd is None:
return self._value
return self._get_gcmd_value(gcmd, self._value)

def __set__(self, instance: S, value: T):
self.value = value
config = instance.get_config()
configfile = config.get_printer().lookup_object("configfile")
configfile.set(config.get_name(), self._config_name, value)


def setting_decorator(
config_name: str,
get_config_value: Callable[[ConfigWrapper], T],
get_gcmd_value: Callable[[GCodeCommand, T], T],
*,
deprecate: bool,
):
def decorator(_: Callable[[S], None]):
value = sentinel

def wrapper(container: S) -> T:
nonlocal value
gcmd = container.get_gcode_command()
if value is sentinel:
value = get_config_value(container.get_config())
if gcmd is None:
return value
return get_gcmd_value(gcmd, value)

return wrapper
return Setting[T, S](
config_name, get_config_value, get_gcmd_value, deprecate=deprecate
)

return decorator

Expand All @@ -132,10 +166,13 @@ def int_setting(
default: int,
minval: Optional[int] = None,
maxval: Optional[int] = None,
deprecate: bool = False,
):
return setting_decorator(
config_name,
lambda config: config.getint(config_name, default, minval, maxval),
lambda gcmd, value: gcmd.get_int(arg_name, value, minval, maxval),
deprecate=deprecate,
)


Expand All @@ -148,20 +185,22 @@ def float_setting(
maxval: Optional[float] = None,
above: Optional[float] = None,
below: Optional[float] = None,
deprecate: bool = False,
):
return setting_decorator(
config_name,
lambda config: config.getfloat(
config_name, default, minval, maxval, above, below
),
lambda gcmd, value: gcmd.get_float(
arg_name, value, minval, maxval, above, below
),
deprecate=deprecate,
)


@final
class ScannerSettings(SettingsContainer):
@property
class ScannerTouchSettings(SettingsContainer):
@float_setting(
config_name="scanner_touch_speed",
arg_name="SPEED",
Expand All @@ -171,7 +210,6 @@ class ScannerSettings(SettingsContainer):
def speed(self):
pass

@property
@float_setting(
config_name="scanner_touch_accel",
arg_name="ACCEL",
Expand All @@ -182,7 +220,6 @@ def speed(self):
def accel(self):
pass

@property
@float_setting(
config_name="scanner_touch_retract_dist",
arg_name="RETRACT",
Expand All @@ -192,7 +229,6 @@ def accel(self):
def retract_dist(self):
pass

@property
@float_setting(
config_name="scanner_touch_retract_speed",
arg_name="RETRACT_SPEED",
Expand All @@ -202,7 +238,6 @@ def retract_dist(self):
def retract_speed(self):
pass

@property
@int_setting(
config_name="scanner_touch_sample_count",
arg_name="SAMPLES",
Expand All @@ -212,7 +247,6 @@ def retract_speed(self):
def sample_count(self):
pass

@property
@float_setting(
config_name="scanner_touch_tolerance",
arg_name="TOLERANCE",
Expand All @@ -222,7 +256,6 @@ def sample_count(self):
def tolerance(self):
pass

@property
@int_setting(
config_name="scanner_touch_max_retries",
arg_name="RETRIES",
Expand All @@ -232,7 +265,6 @@ def tolerance(self):
def max_retries(self):
pass

@property
@float_setting(
config_name="scanner_touch_move_speed",
arg_name="MOVE_SPEED",
Expand All @@ -242,7 +274,6 @@ def max_retries(self):
def move_speed(self):
pass

@property
@float_setting(
config_name="scanner_touch_z_offset",
arg_name="Z_OFFSET",
Expand All @@ -252,7 +283,6 @@ def move_speed(self):
def z_offset(self):
pass

@property
@int_setting(
config_name="scanner_touch_threshold",
arg_name="THRESHOLD",
Expand All @@ -261,7 +291,6 @@ def z_offset(self):
def threshold(self):
pass

@property
@float_setting(
config_name="scanner_touch_max_temp",
arg_name="MAX_TEMP",
Expand All @@ -270,7 +299,6 @@ def threshold(self):
def max_temp(self):
pass

@property
@float_setting(
config_name="scanner_touch_fuzzy_touch",
arg_name="FUZZY_TOUCH",
Expand All @@ -284,7 +312,7 @@ def fuzzy_touch(self):
@final
class Scanner:
def __init__(self, config: ConfigWrapper):
self.settings = ScannerSettings(config)
self.touch_settings = ScannerTouchSettings(config)
self.printer = config.get_printer()
self.reactor = self.printer.get_reactor()
self.name = config.get_name()
Expand Down

0 comments on commit 74e6300

Please sign in to comment.