mc_states_api / general API functions

exception mc_states.api.LockError


mc_states.api.cache_check(key, cache=None, memcache=None, __opts__=None, __salt__=None, *args, **kwargs)

Invalidate record in cache if expired

mc_states.api.filter_locals(reg, filter_list=None)

Filter a dictionnary feeded with all the local variables in a context.

We select what to remove depending on the original callee function (eg: {services, metadata, registry})

mc_states.api.get_cache_key(key, __opts__=None, *args, **kw)

Get an unique cache key from a key (string) candidate

This return a key in the force mcstates_api_cache_<SHA1SUM of args>

If the key is already a ‘unique cache key’, it wont be reprocessed

This cache key will be adapted to be unique per salt daemon if we have enougth information to gather from the __opts__ optional dictionnary.

If the key contains format ({0}, {name}) it will try to be formatted with additionnal arguments from kwargs:

func name
arg to func
kwarg for func


key = get_cache_key(mystring)
key = get_cache_key(_CACHE_PREFIX + '<some-sha1>')
key = get_cache_key('foo_{0}{1}{name}',
                    {'fun': '',
                     'arg': [1],
                     'kwarg': {'name': 'foobar'}})
mc_states.api.get_cache_servers(cache=None, memcache=None, key=None, use_memcache=None, debug=None)

Returns a list of usable caches

either a string to cache in a localthreaded memoize cache or a dict mutable where to stock the cache
memcache pool instance (can be used instead of default one)
if memcache is available, use it amongst local cache
  • if cache is set, we will not use memcache either first
    but may fallback on it in case of error
  • if cache is not set, we will use memcache
  • unless use_memcache is True where absence of local memcached server results in an exception, we always fallback on local python dictionnary cache
mc_states.api.lazy_subregistry_get(__salt__, registry)
  1. lazy load registries by calling them and then use memoize caching on them for 5 minutes.
  2. remove problematic variables from the registries like the salt dictionnary

Convert any string to UTF-8 ENCODED one

mc_states.api.memoize_cache(func, args=None, kwargs=None, key='cache_key_{0}', seconds=60, cache=None, memcache=None, use_memcache=None, __salt__=None, __opts__=None, force_run=False, debug=None)

Memoize the func in the cache in the key ‘key’ and store the cached time in ‘cache_key’ for further check of stale cache

if force_run is set, we will uncondionnaly run. EG:

>>> def serial_for(domain,
...                serials=None,
...                serial=None,
...                autoinc=True):
...     def _do(domain):
...         serial = int(
...                         '%Y%m%d01'))
...         return db_serial
...     cache_key = 'dnsserials_t_{0}'.format(domain)
...     return memoize_cache(
...         _do, [domain], {}, cache_key, 60)
mc_states.api.msplitstrip(mapping, keys=None)

Make the defined keys values stripped of their empty lines





mc_states.api.remove_entry(cache, key, *args, **kw)

Retro compat wrapper


Strip empty lines


uniquify a list