def get_serial_params(section, config): """Returns the serial.Serial parameters stored in the given configuration. Arguments: section -- A string representing the section name containing the parameters of interest. config -- The ConfigParser object containing the parameters of interest. If config does not contain section, then a the default serial.Serial parameters are returned. If not all parameters are included in the section, then default values for the missing parameters are used in their place. """ serial_params = {} default_serial_port = serial.Serial() for opt in SERIAL_ALL_OPTIONS: serial_params[opt] = default_serial_port.__getattribute__(opt) for opt in (SERIAL_PORT_OPTION, SERIAL_PARITY_OPTION): if config.has_option(section, opt): serial_params[opt] = config.get(section, opt) for opt in (SERIAL_BAUDRATE_OPTION, SERIAL_BYTESIZE_OPTION, SERIAL_STOPBITS_OPTION): if config.has_option(section, opt): serial_params[opt] = config.getint(section, opt) for opt in (SERIAL_XONXOFF_OPTION, SERIAL_RTSCTS_OPTION): if config.has_option(section, opt): serial_params[opt] = config.getboolean(section, opt) if config.has_option(section, SERIAL_TIMEOUT_OPTION): timeout = config.get(section, SERIAL_TIMEOUT_OPTION) if timeout == 'None': timeout = None else: timeout = float(timeout) else: timeout = SERIAL_DEFAULT_TIMEOUT serial_params[SERIAL_TIMEOUT_OPTION] = timeout class _Struct(object): # Helper class to convert a dictionary to an object. def __init__(self, **kwargs): self.__dict__.update(kwargs) return _Struct(**serial_params)
def verify_config(config): """Checks that the configuration contains values for all parameters. Arguments: config -- A ConfigParser.RawConfigParser object. Returns True if all parameters were found. Otherwise returns False and adds default values for all parameters. """ config_file = CONFIG_FILE # Verify options exist. for section, option, default in ( (LOGGING_CONFIG_SECTION, LOG_FILE_OPTION, DEFAULT_LOG_FILE), (LOGGING_CONFIG_SECTION, ENABLE_TRANSLATION_LOGGING_OPTION, DEFAULT_ENABLE_TRANSLATION_LOGGING), (LOGGING_CONFIG_SECTION, ENABLE_STROKE_LOGGING_OPTION, DEFAULT_ENABLE_STROKE_LOGGING), (DICTIONARY_CONFIG_SECTION, DICTIONARY_FILE_OPTION, DEFAULT_DICTIONARY_FILE), (MACHINE_CONFIG_SECTION, MACHINE_TYPE_OPTION, DEFAULT_MACHINE_TYPE), (MACHINE_CONFIG_SECTION, MACHINE_AUTO_START_OPTION, DEFAULT_MACHINE_AUTO_START), ): if not config.has_section(section): config.add_section(section) if not config.has_option(section, option): config.set(section, option, default) # Write the file to disk. with open(config_file, 'w') as f: config.write(f)