<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import logging <EOL> from salt . exceptions import CommandExecutionError , SaltInvocationError <EOL> from salt . ext . six import integer_types <EOL> try : <EOL> import memcache <EOL> HAS_LIBS = True <EOL> except ImportError : <EOL> HAS_LIBS = False <EOL> DEFAULT_HOST = '<STR_LIT:127.0.0.1>' <EOL> DEFAULT_PORT = <NUM_LIT> <EOL> DEFAULT_TIME = <NUM_LIT:0> <EOL> DEFAULT_MIN_COMPRESS_LEN = <NUM_LIT:0> <EOL> log = logging . getLogger ( __name__ ) <EOL> __func_alias__ = { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> return True if HAS_LIBS else False <EOL> def get_conn ( opts , profile = None , host = None , port = None ) : <EOL> '''<STR_LIT>''' <EOL> if not ( host and port ) : <EOL> opts_pillar = opts . get ( '<STR_LIT>' , { } ) <EOL> opts_master = opts_pillar . get ( '<STR_LIT>' , { } ) <EOL> opts_merged = { } <EOL> opts_merged . update ( opts_master ) <EOL> opts_merged . update ( opts_pillar ) <EOL> opts_merged . update ( opts ) <EOL> if profile : <EOL> conf = opts_merged . get ( profile , { } ) <EOL> else : <EOL> conf = opts_merged <EOL> host = conf . get ( '<STR_LIT>' , DEFAULT_HOST ) <EOL> port = conf . get ( '<STR_LIT>' , DEFAULT_PORT ) <EOL> if not str ( port ) . isdigit ( ) : <EOL> raise SaltInvocationError ( '<STR_LIT>' ) <EOL> if HAS_LIBS : <EOL> return memcache . Client ( [ '<STR_LIT>' . format ( host , port ) ] ) <EOL> else : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> def _check_stats ( conn ) : <EOL> '''<STR_LIT>''' <EOL> stats = conn . get_stats ( ) <EOL> if not stats : <EOL> raise CommandExecutionError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> return stats <EOL> def set_ ( conn , <EOL> key , <EOL> value , <EOL> time = DEFAULT_TIME , <EOL> min_compress_len = DEFAULT_MIN_COMPRESS_LEN ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( time , integer_types ) : <EOL> raise SaltInvocationError ( '<STR_LIT>' ) <EOL> if not isinstance ( min_compress_len , integer_types ) : <EOL> raise SaltInvocationError ( '<STR_LIT>' ) <EOL> _check_stats ( conn ) <EOL> return conn . set ( key , value , time , min_compress_len ) <EOL> def get ( conn , key ) : <EOL> '''<STR_LIT>''' <EOL> _check_stats ( conn ) <EOL> return conn . get ( key ) </s>
