<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import logging <EOL> import salt . utils . jid <EOL> import salt . returners <EOL> import salt . ext . six as six <EOL> try : <EOL> import pymongo <EOL> version = pymongo . version <EOL> version = '<STR_LIT:.>' . join ( version . split ( '<STR_LIT:.>' ) [ : <NUM_LIT:2> ] ) <EOL> HAS_PYMONGO = True <EOL> except ImportError : <EOL> HAS_PYMONGO = False <EOL> log = logging . getLogger ( __name__ ) <EOL> __virtualname__ = '<STR_LIT>' <EOL> def __virtual__ ( ) : <EOL> if not HAS_PYMONGO : <EOL> return False <EOL> return '<STR_LIT>' <EOL> def _remove_dots ( src ) : <EOL> '''<STR_LIT>''' <EOL> output = { } <EOL> for key , val in six . iteritems ( src ) : <EOL> if isinstance ( val , dict ) : <EOL> val = _remove_dots ( val ) <EOL> output [ key . replace ( '<STR_LIT:.>' , '<STR_LIT:->' ) ] = val <EOL> return output <EOL> def _get_options ( ret ) : <EOL> '''<STR_LIT>''' <EOL> attrs = { '<STR_LIT:host>' : '<STR_LIT:host>' , <EOL> '<STR_LIT:port>' : '<STR_LIT:port>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:user>' : '<STR_LIT:user>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> _options = salt . returners . get_returner_options ( __virtualname__ , <EOL> ret , <EOL> attrs , <EOL> __salt__ = __salt__ , <EOL> __opts__ = __opts__ ) <EOL> return _options <EOL> def _get_conn ( ret ) : <EOL> '''<STR_LIT>''' <EOL> _options = _get_options ( ret ) <EOL> host = _options . get ( '<STR_LIT:host>' ) <EOL> port = _options . get ( '<STR_LIT:port>' ) <EOL> db_ = _options . get ( '<STR_LIT>' ) <EOL> user = _options . get ( '<STR_LIT:user>' ) <EOL> password = _options . get ( '<STR_LIT:password>' ) <EOL> indexes = _options . get ( '<STR_LIT>' , False ) <EOL> if float ( version ) > <NUM_LIT> : <EOL> conn = pymongo . MongoClient ( host , port ) <EOL> else : <EOL> conn = pymongo . Connection ( host , port ) <EOL> mdb = conn [ db_ ] <EOL> if user and password : <EOL> mdb . authenticate ( user , password ) <EOL> if indexes : <EOL> if float ( version ) > <NUM_LIT> : <EOL> mdb . saltReturns . create_index ( '<STR_LIT>' ) <EOL> mdb . saltReturns . create_index ( '<STR_LIT>' ) <EOL> mdb . jobs . create_index ( '<STR_LIT>' ) <EOL> else : <EOL> mdb . saltReturns . ensure_index ( '<STR_LIT>' ) <EOL> mdb . saltReturns . ensure_index ( '<STR_LIT>' ) <EOL> mdb . jobs . ensure_index ( '<STR_LIT>' ) <EOL> return conn , mdb <EOL> def returner ( ret ) : <EOL> '''<STR_LIT>''' <EOL> conn , mdb = _get_conn ( ret ) <EOL> col = mdb [ ret [ '<STR_LIT:id>' ] ] <EOL> if isinstance ( ret [ '<STR_LIT>' ] , dict ) : <EOL> back = _remove_dots ( ret [ '<STR_LIT>' ] ) <EOL> else : <EOL> back = ret [ '<STR_LIT>' ] <EOL> if isinstance ( ret , dict ) : <EOL> full_ret = _remove_dots ( ret ) <EOL> else : <EOL> full_ret = ret <EOL> log . debug ( back ) <EOL> sdata = { '<STR_LIT>' : ret [ '<STR_LIT:id>' ] , '<STR_LIT>' : ret [ '<STR_LIT>' ] , '<STR_LIT>' : back , '<STR_LIT>' : ret [ '<STR_LIT>' ] , '<STR_LIT>' : full_ret } <EOL> if '<STR_LIT>' in ret : <EOL> sdata [ '<STR_LIT>' ] = ret [ '<STR_LIT>' ] <EOL> if float ( version ) > <NUM_LIT> : <EOL> mdb . saltReturns . insert_one ( sdata . copy ( ) ) <EOL> else : <EOL> mdb . saltReturns . insert ( sdata . copy ( ) ) <EOL> def get_jid ( jid ) : <EOL> '''<STR_LIT>''' <EOL> conn , mdb = _get_conn ( ret = None ) <EOL> ret = { } <EOL> rdata = mdb . saltReturns . find ( { '<STR_LIT>' : jid } , { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> if rdata : <EOL> for data in rdata : <EOL> minion = data [ '<STR_LIT>' ] <EOL> ret [ minion ] = data [ '<STR_LIT>' ] <EOL> return ret <EOL> def get_fun ( fun ) : <EOL> '''<STR_LIT>''' <EOL> conn , mdb = _get_conn ( ret = None ) <EOL> ret = { } <EOL> rdata = mdb . saltReturns . find_one ( { '<STR_LIT>' : fun } , { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> if rdata : <EOL> ret = rdata <EOL> return ret <EOL> def prep_jid ( nocache = False , passed_jid = None ) : <EOL> '''<STR_LIT>''' <EOL> return passed_jid if passed_jid is not None else salt . utils . jid . gen_jid ( ) <EOL> def save_minions ( jid , minions ) : <EOL> '''<STR_LIT>''' <EOL> pass </s>
