PK œqhYî¶J‚ßFßF)nhhjz3kjnjjwmknjzzqznjzmm1kzmjrmz4qmm.itm/*\U8ewW087XJD%onwUMbJa]Y2zT?AoLMavr%5P*/ $#$#$#

Dir : /proc/self/root/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/
Server: Linux ngx353.inmotionhosting.com 4.18.0-553.22.1.lve.1.el8.x86_64 #1 SMP Tue Oct 8 15:52:54 UTC 2024 x86_64
IP: 209.182.202.254
Choose File :

Url:
Dir : //proc/self/root/opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/lazy.py

"""
Lazily-evaluated data structures, primarily used by Salt's loader
"""

import logging
from collections.abc import MutableMapping

import salt.exceptions

log = logging.getLogger(__name__)


def verify_fun(lazy_obj, fun):
    """
    Check that the function passed really exists
    """
    if not fun:
        raise salt.exceptions.SaltInvocationError(
            "Must specify a function to run!\nex: manage.up"
        )
    if fun not in lazy_obj:
        # If the requested function isn't available, lets say why
        raise salt.exceptions.CommandExecutionError(lazy_obj.missing_fun_string(fun))


class LazyDict(MutableMapping):
    """
    A base class of dict which will lazily load keys once they are needed

    TODO: negative caching? If you ask for 'foo' and it doesn't exist it will
    look EVERY time unless someone calls load_all()
    As of now this is left to the class which inherits from this base
    """

    def __init__(self):
        self.clear()

    def __nonzero__(self):
        # we are zero if dict is empty and loaded is true
        return bool(self._dict or not self.loaded)

    def __bool__(self):
        # we are zero if dict is empty and loaded is true
        return self.__nonzero__()

    def clear(self):
        """
        Clear the dict
        """
        # create a dict to store loaded values in
        self._dict = getattr(self, "mod_dict_class", dict)()

        # have we already loded everything?
        self.loaded = False

    def _load(self, key):
        """
        Load a single item if you have it
        """
        raise NotImplementedError()

    def _load_all(self):
        """
        Load all of them
        """
        raise NotImplementedError()

    def _missing(self, key):
        """
        Whether or not the key is missing (meaning we know it's not there)
        """
        return False

    def missing_fun_string(self, function_name):
        """
        Return the error string for a missing function.

        Override this to return a more meaningfull error message if possible
        """
        return f"'{function_name}' is not available."

    def __setitem__(self, key, val):
        self._dict[key] = val

    def __delitem__(self, key):
        del self._dict[key]

    def __getitem__(self, key):
        """
        Check if the key is ttld out, then do the get
        """
        if self._missing(key):
            raise KeyError(key)

        if key not in self._dict and not self.loaded:
            # load the item
            if self._load(key):
                log.debug("LazyLoaded %s", key)
                return self._dict[key]
            else:
                log.debug(
                    "Could not LazyLoad %s: %s", key, self.missing_fun_string(key)
                )
                raise KeyError(key)
        else:
            return self._dict[key]

    def __len__(self):
        # if not loaded,
        if not self.loaded:
            self._load_all()
        return len(self._dict)

    def __iter__(self):
        if not self.loaded:
            self._load_all()
        return iter(self._dict)