<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import os <EOL> import salt . utils <EOL> from salt . exceptions import CommandExecutionError <EOL> __virtualname__ = '<STR_LIT>' <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if __grains__ [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> return __virtualname__ <EOL> return ( False , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def show ( config_file = False ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' <EOL> ret = { } <EOL> out = __salt__ [ '<STR_LIT>' ] ( cmd , output_loglevel = '<STR_LIT>' ) <EOL> for line in out . splitlines ( ) : <EOL> if not line or '<STR_LIT:=>' not in line : <EOL> continue <EOL> comps = line . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> ret [ comps [ <NUM_LIT:0> ] ] = comps [ <NUM_LIT:1> ] <EOL> return ret <EOL> def get ( name ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' . format ( name ) <EOL> out = __salt__ [ '<STR_LIT>' ] ( cmd ) <EOL> return out <EOL> def assign ( name , value ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> cmd = '<STR_LIT>' . format ( name , value ) <EOL> data = __salt__ [ '<STR_LIT>' ] ( cmd ) <EOL> if data [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> raise CommandExecutionError ( '<STR_LIT>' . format ( <EOL> data [ '<STR_LIT>' ] ) ) <EOL> new_name , new_value = data [ '<STR_LIT>' ] . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> ret [ new_name ] = new_value . split ( '<STR_LIT>' ) [ - <NUM_LIT:1> ] <EOL> return ret <EOL> def persist ( name , value , config = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> nlines = [ ] <EOL> edited = False <EOL> value = str ( value ) <EOL> if not os . path . isfile ( config ) : <EOL> try : <EOL> salt . utils . fopen ( config , '<STR_LIT>' ) . close ( ) <EOL> except ( IOError , OSError ) : <EOL> msg = '<STR_LIT>' <EOL> raise CommandExecutionError ( msg . format ( config ) ) <EOL> with salt . utils . fopen ( config , '<STR_LIT:r>' ) as ifile : <EOL> for line in ifile : <EOL> if not line . startswith ( '<STR_LIT>' . format ( name ) ) : <EOL> nlines . append ( line ) <EOL> continue <EOL> else : <EOL> key , rest = line . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if rest . startswith ( '<STR_LIT:">' ) : <EOL> _ , rest_v , rest = rest . split ( '<STR_LIT:">' , <NUM_LIT:2> ) <EOL> elif rest . startswith ( '<STR_LIT>' ) : <EOL> _ , rest_v , rest = rest . split ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> else : <EOL> rest_v = rest . split ( ) [ <NUM_LIT:0> ] <EOL> rest = rest [ len ( rest_v ) : ] <EOL> if rest_v == value : <EOL> return '<STR_LIT>' <EOL> new_line = '<STR_LIT>' . format ( key , value , rest ) <EOL> nlines . append ( new_line ) <EOL> edited = True <EOL> if not edited : <EOL> nlines . append ( '<STR_LIT>' . format ( name , value ) ) <EOL> with salt . utils . fopen ( config , '<STR_LIT>' ) as ofile : <EOL> ofile . writelines ( nlines ) <EOL> assign ( name , value ) <EOL> return '<STR_LIT>' </s>
