<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import re <EOL> import logging <EOL> import salt . utils <EOL> SWWS = re . compile ( r'<STR_LIT>' ) <EOL> log = logging . getLogger ( __name__ ) <EOL> MAIN_CF = '<STR_LIT>' <EOL> MASTER_CF = '<STR_LIT>' <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if salt . utils . which ( '<STR_LIT>' ) : <EOL> return True <EOL> return ( False , '<STR_LIT>' ) <EOL> def _parse_master ( path = MASTER_CF ) : <EOL> '''<STR_LIT>''' <EOL> with salt . utils . fopen ( path , '<STR_LIT:r>' ) as fh_ : <EOL> full_conf = fh_ . read ( ) <EOL> conf_list = [ ] <EOL> conf_dict = { } <EOL> for line in full_conf . splitlines ( ) : <EOL> if not line . strip ( ) or line . strip ( ) . startswith ( '<STR_LIT:#>' ) : <EOL> conf_list . append ( line ) <EOL> continue <EOL> comps = line . strip ( ) . split ( ) <EOL> conf_line = { <EOL> '<STR_LIT>' : comps [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : comps [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : comps [ <NUM_LIT:2> ] , <EOL> '<STR_LIT>' : comps [ <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : comps [ <NUM_LIT:4> ] , <EOL> '<STR_LIT>' : comps [ <NUM_LIT:5> ] , <EOL> '<STR_LIT>' : comps [ <NUM_LIT:6> ] , <EOL> '<STR_LIT>' : '<STR_LIT:U+0020>' . join ( comps [ <NUM_LIT:7> : ] ) , <EOL> } <EOL> dict_key = '<STR_LIT>' . format ( comps [ <NUM_LIT:0> ] , comps [ <NUM_LIT:1> ] ) <EOL> conf_list . append ( conf_line ) <EOL> conf_dict [ dict_key ] = conf_line <EOL> return conf_dict , conf_list <EOL> def show_master ( path = MASTER_CF ) : <EOL> '''<STR_LIT>''' <EOL> conf_dict , conf_list = _parse_master ( path ) <EOL> return conf_dict <EOL> def set_master ( service , <EOL> conn_type , <EOL> private = '<STR_LIT:y>' , <EOL> unpriv = '<STR_LIT:y>' , <EOL> chroot = '<STR_LIT:y>' , <EOL> wakeup = '<STR_LIT:n>' , <EOL> maxproc = '<STR_LIT:100>' , <EOL> command = '<STR_LIT>' , <EOL> write_conf = True , <EOL> path = MASTER_CF ) : <EOL> '''<STR_LIT>''' <EOL> conf_dict , conf_list = _parse_master ( path ) <EOL> new_conf = [ ] <EOL> dict_key = '<STR_LIT>' . format ( service , conn_type ) <EOL> new_line = _format_master ( <EOL> service , <EOL> conn_type , <EOL> private , <EOL> unpriv , <EOL> chroot , <EOL> wakeup , <EOL> maxproc , <EOL> command , <EOL> ) <EOL> for line in conf_list : <EOL> if isinstance ( line , dict ) : <EOL> if line [ '<STR_LIT>' ] == service and line [ '<STR_LIT>' ] == conn_type : <EOL> new_conf . append ( new_line ) <EOL> else : <EOL> new_conf . append ( _format_master ( ** line ) ) <EOL> else : <EOL> new_conf . append ( line ) <EOL> if dict_key not in conf_dict : <EOL> new_conf . append ( new_line ) <EOL> if write_conf : <EOL> _write_conf ( new_conf , path ) <EOL> return '<STR_LIT:\n>' . join ( new_conf ) <EOL> def _format_master ( service , <EOL> conn_type , <EOL> private , <EOL> unpriv , <EOL> chroot , <EOL> wakeup , <EOL> maxproc , <EOL> command ) : <EOL> '''<STR_LIT>''' <EOL> if private == '<STR_LIT:y>' : <EOL> private = '<STR_LIT:->' <EOL> if unpriv == '<STR_LIT:y>' : <EOL> unpriv = '<STR_LIT:->' <EOL> if chroot == '<STR_LIT:y>' : <EOL> chroot = '<STR_LIT:->' <EOL> if wakeup == '<STR_LIT:n>' : <EOL> wakeup = '<STR_LIT:->' <EOL> maxproc = str ( maxproc ) <EOL> if maxproc == '<STR_LIT:100>' : <EOL> maxproc = '<STR_LIT:->' <EOL> conf_line = '<STR_LIT>' . format ( <EOL> service , <EOL> conn_type , <EOL> private , <EOL> unpriv , <EOL> chroot , <EOL> wakeup , <EOL> maxproc , <EOL> command , <EOL> ) <EOL> return conf_line <EOL> def _parse_main ( path = MAIN_CF ) : <EOL> '''<STR_LIT>''' <EOL> with salt . utils . fopen ( path , '<STR_LIT:r>' ) as fh_ : <EOL> full_conf = fh_ . read ( ) <EOL> conf_list = [ ] <EOL> for line in full_conf . splitlines ( ) : <EOL> if not line . strip ( ) : <EOL> conf_list . append ( line ) <EOL> continue <EOL> if re . match ( SWWS , line ) : <EOL> if not conf_list : <EOL> conf_list . append ( line ) <EOL> continue <EOL> if not isinstance ( conf_list [ - <NUM_LIT:1> ] , str ) : <EOL> conf_list [ - <NUM_LIT:1> ] = '<STR_LIT>' <EOL> conf_list [ - <NUM_LIT:1> ] = '<STR_LIT:\n>' . join ( [ conf_list [ - <NUM_LIT:1> ] , line ] ) <EOL> else : <EOL> conf_list . append ( line ) <EOL> pairs = { } <EOL> for line in conf_list : <EOL> if not line . strip ( ) : <EOL> continue <EOL> if line . startswith ( '<STR_LIT:#>' ) : <EOL> continue <EOL> comps = line . split ( '<STR_LIT:=>' ) <EOL> pairs [ comps [ <NUM_LIT:0> ] . strip ( ) ] = '<STR_LIT:=>' . join ( comps [ <NUM_LIT:1> : ] ) . strip ( ) <EOL> return pairs , conf_list <EOL> def show_main ( path = MAIN_CF ) : <EOL> '''<STR_LIT>''' <EOL> pairs , conf_list = _parse_main ( path ) <EOL> return pairs <EOL> def set_main ( key , value , path = MAIN_CF ) : <EOL> '''<STR_LIT>''' <EOL> pairs , conf_list = _parse_main ( path ) <EOL> new_conf = [ ] <EOL> if key in pairs : <EOL> for line in conf_list : <EOL> if line . startswith ( key ) : <EOL> new_conf . append ( '<STR_LIT>' . format ( key , value ) ) <EOL> else : <EOL> new_conf . append ( line ) <EOL> else : <EOL> conf_list . append ( '<STR_LIT>' . format ( key , value ) ) <EOL> new_conf = conf_list <EOL> _write_conf ( new_conf , path ) <EOL> return new_conf <EOL> def _write_conf ( conf , path = MAIN_CF ) : <EOL> '''<STR_LIT>''' <EOL> with salt . utils . fopen ( path , '<STR_LIT:w>' ) as fh_ : <EOL> for line in conf : <EOL> if isinstance ( line , dict ) : <EOL> fh_ . write ( '<STR_LIT:U+0020>' . join ( line ) ) <EOL> else : <EOL> fh_ . write ( line ) <EOL> fh_ . write ( '<STR_LIT:\n>' ) <EOL> def show_queue ( ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' <EOL> out = __salt__ [ '<STR_LIT>' ] ( cmd ) . splitlines ( ) <EOL> queue = [ ] <EOL> queue_pattern = re . compile ( r"<STR_LIT>" ) <EOL> recipient_pattern = re . compile ( r"<STR_LIT>" ) <EOL> for line in out : <EOL> if re . match ( '<STR_LIT>' , line ) : <EOL> continue <EOL> if re . match ( queue_pattern , line ) : <EOL> m = re . match ( queue_pattern , line ) <EOL> queue_id = m . group ( '<STR_LIT>' ) <EOL> size = m . group ( '<STR_LIT:size>' ) <EOL> timestamp = m . group ( '<STR_LIT>' ) <EOL> sender = m . group ( '<STR_LIT>' ) <EOL> elif re . match ( recipient_pattern , line ) : <EOL> m = re . match ( recipient_pattern , line ) <EOL> recipient = m . group ( '<STR_LIT>' ) <EOL> elif not line : <EOL> queue . append ( { '<STR_LIT>' : queue_id , '<STR_LIT:size>' : size , '<STR_LIT>' : timestamp , '<STR_LIT>' : sender , '<STR_LIT>' : recipient } ) <EOL> return queue <EOL> def delete ( queue_id ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:message>' : '<STR_LIT>' , <EOL> '<STR_LIT:result>' : True <EOL> } <EOL> if not queue_id : <EOL> log . error ( '<STR_LIT>' ) <EOL> if not queue_id == '<STR_LIT>' : <EOL> queue = show_queue ( ) <EOL> _message = None <EOL> for item in queue : <EOL> if item [ '<STR_LIT>' ] == queue_id : <EOL> _message = item <EOL> if not _message : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( queue_id ) <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> cmd = '<STR_LIT>' . format ( queue_id ) <EOL> result = __salt__ [ '<STR_LIT>' ] ( cmd ) <EOL> if result [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> if queue_id == '<STR_LIT>' : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> else : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( queue_id ) <EOL> else : <EOL> if queue_id == '<STR_LIT>' : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> else : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( queue_id , result [ '<STR_LIT>' ] ) <EOL> return ret <EOL> def hold ( queue_id ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:message>' : '<STR_LIT>' , <EOL> '<STR_LIT:result>' : True <EOL> } <EOL> if not queue_id : <EOL> log . error ( '<STR_LIT>' ) <EOL> if not queue_id == '<STR_LIT>' : <EOL> queue = show_queue ( ) <EOL> _message = None <EOL> for item in queue : <EOL> if item [ '<STR_LIT>' ] == queue_id : <EOL> _message = item <EOL> if not _message : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( queue_id ) <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> cmd = '<STR_LIT>' . format ( queue_id ) <EOL> result = __salt__ [ '<STR_LIT>' ] ( cmd ) <EOL> if result [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> if queue_id == '<STR_LIT>' : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> else : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( queue_id ) <EOL> else : <EOL> if queue_id == '<STR_LIT>' : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> else : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( queue_id , result [ '<STR_LIT>' ] ) <EOL> return ret <EOL> def unhold ( queue_id ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:message>' : '<STR_LIT>' , <EOL> '<STR_LIT:result>' : True <EOL> } <EOL> if not queue_id : <EOL> log . error ( '<STR_LIT>' ) <EOL> if not queue_id == '<STR_LIT>' : <EOL> queue = show_queue ( ) <EOL> _message = None <EOL> for item in queue : <EOL> if item [ '<STR_LIT>' ] == queue_id : <EOL> _message = item <EOL> if not _message : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( queue_id ) <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> cmd = '<STR_LIT>' . format ( queue_id ) <EOL> result = __salt__ [ '<STR_LIT>' ] ( cmd ) <EOL> if result [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> if queue_id == '<STR_LIT>' : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> else : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( queue_id ) <EOL> else : <EOL> if queue_id == '<STR_LIT>' : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> else : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( queue_id , result [ '<STR_LIT>' ] ) <EOL> return ret <EOL> def requeue ( queue_id ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:message>' : '<STR_LIT>' , <EOL> '<STR_LIT:result>' : True <EOL> } <EOL> if not queue_id : <EOL> log . error ( '<STR_LIT>' ) <EOL> if not queue_id == '<STR_LIT>' : <EOL> queue = show_queue ( ) <EOL> _message = None <EOL> for item in queue : <EOL> if item [ '<STR_LIT>' ] == queue_id : <EOL> _message = item <EOL> if not _message : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( queue_id ) <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> cmd = '<STR_LIT>' . format ( queue_id ) <EOL> result = __salt__ [ '<STR_LIT>' ] ( cmd ) <EOL> if result [ '<STR_LIT>' ] == <NUM_LIT:0> : <EOL> if queue_id == '<STR_LIT>' : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> else : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( queue_id ) <EOL> else : <EOL> if queue_id == '<STR_LIT>' : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' <EOL> else : <EOL> ret [ '<STR_LIT:message>' ] = '<STR_LIT>' . format ( queue_id , result [ '<STR_LIT>' ] ) <EOL> return ret </s>
