def __init__(self): """ initialize the varspace of an existing plugin instance init_varspace() is a superclass method of plugin """ self.folder = os.path.join(g.folder, c.DEFAULT_CONFIG_DIR) self.filename = os.path.join(self.folder, 'config' + c.CONFIG_EXTENSION) self.default_config = False # whether a new name was generated self.var_dict = dict() self.spec = ConfigObj(CONFIG_SPEC, interpolation=False, list_values=False, _inspec=True) # try: self.load_config() # convenience - flatten nested config dict to access it via self.config.sectionname.varname self.vars = DictDotLookup(self.var_dict) self.mode3d = self.vars.General['mode3d'] self.machine_type = self.vars.General['machine_type'] self.fitting_tolerance = self.vars.Import_Parameters[ 'fitting_tolerance'] self.point_tolerance = self.vars.Import_Parameters['point_tolerance'] self.metric = 1 # true unit is determined while importing self.tool_units_metric = 0 if self.vars.General[ 'tool_units'] == 'in' else 1
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 __init__(self, file_name='postpro_config' + c.CONFIG_EXTENSION): """ initialize the varspace of an existing plugin instance init_varspace() is a superclass method of plugin @param file_name: The file_name for the creation of a new config file and the file_name of the file to read config from. """ self.folder = os.path.join('C:\\PythonScripts\\1.Programming\\dxf2g\\config') self.file_name = os.path.join(self.folder, file_name) self.version_mismatch = '' # no problem for now self.default_config = False # whether a new name was generated self.var_dict = dict() self.spec = ConfigObj(POSTPRO_SPEC, interpolation=False, list_values=False, _inspec=True)
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()
def __init__(self, filename='postpro_config' + c.CONFIG_EXTENSION): """ initialize the varspace of an existing plugin instance init_varspace() is a superclass method of plugin @param filename: The filename for the creation of a new config file and the filename of the file to read config from. """ self.folder = os.path.join(g.folder, c.DEFAULT_POSTPRO_DIR) self.filename = os.path.join(self.folder, filename) self.default_config = False # whether a new name was generated self.var_dict = dict() self.spec = ConfigObj(POSTPRO_SPEC, interpolation=False, list_values=False, _inspec=True)
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
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 __init__(self, script_dir): """ initialize the varspace of an existing plugin instance init_varspace() is a superclass method of plugin """ self.folder = script_dir + '\\config' self.file_name = os.path.join(self.folder, 'config' + c.CONFIG_EXTENSION) self.version_mismatch = '' # no problem for now self.default_config = False # whether a new name was generated self.var_dict = dict() self.spec = ConfigObj(CONFIG_SPEC, interpolation=False, list_values=False, _inspec=True) # try: self.load_config() self.update_config() # The following settings won't be modified after a change in the configuration window. # If a setting need to be updated when the configuration changes, move it to the update_config() function self.mode3d = self.vars.General['mode3d'] self.machine_type = self.vars.General['machine_type'] self.fitting_tolerance = self.vars.Import_Parameters[ 'fitting_tolerance'] self.point_tolerance = self.vars.Import_Parameters['point_tolerance'] self.metric = 1 # true unit is determined while importing self.tool_units = self.vars.General[ 'tool_units'] # store the initial tool_units (we don't want it to change until software restart) self.tool_units_metric = 0 if self.vars.General[ 'tool_units'] == 'in' else 1
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)
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