Skip to content

File: Broken/Loaders/LoaderString.py

Broken.Loaders.LoaderString

LoaderString

Bases: BrokenLoader

Source code in Broken/Loaders/LoaderString.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@define
class LoaderString(BrokenLoader):

    @staticmethod
    def load(value: Any=None, **kwargs) -> Optional[str]:
        if not value:
            return ""

        if isinstance(value, str):
            log.debug(f"Loading String from String {value}")
            return value

        if isinstance(value, bytes):
            log.debug("Loading String from Bytes")
            return value.decode(encoding="utf-8")

        if (path := BrokenPath.get(value)).exists():
            log.debug(f"Loading String from Path ({path})")
            return path.read_text(encoding="utf-8")

        return None

load

1
load(value: Any = None, **kwargs) -> Optional[str]
Source code in Broken/Loaders/LoaderString.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@staticmethod
def load(value: Any=None, **kwargs) -> Optional[str]:
    if not value:
        return ""

    if isinstance(value, str):
        log.debug(f"Loading String from String {value}")
        return value

    if isinstance(value, bytes):
        log.debug("Loading String from Bytes")
        return value.decode(encoding="utf-8")

    if (path := BrokenPath.get(value)).exists():
        log.debug(f"Loading String from Path ({path})")
        return path.read_text(encoding="utf-8")

    return None

LoadableString

1
LoadableString: TypeAlias = Union[str, bytes, Path, None]