<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import difflib <EOL> import os <EOL> import yaml <EOL> import salt . utils <EOL> from salt . ext . six . moves import map <EOL> import logging <EOL> log = logging . getLogger ( __name__ ) <EOL> __func_alias__ = { <EOL> '<STR_LIT>' : '<STR_LIT:list>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def list_ ( return_yaml = True ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> eventer = salt . utils . event . get_event ( '<STR_LIT>' , opts = __opts__ ) <EOL> res = __salt__ [ '<STR_LIT>' ] ( { '<STR_LIT>' : '<STR_LIT:list>' } , '<STR_LIT>' ) <EOL> if res : <EOL> event_ret = eventer . get_event ( tag = '<STR_LIT>' , wait = <NUM_LIT:30> ) <EOL> log . debug ( '<STR_LIT>' . format ( event_ret ) ) <EOL> if event_ret and event_ret [ '<STR_LIT>' ] : <EOL> beacons = event_ret [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> ret = { } <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> if beacons : <EOL> if return_yaml : <EOL> tmp = { '<STR_LIT>' : beacons } <EOL> yaml_out = yaml . safe_dump ( tmp , default_flow_style = False ) <EOL> return yaml_out <EOL> else : <EOL> return beacons <EOL> else : <EOL> return { '<STR_LIT>' : { } } <EOL> def add ( name , beacon_data , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT>' : '<STR_LIT>' . format ( name ) , <EOL> '<STR_LIT:result>' : False } <EOL> if name in list_ ( return_yaml = False ) : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> return ret <EOL> if '<STR_LIT:test>' in kwargs and kwargs [ '<STR_LIT:test>' ] : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> else : <EOL> try : <EOL> beacon_module = __import__ ( '<STR_LIT>' + name , fromlist = [ '<STR_LIT>' ] ) <EOL> log . debug ( '<STR_LIT>' ) <EOL> except ImportError : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> return ret <EOL> if hasattr ( beacon_module , '<STR_LIT>' ) : <EOL> _beacon_data = beacon_data <EOL> if '<STR_LIT>' in _beacon_data : <EOL> del _beacon_data [ '<STR_LIT>' ] <EOL> valid , vcomment = beacon_module . validate ( _beacon_data ) <EOL> else : <EOL> log . info ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( name ) ) <EOL> valid = True <EOL> if not valid : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( name , vcomment ) ) <EOL> return ret <EOL> try : <EOL> eventer = salt . utils . event . get_event ( '<STR_LIT>' , opts = __opts__ ) <EOL> res = __salt__ [ '<STR_LIT>' ] ( { '<STR_LIT:name>' : name , '<STR_LIT>' : beacon_data , '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> if res : <EOL> event_ret = eventer . get_event ( tag = '<STR_LIT>' , wait = <NUM_LIT:30> ) <EOL> if event_ret and event_ret [ '<STR_LIT>' ] : <EOL> beacons = event_ret [ '<STR_LIT>' ] <EOL> if name in beacons and beacons [ name ] == beacon_data : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> return ret <EOL> except KeyError : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> def modify ( name , beacon_data , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:result>' : True } <EOL> current_beacons = list_ ( return_yaml = False ) <EOL> if name not in current_beacons : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> return ret <EOL> if '<STR_LIT:test>' in kwargs and kwargs [ '<STR_LIT:test>' ] : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> else : <EOL> try : <EOL> beacon_module = __import__ ( '<STR_LIT>' + name , fromlist = [ '<STR_LIT>' ] ) <EOL> log . debug ( '<STR_LIT>' ) <EOL> except ImportError : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> return ret <EOL> if hasattr ( beacon_module , '<STR_LIT>' ) : <EOL> _beacon_data = beacon_data <EOL> if '<STR_LIT>' in _beacon_data : <EOL> del _beacon_data [ '<STR_LIT>' ] <EOL> valid , vcomment = beacon_module . validate ( _beacon_data ) <EOL> else : <EOL> log . info ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( name ) ) <EOL> valid = True <EOL> if not valid : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( name , vcomment ) ) <EOL> return ret <EOL> _current = current_beacons [ name ] <EOL> _new = beacon_data <EOL> if _new == _current : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> return ret <EOL> _current_lines = [ '<STR_LIT>' . format ( key , value ) <EOL> for ( key , value ) in sorted ( _current . items ( ) ) ] <EOL> _new_lines = [ '<STR_LIT>' . format ( key , value ) <EOL> for ( key , value ) in sorted ( _new . items ( ) ) ] <EOL> _diff = difflib . unified_diff ( _current_lines , _new_lines ) <EOL> ret [ '<STR_LIT>' ] = { } <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' . join ( _diff ) <EOL> try : <EOL> eventer = salt . utils . event . get_event ( '<STR_LIT>' , opts = __opts__ ) <EOL> res = __salt__ [ '<STR_LIT>' ] ( { '<STR_LIT:name>' : name , '<STR_LIT>' : beacon_data , '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> if res : <EOL> event_ret = eventer . get_event ( tag = '<STR_LIT>' , wait = <NUM_LIT:30> ) <EOL> if event_ret and event_ret [ '<STR_LIT>' ] : <EOL> beacons = event_ret [ '<STR_LIT>' ] <EOL> if name in beacons and beacons [ name ] == beacon_data : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> return ret <EOL> except KeyError : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> def delete ( name , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT>' : '<STR_LIT>' . format ( name ) , <EOL> '<STR_LIT:result>' : False } <EOL> if '<STR_LIT:test>' in kwargs and kwargs [ '<STR_LIT:test>' ] : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> else : <EOL> try : <EOL> eventer = salt . utils . event . get_event ( '<STR_LIT>' , opts = __opts__ ) <EOL> res = __salt__ [ '<STR_LIT>' ] ( { '<STR_LIT:name>' : name , '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> if res : <EOL> event_ret = eventer . get_event ( tag = '<STR_LIT>' , wait = <NUM_LIT:30> ) <EOL> if event_ret and event_ret [ '<STR_LIT>' ] : <EOL> beacons = event_ret [ '<STR_LIT>' ] <EOL> if name not in beacons : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> return ret <EOL> except KeyError : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> def save ( ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:result>' : True } <EOL> beacons = list_ ( return_yaml = False ) <EOL> sfn = '<STR_LIT>' . format ( __opts__ [ '<STR_LIT>' ] , <EOL> os . path . dirname ( __opts__ [ '<STR_LIT>' ] ) ) <EOL> if beacons : <EOL> tmp = { '<STR_LIT>' : beacons } <EOL> yaml_out = yaml . safe_dump ( tmp , default_flow_style = False ) <EOL> else : <EOL> yaml_out = '<STR_LIT>' <EOL> try : <EOL> with salt . utils . fopen ( sfn , '<STR_LIT>' ) as fp_ : <EOL> fp_ . write ( yaml_out ) <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( sfn ) <EOL> except ( IOError , OSError ) : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( sfn ) <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> def enable ( ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:result>' : True } <EOL> if '<STR_LIT:test>' in kwargs and kwargs [ '<STR_LIT:test>' ] : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> try : <EOL> eventer = salt . utils . event . get_event ( '<STR_LIT>' , opts = __opts__ ) <EOL> res = __salt__ [ '<STR_LIT>' ] ( { '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> if res : <EOL> event_ret = eventer . get_event ( tag = '<STR_LIT>' , wait = <NUM_LIT:30> ) <EOL> if event_ret and event_ret [ '<STR_LIT>' ] : <EOL> beacons = event_ret [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in beacons and beacons [ '<STR_LIT>' ] : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> except KeyError : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> def disable ( ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:result>' : True } <EOL> if '<STR_LIT:test>' in kwargs and kwargs [ '<STR_LIT:test>' ] : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> try : <EOL> eventer = salt . utils . event . get_event ( '<STR_LIT>' , opts = __opts__ ) <EOL> res = __salt__ [ '<STR_LIT>' ] ( { '<STR_LIT>' : '<STR_LIT>' } , '<STR_LIT>' ) <EOL> if res : <EOL> event_ret = eventer . get_event ( tag = '<STR_LIT>' , wait = <NUM_LIT:30> ) <EOL> log . debug ( '<STR_LIT>' . format ( event_ret ) ) <EOL> if event_ret and event_ret [ '<STR_LIT>' ] : <EOL> beacons = event_ret [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in beacons and not beacons [ '<STR_LIT>' ] : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> except KeyError : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> def _get_beacon_config_dict ( beacon_config ) : <EOL> beacon_config_dict = { } <EOL> if isinstance ( beacon_config , list ) : <EOL> list ( map ( beacon_config_dict . update , beacon_config ) ) <EOL> else : <EOL> beacon_config_dict = beacon_config <EOL> return beacon_config_dict <EOL> def enable_beacon ( name , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:result>' : True } <EOL> if not name : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> if '<STR_LIT:test>' in kwargs and kwargs [ '<STR_LIT:test>' ] : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> else : <EOL> _beacons = list_ ( return_yaml = False ) <EOL> if name not in _beacons : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> try : <EOL> eventer = salt . utils . event . get_event ( '<STR_LIT>' , opts = __opts__ ) <EOL> res = __salt__ [ '<STR_LIT>' ] ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:name>' : name } , '<STR_LIT>' ) <EOL> if res : <EOL> event_ret = eventer . get_event ( tag = '<STR_LIT>' , wait = <NUM_LIT:30> ) <EOL> if event_ret and event_ret [ '<STR_LIT>' ] : <EOL> beacons = event_ret [ '<STR_LIT>' ] <EOL> beacon_config_dict = _get_beacon_config_dict ( beacons [ name ] ) <EOL> if '<STR_LIT>' in beacon_config_dict and beacon_config_dict [ '<STR_LIT>' ] : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> return ret <EOL> except KeyError : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> def disable_beacon ( name , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:result>' : True } <EOL> if not name : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> if '<STR_LIT:test>' in kwargs and kwargs [ '<STR_LIT:test>' ] : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> _beacons = list_ ( return_yaml = False ) <EOL> if name not in _beacons : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> try : <EOL> eventer = salt . utils . event . get_event ( '<STR_LIT>' , opts = __opts__ ) <EOL> res = __salt__ [ '<STR_LIT>' ] ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:name>' : name } , '<STR_LIT>' ) <EOL> if res : <EOL> event_ret = eventer . get_event ( tag = '<STR_LIT>' , wait = <NUM_LIT:30> ) <EOL> if event_ret and event_ret [ '<STR_LIT>' ] : <EOL> beacons = event_ret [ '<STR_LIT>' ] <EOL> beacon_config_dict = _get_beacon_config_dict ( beacons [ name ] ) <EOL> if '<STR_LIT>' in beacon_config_dict and not beacon_config_dict [ '<STR_LIT>' ] : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name ) <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> except KeyError : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret </s>
