<s> import os <EOL> import re <EOL> import sys <EOL> from karesansui . lib . file . configfile import ConfigFile <EOL> from karesansui . lib . dict_op import DictOp <EOL> class shConfParser : <EOL> _delim = "<STR_LIT:=>" <EOL> _new_delim = "<STR_LIT:=>" <EOL> _comment = "<STR_LIT:#>" <EOL> _multidefine = False <EOL> _reserved_key_prefix = "<STR_LIT:@>" <EOL> _module = "<STR_LIT>" <EOL> def __init__ ( self , paths = [ ] ) : <EOL> self . dop = DictOp ( ) <EOL> self . dop . addconf ( self . _module , { } ) <EOL> self . set_source_file ( paths ) <EOL> def set_delim ( self , delim = "<STR_LIT:=>" ) : <EOL> self . _delim = delim <EOL> def set_new_delim ( self , delim = "<STR_LIT:=>" ) : <EOL> self . _new_delim = delim <EOL> def set_comment ( self , comment = "<STR_LIT:#>" ) : <EOL> self . _comment = comment <EOL> def set_multidefine ( self , multidefine = False ) : <EOL> self . _multidefine = multidefine <EOL> def set_reserved_key_prefix ( self , prefix = "<STR_LIT:@>" ) : <EOL> self . _reserved_key_prefix = prefix <EOL> def set_source_file ( self , paths = [ ] ) : <EOL> if type ( paths ) == str : <EOL> paths = [ paths ] <EOL> self . paths = paths <EOL> return True <EOL> def get_source_file ( self ) : <EOL> return self . paths <EOL> def source_file ( self ) : <EOL> return self . get_source_file ( ) <EOL> def read_conf ( self ) : <EOL> retval = { } <EOL> for _afile in self . source_file ( ) : <EOL> res = ConfigFile ( _afile ) . read ( ) <EOL> orders = [ ] <EOL> for _aline in res : <EOL> _aline = _aline . rstrip ( '<STR_LIT:\r\n>' ) <EOL> regex_str = "<STR_LIT>" % ( self . _comment , self . _delim , self . _delim , ) <EOL> regex = re . compile ( r"<STR_LIT:%s>" % regex_str ) <EOL> m = regex . match ( _aline ) <EOL> if m : <EOL> comment = m . group ( '<STR_LIT>' ) <EOL> key = m . group ( '<STR_LIT:key>' ) <EOL> value = m . group ( '<STR_LIT:value>' ) <EOL> if not value . rfind ( self . _comment ) == - <NUM_LIT:1> : <EOL> value = value [ : value . rfind ( self . _comment ) ] <EOL> if self . _multidefine and self . dop . isset ( self . _module , [ _afile , key ] ) is True : <EOL> new_value = self . dop . get ( self . _module , [ _afile , key ] ) <EOL> if type ( new_value ) == str : <EOL> new_value = [ new_value ] <EOL> if not value in new_value : <EOL> new_value . append ( value ) <EOL> else : <EOL> new_value = value <EOL> self . dop . set ( self . _module , [ _afile , key ] , new_value ) <EOL> if not key in orders : <EOL> orders . append ( key ) <EOL> if comment is not None : <EOL> self . dop . comment ( self . _module , [ _afile , key ] ) <EOL> orders_key = "<STR_LIT>" % ( self . _reserved_key_prefix , ) <EOL> self . dop . set ( self . _module , [ _afile , orders_key ] , orders ) <EOL> return self . dop . getconf ( self . _module ) <EOL> def _value_to_lines ( self , value ) : <EOL> lines = [ ] <EOL> for _k , _v in value . iteritems ( ) : <EOL> try : <EOL> if _v [ '<STR_LIT:action>' ] == "<STR_LIT>" : <EOL> continue <EOL> except : <EOL> pass <EOL> try : <EOL> val = _v [ '<STR_LIT:value>' ] <EOL> comment = False <EOL> try : <EOL> if _v [ '<STR_LIT>' ] is True : <EOL> comment = True <EOL> except : <EOL> pass <EOL> if type ( val ) == list : <EOL> for _val in val : <EOL> aline = "<STR_LIT>" % ( _k , self . _new_delim , _val , ) <EOL> if comment is True : <EOL> aline = "<STR_LIT>" % ( self . _comment , aline , ) <EOL> lines . append ( aline ) <EOL> else : <EOL> aline = "<STR_LIT>" % ( _k , self . _new_delim , val , ) <EOL> lines . append ( aline ) <EOL> except : <EOL> pass <EOL> return lines <EOL> def write_conf ( self , conf_arr = { } , dryrun = False ) : <EOL> retval = True <EOL> self . dop . addconf ( self . _module , conf_arr ) <EOL> orders_key = "<STR_LIT>" % ( self . _reserved_key_prefix , ) <EOL> for _path , _v in conf_arr . iteritems ( ) : <EOL> if _path [ <NUM_LIT:0> : <NUM_LIT:1> ] != "<STR_LIT:/>" : <EOL> continue <EOL> lines = [ ] <EOL> try : <EOL> _v [ '<STR_LIT:value>' ] <EOL> except : <EOL> continue <EOL> exclude_regex = "<STR_LIT>" % self . _reserved_key_prefix <EOL> if self . dop . isset ( self . _module , [ _path , orders_key ] ) is True : <EOL> for _k2 in self . dop . get ( self . _module , [ _path , orders_key ] ) : <EOL> m = re . match ( exclude_regex , _k2 ) <EOL> if not m : <EOL> try : <EOL> if type ( _k2 ) == list : <EOL> _k2 = _k2 . pop ( ) <EOL> value = { } <EOL> value [ _k2 ] = _v [ '<STR_LIT:value>' ] [ _k2 ] <EOL> lines = lines + self . _value_to_lines ( value ) <EOL> self . dop . unset ( self . _module , [ _path , _k2 ] ) <EOL> except : <EOL> pass <EOL> for _k2 , _v2 in self . dop . get ( self . _module , [ _path ] ) . iteritems ( ) : <EOL> m = re . match ( exclude_regex , _k2 ) <EOL> if not m : <EOL> try : <EOL> value = { } <EOL> value [ _k2 ] = _v2 <EOL> lines = lines + self . _value_to_lines ( value ) <EOL> except : <EOL> pass <EOL> if dryrun is False : <EOL> if len ( lines ) > <NUM_LIT:0> : <EOL> ConfigFile ( _path ) . write ( "<STR_LIT:\n>" . join ( lines ) + "<STR_LIT:\n>" ) <EOL> if len ( lines ) == <NUM_LIT:0> : <EOL> ConfigFile ( _path ) . write ( "<STR_LIT>" ) <EOL> else : <EOL> if len ( lines ) > <NUM_LIT:0> : <EOL> print "<STR_LIT:\n>" . join ( lines ) <EOL> if len ( lines ) == <NUM_LIT:0> : <EOL> print "<STR_LIT>" <EOL> return retval <EOL> """<STR_LIT:U+0020>""" <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> """<STR_LIT>""" <EOL> pass </s>
