confighelper: add gettemplate method
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
2142d344dd
commit
e2b821e90c
|
@ -10,6 +10,7 @@ import os
|
||||||
import hashlib
|
import hashlib
|
||||||
from utils import SentinelClass
|
from utils import SentinelClass
|
||||||
from components.gpio import GpioOutputPin
|
from components.gpio import GpioOutputPin
|
||||||
|
from components.template import JinjaTemplate
|
||||||
|
|
||||||
# Annotation imports
|
# Annotation imports
|
||||||
from typing import (
|
from typing import (
|
||||||
|
@ -28,6 +29,7 @@ from typing import (
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from moonraker import Server
|
from moonraker import Server
|
||||||
from components.gpio import GpioFactory
|
from components.gpio import GpioFactory
|
||||||
|
from components.template import TemplateFactory
|
||||||
_T = TypeVar("_T")
|
_T = TypeVar("_T")
|
||||||
ConfigVal = Union[None, int, float, bool, str]
|
ConfigVal = Union[None, int, float, bool, str]
|
||||||
|
|
||||||
|
@ -114,7 +116,7 @@ class ConfigHelper:
|
||||||
self._check_option(option, val, above, below, minval, maxval)
|
self._check_option(option, val, above, below, minval, maxval)
|
||||||
if self.section in self.orig_sections:
|
if self.section in self.orig_sections:
|
||||||
# Only track sections included in the original config
|
# Only track sections included in the original config
|
||||||
if isinstance(val, GpioOutputPin):
|
if isinstance(val, (GpioOutputPin, JinjaTemplate)):
|
||||||
self.parsed[self.section][option] = str(val)
|
self.parsed[self.section][option] = str(val)
|
||||||
else:
|
else:
|
||||||
self.parsed[self.section][option] = val
|
self.parsed[self.section][option] = val
|
||||||
|
@ -292,19 +294,36 @@ class ConfigHelper:
|
||||||
default: Union[SentinelClass, _T] = SENTINEL,
|
default: Union[SentinelClass, _T] = SENTINEL,
|
||||||
initial_value: int = 0
|
initial_value: int = 0
|
||||||
) -> Union[GpioOutputPin, _T]:
|
) -> Union[GpioOutputPin, _T]:
|
||||||
gpio: Optional[GpioFactory]
|
try:
|
||||||
gpio = self.server.load_component(self, 'gpio', None)
|
gpio: GpioFactory = self.server.load_component(self, 'gpio')
|
||||||
if gpio is None:
|
except Exception:
|
||||||
raise ConfigError(
|
raise ConfigError(
|
||||||
f"Section [{self.section}], option '{option}', "
|
f"Section [{self.section}], option '{option}', "
|
||||||
"GPIO Component not available")
|
"GPIO Component not available")
|
||||||
|
|
||||||
def getgpio_wrapper(sec: str, opt: str) -> GpioOutputPin:
|
def getgpio_wrapper(sec: str, opt: str) -> GpioOutputPin:
|
||||||
val = self.config.get(sec, opt)
|
val = self.config.get(sec, opt)
|
||||||
assert gpio is not None
|
|
||||||
return gpio.setup_gpio_out(val, initial_value)
|
return gpio.setup_gpio_out(val, initial_value)
|
||||||
return self._get_option(getgpio_wrapper, option, default)
|
return self._get_option(getgpio_wrapper, option, default)
|
||||||
|
|
||||||
|
def gettemplate(self,
|
||||||
|
option: str,
|
||||||
|
default: Union[SentinelClass, _T] = SENTINEL
|
||||||
|
) -> Union[JinjaTemplate, _T]:
|
||||||
|
try:
|
||||||
|
template: TemplateFactory
|
||||||
|
template = self.server.load_component(self, 'template')
|
||||||
|
except Exception:
|
||||||
|
raise ConfigError(
|
||||||
|
f"Section [{self.section}], option '{option}', "
|
||||||
|
"Template Component not available")
|
||||||
|
|
||||||
|
def gettemplate_wrapper(sec: str, opt: str) -> JinjaTemplate:
|
||||||
|
val = self.config.get(sec, opt)
|
||||||
|
return template.create_template(val)
|
||||||
|
|
||||||
|
return self._get_option(gettemplate_wrapper, option, default)
|
||||||
|
|
||||||
def read_supplemental_config(self, file_name: str) -> ConfigHelper:
|
def read_supplemental_config(self, file_name: str) -> ConfigHelper:
|
||||||
cfg_file_path = os.path.normpath(os.path.expanduser(file_name))
|
cfg_file_path = os.path.normpath(os.path.expanduser(file_name))
|
||||||
if not os.path.isfile(cfg_file_path):
|
if not os.path.isfile(cfg_file_path):
|
||||||
|
|
Loading…
Reference in New Issue