<s> from __future__ import absolute_import , print_function , unicode_literals <EOL> import sys <EOL> from . data import CMS_VERSION_MATRIX , DJANGO_VERSION_MATRIX <EOL> try : <EOL> from configparser import ConfigParser <EOL> except ImportError : <EOL> from ConfigParser import ConfigParser <EOL> SECTION = '<STR_LIT>' <EOL> def parse_config_file ( parser , stdin_args ) : <EOL> """<STR_LIT>""" <EOL> config_args = [ ] <EOL> required_args = [ ] <EOL> for action in parser . _actions : <EOL> if action . required : <EOL> required_args . append ( action ) <EOL> action . required = False <EOL> parsed_args = parser . parse_args ( stdin_args ) <EOL> for action in required_args : <EOL> action . required = True <EOL> if not parsed_args . config_file : <EOL> return config_args <EOL> config = ConfigParser ( ) <EOL> if not config . read ( parsed_args . config_file ) : <EOL> sys . stderr . write ( '<STR_LIT>' . format ( parsed_args . config_file ) ) <EOL> sys . exit ( <NUM_LIT:7> ) <EOL> config_args = _convert_config_to_stdin ( config , parser ) <EOL> return config_args <EOL> def dump_config_file ( filename , args , parser = None ) : <EOL> """<STR_LIT>""" <EOL> config = ConfigParser ( ) <EOL> config . add_section ( SECTION ) <EOL> if parser is None : <EOL> for attr in args : <EOL> config . set ( SECTION , attr , args . attr ) <EOL> else : <EOL> keys_empty_values_not_pass = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for action in parser . _actions : <EOL> if action . dest in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> keyp = action . option_strings [ <NUM_LIT:0> ] <EOL> option_name = keyp . lstrip ( '<STR_LIT:->' ) <EOL> option_value = getattr ( args , action . dest ) <EOL> if any ( [ i for i in keys_empty_values_not_pass if i in action . option_strings ] ) : <EOL> if action . dest == '<STR_LIT>' : <EOL> config . set ( SECTION , option_name , option_value . zone ) <EOL> elif action . dest == '<STR_LIT>' : <EOL> if len ( option_value ) == <NUM_LIT:1> and option_value [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> config . set ( SECTION , option_name , '<STR_LIT>' ) <EOL> else : <EOL> config . set ( SECTION , option_name , '<STR_LIT:U+002C>' . join ( option_value ) ) <EOL> else : <EOL> config . set ( SECTION , option_name , option_value if option_value else '<STR_LIT>' ) <EOL> elif action . choices == ( '<STR_LIT:yes>' , '<STR_LIT>' ) : <EOL> config . set ( SECTION , option_name , '<STR_LIT:yes>' if option_value else '<STR_LIT>' ) <EOL> elif action . dest == '<STR_LIT>' : <EOL> config . set ( SECTION , option_name , option_value if option_value else '<STR_LIT>' ) <EOL> elif action . dest == '<STR_LIT>' : <EOL> version = ( '<STR_LIT>' if option_value == CMS_VERSION_MATRIX [ '<STR_LIT>' ] <EOL> else option_value ) <EOL> config . set ( SECTION , option_name , version ) <EOL> elif action . dest == '<STR_LIT>' : <EOL> version = ( '<STR_LIT>' if option_value == DJANGO_VERSION_MATRIX [ '<STR_LIT>' ] <EOL> else option_value ) <EOL> config . set ( SECTION , option_name , version ) <EOL> elif action . const : <EOL> config . set ( SECTION , option_name , '<STR_LIT:true>' if option_value else '<STR_LIT:false>' ) <EOL> else : <EOL> config . set ( SECTION , option_name , str ( option_value ) ) <EOL> with open ( filename , '<STR_LIT:w>' ) as fp : <EOL> config . write ( fp ) <EOL> def _convert_config_to_stdin ( config , parser ) : <EOL> """<STR_LIT>""" <EOL> keys_empty_values_not_pass = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> args = [ ] <EOL> for key , val in config . items ( SECTION ) : <EOL> keyp = '<STR_LIT>' . format ( key ) <EOL> action = parser . _option_string_actions [ keyp ] <EOL> if action . const : <EOL> try : <EOL> if config . getboolean ( SECTION , key ) : <EOL> args . append ( keyp ) <EOL> except ValueError : <EOL> args . extend ( [ keyp , val ] ) <EOL> elif any ( [ i for i in keys_empty_values_not_pass if i in action . option_strings ] ) : <EOL> if val != '<STR_LIT>' : <EOL> args . extend ( [ keyp , val ] ) <EOL> else : <EOL> args . extend ( [ keyp , val ] ) <EOL> return args </s>
