confighelper: implement getpath

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2022-11-03 06:27:41 -04:00
parent e8dad1c8c0
commit 484950cb40
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 14 additions and 1 deletions

View File

@ -15,6 +15,7 @@ import copy
import logging import logging
from io import StringIO from io import StringIO
from utils import SentinelClass from utils import SentinelClass
from components.template import JinjaTemplate
# Annotation imports # Annotation imports
from typing import ( from typing import (
@ -35,7 +36,7 @@ from typing import (
if TYPE_CHECKING: if TYPE_CHECKING:
from moonraker import Server from moonraker import Server
from components.gpio import GpioFactory, GpioOutputPin from components.gpio import GpioFactory, GpioOutputPin
from components.template import TemplateFactory, JinjaTemplate from components.template import TemplateFactory
from io import TextIOWrapper from io import TextIOWrapper
_T = TypeVar("_T") _T = TypeVar("_T")
ConfigVal = Union[None, int, float, bool, str, dict, list] ConfigVal = Union[None, int, float, bool, str, dict, list]
@ -406,6 +407,18 @@ class ConfigHelper:
return template.create_template(val.strip(), is_async) return template.create_template(val.strip(), is_async)
return val return val
def getpath(self,
option: str,
default: Union[SentinelClass, _T] = SENTINEL,
deprecate: bool = False
) -> Union[pathlib.Path, _T]:
val = self.gettemplate(option, default, deprecate=deprecate)
if isinstance(val, JinjaTemplate):
ctx = {"data_path": self.server.get_app_args()["data_path"]}
strpath = val.render(ctx)
return pathlib.Path(strpath).expanduser().resolve()
return val
def read_supplemental_dict(self, obj: Dict[str, Any]) -> ConfigHelper: def read_supplemental_dict(self, obj: Dict[str, Any]) -> ConfigHelper:
if not obj: if not obj:
raise ConfigError(f"Cannot ready Empty Dict") raise ConfigError(f"Cannot ready Empty Dict")