Beispiel #1
0
    def create_default_config(self):
        # check for existing setting folder or create one
        self.make_settings_folder()

        # derive config file with defaults from spec
        self.var_dict = ConfigObj(configspec=CONFIG_SPEC)
        _vdt = Validator()
        self.var_dict.validate(_vdt, copy=True)
        self.var_dict.filename = self.filename
        self.var_dict.write()
    def create_default_config(self):
        """
        If no postprocessor config file exists this function is called
        to generate the config file based on its specification.
        """
        # check for existing setting folder or create one
        self.make_settings_folder()

        # derive config file with defaults from spec
        logger.debug(POSTPRO_SPEC)

        self.var_dict = ConfigObj(configspec=POSTPRO_SPEC)
        _vdt = Validator()
        self.var_dict.validate(_vdt, copy=True)
        self.var_dict.filename = self.filename
        self.var_dict.write()
Beispiel #3
0
    def load_config(self):
        """Load Config File"""
        print(self.file_name)
        if os.path.isfile(self.file_name):
            try:
                # file exists, read & validate it
                self.var_dict = ConfigObj(self.file_name,
                                          configspec=CONFIG_SPEC)
                _vdt = Validator()
                result = self.var_dict.validate(_vdt, preserve_errors=True)
                validate_errors = flatten_errors(self.var_dict, result)
            except self.var_dict == None:  #rw
                logger.error(
                    "reading values from postprocessorconfig file error")  #rw

        self.var_dict.main.interpolation = False  # avoid ConfigObj getting too clever
Beispiel #4
0
    def load_config(self):
        """
        This method tries to load the defined postprocessor file given in
        self.file_name. If this fails it will create a new one
        """

        try:
            # file exists, read & validate it
            self.var_dict = ConfigObj(self.file_name, configspec=POSTPRO_SPEC)
            _vdt = Validator()
            result = self.var_dict.validate(_vdt, preserve_errors=True)
            validate_errors = flatten_errors(self.var_dict, result)
        except self.var_dict == None: #rw
            logger.error("reading values from postprocessorconfig file error") #rw

        self.var_dict.main.interpolation = False  # avoid ConfigObj getting too clever
        self.update_config()
    def load_config(self):
        """
        This method tries to load the defined postprocessor file given in
        self.filename. If this fails it will create a new one
        """

        try:
            # file exists, read & validate it
            self.var_dict = ConfigObj(self.filename, configspec=POSTPRO_SPEC)
            _vdt = Validator()
            result = self.var_dict.validate(_vdt, preserve_errors=True)
            validate_errors = flatten_errors(self.var_dict, result)

            if validate_errors:
                g.logger.logger.error(
                    self.tr("errors reading %s:") % self.filename)
            for entry in validate_errors:
                section_list, key, error = entry
                if key is not None:
                    section_list.append(key)
                else:
                    section_list.append('[missing section]')
                section_string = ', '.join(section_list)
                if error == False:
                    error = self.tr('Missing value or section.')
                g.logger.logger.error(section_string + ' = ' + error)

            if validate_errors:
                raise BadConfigFileError(
                    self.tr("syntax errors in postpro_config file"))

            # check config file version against internal version

            if POSTPRO_VERSION:
                fileversion = self.var_dict['Version'][
                    'config_version']  # this could raise KeyError

                if fileversion != POSTPRO_VERSION:
                    raise VersionMismatchError(fileversion, POSTPRO_VERSION)

        except VersionMismatchError:
            raise VersionMismatchError(fileversion, POSTPRO_VERSION)

        except Exception as inst:
            logger.error(inst)
            (base, ext) = os.path.splitext(self.filename)
            badfilename = base + c.BAD_CONFIG_EXTENSION
            logger.debug(
                self.tr("trying to rename bad cfg %s to %s") %
                (self.filename, badfilename))
            try:
                os.rename(self.filename, badfilename)
            except OSError as e:
                logger.error(
                    self.tr("rename(%s,%s) failed: %s") %
                    (self.filename, badfilename, e.strerror))
                raise
            else:
                logger.debug(
                    self.tr("renamed bad varspace %s to '%s'") %
                    (self.filename, badfilename))
                self.create_default_config()
                self.default_config = True
                logger.debug(
                    self.tr("created default varspace '%s'") % self.filename)
        else:
            self.default_config = False
            logger.debug(
                self.tr("read existing varspace '%s'") % self.filename)

        # convenience - flatten nested config dict to access it via self.config.sectionname.varname
        self.var_dict.main.interpolation = False  # avoid ConfigObj getting too clever
        self.vars = DictDotLookup(self.var_dict)
Beispiel #6
0
    def load_config(self):
        """Load Config File"""
        if os.path.isfile(self.filename):
            try:
                # file exists, read & validate it
                self.var_dict = ConfigObj(self.filename,
                                          configspec=CONFIG_SPEC)
                _vdt = Validator()
                result = self.var_dict.validate(_vdt, preserve_errors=True)
                validate_errors = flatten_errors(self.var_dict, result)

                if validate_errors:
                    logger.error(self.tr("errors reading %s:") % self.filename)

                for entry in validate_errors:
                    section_list, key, error = entry
                    if key is not None:
                        section_list.append(key)
                    else:
                        section_list.append('[missing section]')
                    section_string = ', '.join(section_list)
                    if not error:
                        error = self.tr('Missing value or section.')
                    logger.error(section_string + ' = ' + error)

                if validate_errors:
                    raise BadConfigFileError("syntax errors in config file")

                # check config file version against internal version
                if CONFIG_VERSION:
                    fileversion = self.var_dict['Version'][
                        'config_version']  # this could raise KeyError

                    if fileversion != CONFIG_VERSION:
                        raise VersionMismatchError(fileversion, CONFIG_VERSION)

            except VersionMismatchError:
                #raise VersionMismatchError(fileversion, CONFIG_VERSION)
                # version mismatch flag, it will be used to display an error.
                self.version_mismatch = self.tr(
                    "The configuration file version ({0}) doesn't match the software expected version ({1}).\n\nYou have to delete (or carefully edit) the configuration file \"{2}\" to solve the problem."
                ).format(fileversion, CONFIG_VERSION, self.filename)

            except Exception as inst:
                logger.error(inst)
                (base, ext) = os.path.splitext(self.filename)
                badfilename = base + c.BAD_CONFIG_EXTENSION
                logger.debug(
                    self.tr("trying to rename bad cfg %s to %s") %
                    (self.filename, badfilename))
                try:
                    os.rename(self.filename, badfilename)
                except OSError as e:
                    logger.error(
                        self.tr("rename(%s,%s) failed: %s") %
                        (self.filename, badfilename, e.strerror))
                    raise
                else:
                    logger.debug(
                        self.tr("renamed bad varspace %s to '%s'") %
                        (self.filename, badfilename))
                    self.create_default_config()
                    self.default_config = True
                    logger.debug(
                        self.tr("created default varspace '%s'") %
                        self.filename)
            else:
                self.default_config = False
                # logger.debug(self.dir())
                # logger.debug(self.tr("created default varspace '%s'") % self.filename)
                # logger.debug(self.tr("read existing varspace '%s'") % self.filename)
        else:
            self.create_default_config()
            self.default_config = True
            logger.debug(
                self.tr("created default varspace '%s'") % self.filename)

        self.var_dict.main.interpolation = False  # avoid ConfigObj getting too clever