403Webshell
Server IP : 198.38.94.13  /  Your IP : 216.73.217.33
Web Server : Apache
System : Linux d4744.dxb1.stableserver.net 5.14.0-611.49.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Apr 21 16:39:08 EDT 2026 x86_64
User : revivere ( 1140)
PHP Version : 8.2.33
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /opt/saltstack/salt/lib/python3.10/site-packages/salt/utils/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /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)

Youez - 2016 - github.com/yon3zu
LinuXploit