feat: add docker preset
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from ...util import reflect
|
||||
import dotenv
|
||||
import tomli
|
||||
|
||||
from ..core import Pipe
|
||||
|
||||
__all__ = (
|
||||
@@ -11,31 +14,24 @@ __all__ = (
|
||||
|
||||
|
||||
class FileData:
|
||||
_LOADERS = {
|
||||
".toml": "tomli:load",
|
||||
".json": "json:load",
|
||||
}
|
||||
|
||||
def __init__(self, filepath: Path, loader: str | None = None):
|
||||
self._filepath = filepath
|
||||
self._loader = loader
|
||||
self._data = None
|
||||
|
||||
def _resolve_loader(self, filepath: Path):
|
||||
loader = self._loader or self._LOADERS.get(filepath.suffix)
|
||||
return reflect(loader) if loader else None
|
||||
|
||||
def _load_data(self):
|
||||
loader = self._resolve_loader(self._filepath)
|
||||
with open(self._filepath, "rb") as fd:
|
||||
if loader:
|
||||
self._data = loader(fd)
|
||||
else:
|
||||
self._data = fd.read()
|
||||
if self._filepath.suffix == ".toml":
|
||||
return tomli.loads(self._filepath.read_text())
|
||||
elif self._filepath.suffix == ".json":
|
||||
return json.loads(self._filepath.read_text())
|
||||
elif self._filepath.suffix == ".env" or self._filepath.name == ".env":
|
||||
return dotenv.dotenv_values(self._filepath)
|
||||
return self._filepath.read_text()
|
||||
|
||||
def get(self, key: str):
|
||||
if self._data is None and self._filepath.exists():
|
||||
self._load_data()
|
||||
self._data = self._load_data()
|
||||
if self._data:
|
||||
return self._data.get(key)
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user