<s> import logging <EOL> import os <EOL> import six <EOL> from six . moves import configparser <EOL> from kmip . core import exceptions <EOL> class KmipServerConfig ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _logger = logging . getLogger ( '<STR_LIT>' ) <EOL> self . settings = dict ( ) <EOL> self . _expected_settings = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:port>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> def set_setting ( self , setting , value ) : <EOL> """<STR_LIT>""" <EOL> if setting not in self . _expected_settings : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" . format ( setting ) <EOL> ) <EOL> if setting == '<STR_LIT>' : <EOL> self . _set_hostname ( value ) <EOL> elif setting == '<STR_LIT:port>' : <EOL> self . _set_port ( value ) <EOL> elif setting == '<STR_LIT>' : <EOL> self . _set_certificate_path ( value ) <EOL> elif setting == '<STR_LIT>' : <EOL> self . _set_key_path ( value ) <EOL> elif setting == '<STR_LIT>' : <EOL> self . _set_ca_path ( value ) <EOL> else : <EOL> self . _set_auth_suite ( value ) <EOL> def load_settings ( self , path ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( path ) : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( path ) <EOL> ) <EOL> self . _logger . info ( <EOL> "<STR_LIT>" . format ( path ) <EOL> ) <EOL> parser = configparser . SafeConfigParser ( ) <EOL> parser . read ( path ) <EOL> self . _parse_settings ( parser ) <EOL> def _parse_settings ( self , parser ) : <EOL> if not parser . has_section ( '<STR_LIT>' ) : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> settings = [ x [ <NUM_LIT:0> ] for x in parser . items ( '<STR_LIT>' ) ] <EOL> for setting in settings : <EOL> if setting not in self . _expected_settings : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( setting ) <EOL> ) <EOL> for setting in self . _expected_settings : <EOL> if setting not in settings : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( setting ) <EOL> ) <EOL> if parser . has_option ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . _set_hostname ( parser . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if parser . has_option ( '<STR_LIT>' , '<STR_LIT:port>' ) : <EOL> self . _set_port ( parser . getint ( '<STR_LIT>' , '<STR_LIT:port>' ) ) <EOL> if parser . has_option ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . _set_certificate_path ( parser . get ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> ) <EOL> if parser . has_option ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . _set_key_path ( parser . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if parser . has_option ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . _set_ca_path ( parser . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if parser . has_option ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . _set_auth_suite ( parser . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def _set_hostname ( self , value ) : <EOL> if isinstance ( value , six . string_types ) : <EOL> self . settings [ '<STR_LIT>' ] = value <EOL> else : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> def _set_port ( self , value ) : <EOL> if isinstance ( value , six . integer_types ) : <EOL> if <NUM_LIT:0> < value < <NUM_LIT> : <EOL> self . settings [ '<STR_LIT:port>' ] = value <EOL> else : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> else : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> def _set_certificate_path ( self , value ) : <EOL> if value is None : <EOL> self . settings [ '<STR_LIT>' ] = None <EOL> elif isinstance ( value , six . string_types ) : <EOL> if os . path . exists ( value ) : <EOL> self . settings [ '<STR_LIT>' ] = value <EOL> else : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> else : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> def _set_key_path ( self , value ) : <EOL> if value is None : <EOL> self . settings [ '<STR_LIT>' ] = None <EOL> elif isinstance ( value , six . string_types ) : <EOL> if os . path . exists ( value ) : <EOL> self . settings [ '<STR_LIT>' ] = value <EOL> else : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> else : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> def _set_ca_path ( self , value ) : <EOL> if value is None : <EOL> self . settings [ '<STR_LIT>' ] = None <EOL> elif isinstance ( value , six . string_types ) : <EOL> if os . path . exists ( value ) : <EOL> self . settings [ '<STR_LIT>' ] = value <EOL> else : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> else : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> def _set_auth_suite ( self , value ) : <EOL> auth_suites = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> if value not in auth_suites : <EOL> raise exceptions . ConfigurationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> else : <EOL> self . settings [ '<STR_LIT>' ] = value </s>
