<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import , print_function <EOL> import os <EOL> import copy <EOL> import math <EOL> import random <EOL> import logging <EOL> import operator <EOL> import collections <EOL> import json <EOL> from functools import reduce <EOL> import salt . utils . compat <EOL> from salt . utils . odict import OrderedDict <EOL> import yaml <EOL> import salt . ext . six as six <EOL> from salt . ext . six . moves import range <EOL> import salt . utils <EOL> import salt . utils . dictupdate <EOL> from salt . defaults import DEFAULT_TARGET_DELIM <EOL> from salt . exceptions import SaltException <EOL> __proxyenabled__ = [ '<STR_LIT:*>' ] <EOL> __grains__ = { } <EOL> __outputter__ = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> _infinitedict = lambda : collections . defaultdict ( _infinitedict ) <EOL> _non_existent_key = '<STR_LIT>' <EOL> log = logging . getLogger ( __name__ ) <EOL> def _serial_sanitizer ( instr ) : <EOL> '''<STR_LIT>''' <EOL> length = len ( instr ) <EOL> index = int ( math . floor ( length * <NUM_LIT> ) ) <EOL> return '<STR_LIT>' . format ( instr [ : index ] , '<STR_LIT:X>' * ( length - index ) ) <EOL> _FQDN_SANITIZER = lambda x : '<STR_LIT>' <EOL> _HOSTNAME_SANITIZER = lambda x : '<STR_LIT>' <EOL> _DOMAINNAME_SANITIZER = lambda x : '<STR_LIT>' <EOL> _SANITIZERS = { <EOL> '<STR_LIT>' : _serial_sanitizer , <EOL> '<STR_LIT>' : _DOMAINNAME_SANITIZER , <EOL> '<STR_LIT>' : _FQDN_SANITIZER , <EOL> '<STR_LIT:id>' : _FQDN_SANITIZER , <EOL> '<STR_LIT:host>' : _HOSTNAME_SANITIZER , <EOL> '<STR_LIT:localhost>' : _HOSTNAME_SANITIZER , <EOL> '<STR_LIT>' : _HOSTNAME_SANITIZER , <EOL> } <EOL> def get ( key , default = '<STR_LIT>' , delimiter = DEFAULT_TARGET_DELIM , ordered = True ) : <EOL> '''<STR_LIT>''' <EOL> if ordered is True : <EOL> grains = __grains__ <EOL> else : <EOL> grains = json . loads ( json . dumps ( __grains__ ) ) <EOL> return salt . utils . traverse_dict_and_list ( grains , <EOL> key , <EOL> default , <EOL> delimiter ) <EOL> def has_value ( key ) : <EOL> '''<STR_LIT>''' <EOL> return True if salt . utils . traverse_dict_and_list ( __grains__ , key , False ) else False <EOL> def items ( sanitize = False ) : <EOL> '''<STR_LIT>''' <EOL> if salt . utils . is_true ( sanitize ) : <EOL> out = dict ( __grains__ ) <EOL> for key , func in six . iteritems ( _SANITIZERS ) : <EOL> if key in out : <EOL> out [ key ] = func ( out [ key ] ) <EOL> return out <EOL> else : <EOL> return __grains__ <EOL> def item ( * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> default = kwargs . get ( '<STR_LIT:default>' , '<STR_LIT>' ) <EOL> delimiter = kwargs . get ( '<STR_LIT>' , DEFAULT_TARGET_DELIM ) <EOL> try : <EOL> for arg in args : <EOL> ret [ arg ] = salt . utils . traverse_dict_and_list ( __grains__ , <EOL> arg , <EOL> default , <EOL> delimiter ) <EOL> except KeyError : <EOL> pass <EOL> if salt . utils . is_true ( kwargs . get ( '<STR_LIT>' ) ) : <EOL> for arg , func in six . iteritems ( _SANITIZERS ) : <EOL> if arg in ret : <EOL> ret [ arg ] = func ( ret [ arg ] ) <EOL> return ret <EOL> def setvals ( grains , destructive = False ) : <EOL> '''<STR_LIT>''' <EOL> new_grains = grains <EOL> if not isinstance ( new_grains , collections . Mapping ) : <EOL> raise SaltException ( '<STR_LIT>' ) <EOL> grains = { } <EOL> if os . path . isfile ( __opts__ [ '<STR_LIT>' ] ) : <EOL> gfn = os . path . join ( <EOL> os . path . dirname ( __opts__ [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> elif os . path . isdir ( __opts__ [ '<STR_LIT>' ] ) : <EOL> gfn = os . path . join ( <EOL> __opts__ [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' <EOL> ) <EOL> else : <EOL> gfn = os . path . join ( <EOL> os . path . dirname ( __opts__ [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> if os . path . isfile ( gfn ) : <EOL> with salt . utils . fopen ( gfn , '<STR_LIT:rb>' ) as fp_ : <EOL> try : <EOL> grains = yaml . safe_load ( fp_ . read ( ) ) <EOL> except yaml . YAMLError as exc : <EOL> return '<STR_LIT>' . format ( exc ) <EOL> if not isinstance ( grains , dict ) : <EOL> grains = { } <EOL> for key , val in six . iteritems ( new_grains ) : <EOL> if val is None and destructive is True : <EOL> if key in grains : <EOL> del grains [ key ] <EOL> if key in __grains__ : <EOL> del __grains__ [ key ] <EOL> else : <EOL> grains [ key ] = val <EOL> __grains__ [ key ] = val <EOL> try : <EOL> yaml_reps = copy . deepcopy ( yaml . representer . SafeRepresenter . yaml_representers ) <EOL> yaml_multi_reps = copy . deepcopy ( yaml . representer . SafeRepresenter . yaml_multi_representers ) <EOL> except ( TypeError , NameError ) : <EOL> yaml_reps = salt . utils . compat . deepcopy_bound ( yaml . representer . SafeRepresenter . yaml_representers ) <EOL> yaml_multi_reps = salt . utils . compat . deepcopy_bound ( yaml . representer . SafeRepresenter . yaml_multi_representers ) <EOL> yaml . representer . SafeRepresenter . add_representer ( collections . defaultdict , <EOL> yaml . representer . SafeRepresenter . represent_dict ) <EOL> yaml . representer . SafeRepresenter . add_representer ( OrderedDict , <EOL> yaml . representer . SafeRepresenter . represent_dict ) <EOL> cstr = yaml . safe_dump ( grains , default_flow_style = False ) <EOL> yaml . representer . SafeRepresenter . yaml_representers = yaml_reps <EOL> yaml . representer . SafeRepresenter . yaml_multi_representers = yaml_multi_reps <EOL> try : <EOL> with salt . utils . fopen ( gfn , '<STR_LIT>' ) as fp_ : <EOL> fp_ . write ( cstr ) <EOL> except ( IOError , OSError ) : <EOL> msg = '<STR_LIT>' <EOL> log . error ( msg . format ( gfn ) ) <EOL> fn_ = os . path . join ( __opts__ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> try : <EOL> with salt . utils . flopen ( fn_ , '<STR_LIT>' ) as fp_ : <EOL> fp_ . write ( '<STR_LIT>' ) <EOL> except ( IOError , OSError ) : <EOL> msg = '<STR_LIT>' <EOL> log . error ( msg . format ( fn_ ) ) <EOL> if not __opts__ . get ( '<STR_LIT>' , False ) : <EOL> __salt__ [ '<STR_LIT>' ] ( ) <EOL> return new_grains <EOL> def setval ( key , val , destructive = False ) : <EOL> '''<STR_LIT>''' <EOL> return setvals ( { key : val } , destructive ) <EOL> def append ( key , val , convert = False , delimiter = DEFAULT_TARGET_DELIM ) : <EOL> '''<STR_LIT>''' <EOL> grains = get ( key , [ ] , delimiter ) <EOL> if not isinstance ( grains , list ) and convert is True : <EOL> grains = [ grains ] <EOL> if not isinstance ( grains , list ) : <EOL> return '<STR_LIT>' . format ( key ) <EOL> if val in grains : <EOL> return '<STR_LIT>' . format ( val , key ) <EOL> if isinstance ( val , list ) : <EOL> for item in val : <EOL> grains . append ( item ) <EOL> else : <EOL> grains . append ( val ) <EOL> while delimiter in key : <EOL> key , rest = key . rsplit ( delimiter , <NUM_LIT:1> ) <EOL> _grain = get ( key , _infinitedict ( ) , delimiter ) <EOL> if isinstance ( _grain , dict ) : <EOL> _grain . update ( { rest : grains } ) <EOL> grains = _grain <EOL> return setval ( key , grains ) <EOL> def remove ( key , val , delimiter = DEFAULT_TARGET_DELIM ) : <EOL> '''<STR_LIT>''' <EOL> grains = get ( key , [ ] , delimiter ) <EOL> if not isinstance ( grains , list ) : <EOL> return '<STR_LIT>' . format ( key ) <EOL> if val not in grains : <EOL> return '<STR_LIT>' . format ( val , key ) <EOL> grains . remove ( val ) <EOL> while delimiter in key : <EOL> key , rest = key . rsplit ( delimiter , <NUM_LIT:1> ) <EOL> _grain = get ( key , None , delimiter ) <EOL> if isinstance ( _grain , dict ) : <EOL> _grain . update ( { rest : grains } ) <EOL> grains = _grain <EOL> return setval ( key , grains ) <EOL> def delval ( key , destructive = False ) : <EOL> '''<STR_LIT>''' <EOL> setval ( key , None , destructive = destructive ) <EOL> def ls ( ) : <EOL> '''<STR_LIT>''' <EOL> return sorted ( __grains__ ) <EOL> def filter_by ( lookup_dict , grain = '<STR_LIT>' , merge = None , default = '<STR_LIT:default>' , base = None ) : <EOL> '''<STR_LIT>''' <EOL> ret = lookup_dict . get ( <EOL> salt . utils . traverse_dict_and_list ( __grains__ , grain , None ) , <EOL> lookup_dict . get ( <EOL> default , None ) <EOL> ) <EOL> if base and base in lookup_dict : <EOL> base_values = lookup_dict [ base ] <EOL> if ret is None : <EOL> ret = base_values <EOL> elif isinstance ( base_values , collections . Mapping ) : <EOL> if not isinstance ( ret , collections . Mapping ) : <EOL> raise SaltException ( '<STR_LIT>' ) <EOL> ret = salt . utils . dictupdate . update ( copy . deepcopy ( base_values ) , ret ) <EOL> if merge : <EOL> if not isinstance ( merge , collections . Mapping ) : <EOL> raise SaltException ( '<STR_LIT>' ) <EOL> if ret is None : <EOL> ret = merge <EOL> else : <EOL> salt . utils . dictupdate . update ( ret , copy . deepcopy ( merge ) ) <EOL> return ret <EOL> def _dict_from_path ( path , val , delimiter = DEFAULT_TARGET_DELIM ) : <EOL> '''<STR_LIT>''' <EOL> nested_dict = _infinitedict ( ) <EOL> keys = path . rsplit ( delimiter ) <EOL> lastplace = reduce ( operator . getitem , keys [ : - <NUM_LIT:1> ] , nested_dict ) <EOL> lastplace [ keys [ - <NUM_LIT:1> ] ] = val <EOL> return nested_dict <EOL> def get_or_set_hash ( name , <EOL> length = <NUM_LIT:8> , <EOL> chars = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> ret = get ( name , None ) <EOL> if ret is None : <EOL> val = '<STR_LIT>' . join ( [ random . SystemRandom ( ) . choice ( chars ) for _ in range ( length ) ] ) <EOL> if DEFAULT_TARGET_DELIM in name : <EOL> root , rest = name . split ( DEFAULT_TARGET_DELIM , <NUM_LIT:1> ) <EOL> curr = get ( root , _infinitedict ( ) ) <EOL> val = _dict_from_path ( rest , val ) <EOL> curr . update ( val ) <EOL> setval ( root , curr ) <EOL> else : <EOL> setval ( name , val ) <EOL> return get ( name ) <EOL> def set ( key , <EOL> val = '<STR_LIT>' , <EOL> force = False , <EOL> destructive = False , <EOL> delimiter = DEFAULT_TARGET_DELIM ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT:result>' : True } <EOL> _new_value_type = '<STR_LIT>' <EOL> if isinstance ( val , dict ) : <EOL> _new_value_type = '<STR_LIT>' <EOL> elif isinstance ( val , list ) : <EOL> _new_value_type = '<STR_LIT>' <EOL> _non_existent = object ( ) <EOL> _existing_value = get ( key , _non_existent , delimiter ) <EOL> _value = _existing_value <EOL> _existing_value_type = '<STR_LIT>' <EOL> if _existing_value is _non_existent : <EOL> _existing_value_type = None <EOL> elif isinstance ( _existing_value , dict ) : <EOL> _existing_value_type = '<STR_LIT>' <EOL> elif isinstance ( _existing_value , list ) : <EOL> _existing_value_type = '<STR_LIT>' <EOL> if _existing_value_type is not None and _existing_value == val and ( val is not None or destructive is not True ) : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> if _existing_value is not None and not force : <EOL> if _existing_value_type == '<STR_LIT>' : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( key ) + '<STR_LIT>' <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> elif _new_value_type == '<STR_LIT>' and _existing_value_type is not None : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( key ) + '<STR_LIT>' <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> else : <EOL> _value = val <EOL> else : <EOL> _value = val <EOL> while delimiter in key : <EOL> key , rest = key . rsplit ( delimiter , <NUM_LIT:1> ) <EOL> _existing_value = get ( key , { } , delimiter ) <EOL> if isinstance ( _existing_value , dict ) : <EOL> if _value is None and destructive : <EOL> if rest in _existing_value . keys ( ) : <EOL> _existing_value . pop ( rest ) <EOL> else : <EOL> _existing_value . update ( { rest : _value } ) <EOL> elif isinstance ( _existing_value , list ) : <EOL> _list_updated = False <EOL> for _index , _item in enumerate ( _existing_value ) : <EOL> if _item == rest : <EOL> _existing_value [ _index ] = { rest : _value } <EOL> _list_updated = True <EOL> elif isinstance ( _item , dict ) and rest in _item : <EOL> _item . update ( { rest : _value } ) <EOL> _list_updated = True <EOL> if not _list_updated : <EOL> _existing_value . append ( { rest : _value } ) <EOL> elif _existing_value == rest or force : <EOL> _existing_value = { rest : _value } <EOL> else : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( key , _existing_value ) + '<STR_LIT>' . format ( rest ) + '<STR_LIT>' <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> _value = _existing_value <EOL> _setval_ret = setval ( key , _value , destructive = destructive ) <EOL> if isinstance ( _setval_ret , dict ) : <EOL> ret [ '<STR_LIT>' ] = _setval_ret <EOL> else : <EOL> ret [ '<STR_LIT>' ] = _setval_ret <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> fetch = get </s>
