def loadFromFile(self, path): config = configparser.ConfigParser(interpolation=None) config.read(path, "utf-8") if not config.has_section("general"): raise SettingsError.InvalidFileError(path) if not config.has_option("general", "version"): raise SettingsError.InvalidFileError(path) if not config.has_option("general", "name") or not config.has_option( "general", "type"): raise SettingsError.InvalidFileError(path) if int(config.get("general", "version")) != self.MachineInstanceVersion: raise SettingsError.InvalidVersionError(path) type_name = config.get("general", "type") variant_name = config.get("general", "variant", fallback="") self._machine_definition = self._machine_manager.findMachineDefinition( type_name, variant_name) self._machine_definition.loadAll() self._name = config.get("general", "name") for key, value in config["machine_settings"].items(): self._machine_setting_overrides[key] = value
def loadMetaData(self): # Should we clean up the loaded JSON data after reading metadata? # When we call loadALL the JSON data gets loaded and cleaned up in loadAll. # loadAll calls loadMetaData internally but we should not try to load the # JSON data again. So only perform the JSON data cleanup when we are not being # called by loadAll. clean_json = False if not self._json_data: clean_json = True with open(self._path, "rt", -1, "utf-8") as f: try: self._json_data = json.load( f, object_pairs_hook=collections.OrderedDict) except JSONDecodeError as e: raise SettingsError.InvalidFileError(self._path) from e if "id" not in self._json_data or "name" not in self._json_data or "version" not in self._json_data: raise SettingsError.InvalidFileError(self._path) if int(self._json_data["version"]) != self.MachineDefinitionVersion: raise SettingsError.InvalidVersionError(self._path) if self._machine_manager.getApplicationName() in self._json_data: app_data = self._json_data[ self._machine_manager.getApplicationName()] self._json_data[self._machine_manager.getApplicationName()] = None self._json_data.update(app_data) self._id = self._json_data["id"] if "profiles_machine" in self._json_data: self._profiles_machine_id = self._json_data["profiles_machine"] else: self._profiles_machine_id = self._id self._name = self._json_data["name"] self._visible = self._json_data.get("visible", True) self._variant_name = self._json_data.get("variant", "") self._manufacturer = self._json_data.get( "manufacturer", uranium_catalog.i18nc("@label", "Unknown Manufacturer")) self._author = self._json_data.get( "author", uranium_catalog.i18nc("@label", "Unknown Author")) self._pages = self._json_data.get("pages", {}) self._file_formats = [ file_type.strip() for file_type in self._json_data.get("file_formats", "").split(";") ] #Split by semicolon, then strip all whitespace on both sides. if clean_json: self._json_data = None
def loadFromFile(self, path): parser = configparser.ConfigParser(interpolation=None) parser.read(path, "utf-8") if not parser.has_section("general"): raise SettingsError.InvalidFileError(path) if not parser.has_option("general", "version") or int( parser.get("general", "version")) != self.ProfileVersion: raise SettingsError.InvalidVersionError(path) self._name = parser.get("general", "name") if parser.has_section("settings"): for key, value in parser["settings"].items(): self.setSettingValue(key, value)
def unserialise(self, serialised, origin="(unknown)"): stream = io.StringIO( serialised) #ConfigParser needs to read from a stream. parser = configparser.ConfigParser(interpolation=None) parser.readfp(stream) if not parser.has_section("general"): raise SettingsError.InvalidFileError(origin) if not parser.has_option("general", "version") or int( parser.get("general", "version")) != self.ProfileVersion: raise SettingsError.InvalidVersionError(origin) self._name = parser.get("general", "name") if parser.has_section("settings"): for key, value in parser["settings"].items(): self.setSettingValue(key, value)
def addProfile(self, profile): if profile in self._profiles: return for p in self._profiles: if p.getName() == profile.getName(): raise SettingsError.DuplicateProfileError(profile.getName()) self._profiles.append(profile) profile.nameChanged.connect(self._onProfileNameChanged) self.profilesChanged.emit()
def unserialise(self, serialised, origin = "(unknown)"): stream = io.StringIO(serialised) # ConfigParser needs to read from a stream. parser = configparser.ConfigParser(interpolation = None) parser.readfp(stream) if not parser.has_section("general"): raise SettingsError.InvalidFileError(origin) if not parser.has_option("general", "version") or int(parser.get("general", "version")) != self.ProfileVersion: raise SettingsError.InvalidVersionError(origin) self._name = parser.get("general", "name") if "type" in parser["general"]: self._type = parser.get("general", "type") if "weight" in parser["general"]: self._weight = int(parser.get("general", "weight")) if "machine_type" in parser["general"]: self._machine_type_id = parser.get("general", "machine_type") if "machine_variant" in parser["general"]: self._machine_variant_name = parser.get("general", "machine_variant") if "machine_instance" in parser["general"]: self._machine_instance_name = parser.get("general", "machine_instance") if "material" in parser["general"]: self._material_name = parser.get("general", "material") elif self._type == "material" and "name" in parser["general"]: self._material_name = parser.get("general", "name") if parser.has_section("settings"): for key, value in parser["settings"].items(): self.setSettingValue(key, value, silent = True) if parser.has_section("defaults"): self._changed_settings_defaults = {} for key, value in parser["defaults"].items(): self._changed_settings_defaults[key] = value self._disabled_settings_defaults = [] if parser.has_section("disabled_defaults"): disabled_defaults_string = parser.get("disabled_defaults", "values") for item in disabled_defaults_string.split(","): if item != "": self._disabled_settings_defaults.append(item)
def addMachineInstance(self, instance): if instance in self._machine_instances: return for i in self._machine_instances: if i.getName() == instance.getName(): raise SettingsError.DuplicateMachineInstanceError( instance.getName()) self._machine_instances.append(instance) instance.nameChanged.connect(self._onInstanceNameChanged) self.machineInstancesChanged.emit()
def addProfile(self, profile): if profile in self._profiles: return profiles = self.getProfiles(instance=self._active_machine) for p in profiles: if p.getName() == profile.getName(): raise SettingsError.DuplicateProfileError(profile.getName()) self._profiles.append(profile) profile.nameChanged.connect(self._onProfileNameChanged) self.profilesChanged.emit()
def loadFromFile(self, path): config = configparser.ConfigParser() config.read(path) if not config.has_section("General"): raise SettingsError.InvalidFileError(path) if not config.has_option( "General", "version") or int(config.get( "General", "version")) != self.MachineInstanceVersion: raise SettingsError.InvalidVersionError(path) if not config.has_option("General", "name") or not config.has_option( "General", "json_file"): raise SettingsError.InvalidFileError(path) try: self.loadSettingsFromFile( Resources.getPath(Resources.MachineDefinitions, config["General"]["json_file"])) except FileNotFoundError: raise SettingsError.InvalidFileError(path) self._name = config.get("General", "name", fallback="Unknown Machine")
def loadAll(self): if self._loaded: return with open(self._path, "rt", -1, "utf-8") as f: try: self._json_data = json.load( f, object_pairs_hook=collections.OrderedDict) except JSONDecodeError as e: raise SettingsError.InvalidFileError(self._path) from e if not self._name: self.loadMetaData() self._i18n_catalog = i18nCatalog(os.path.basename(self._path)) self._platform_mesh = self._json_data.get("platform", "") self._platform_texture = self._json_data.get("platform_texture", "") if "inherits" in self._json_data: try: path = Resources.getPath(Resources.MachineDefinitions, self._json_data["inherits"]) except FileNotFoundError as e: # If we cannot find the file in Resources, try and see if it can be found relative to this file. # This is primarily used by the unit tests. path = os.path.join(os.path.dirname(self._path), self._json_data["inherits"]) if not os.path.exists(path): raise FileNotFoundError( "Could not find file {0} in Resources or next to {1}". format(self._json_data["inherits"], self._path)) from e inherits_from = MachineDefinition(self._machine_manager, path) inherits_from.loadAll() self._machine_settings = inherits_from._machine_settings self._categories = inherits_from._categories self._preferences = inherits_from._preferences if "machine_settings" in self._json_data: for key, value in self._json_data["machine_settings"].items(): setting = self.getSetting(key) if not setting: setting = Setting(self._machine_manager, key, self._i18n_catalog) self._machine_settings.append(setting) setting.fillByDict(value) if "categories" in self._json_data: for key, value in self._json_data["categories"].items(): category = self.getSettingsCategory(key) if not category: category = SettingsCategory(self._machine_manager, key, self._i18n_catalog, self) self._categories.append(category) category.fillByDict(value) if "overrides" in self._json_data: for key, value in self._json_data["overrides"].items(): setting = self.getSetting(key) if not setting: continue setting.fillByDict(value) if "machine_preferences" in self._json_data: for key, value in self._json_data["machine_preferences"].items(): self._preferences[key] = value self.settingsLoaded.emit() self._loaded = True
def loadSettingsFromFile(self, file_name): with open(file_name, "rt", -1, "utf-8") as f: data = json.load(f, object_pairs_hook=collections.OrderedDict) if "id" not in data or "name" not in data: raise SettingsError.InvalidFileError(file_name) if "version" not in data or data[ "version"] != self.MachineDefinitionVersion: raise SettingsError.InvalidVersionError(file_name) self._i18n_catalog = i18nCatalog(os.path.basename(file_name)) self._json_file = file_name self._type_id = data["id"] self._type_name = data["name"] if "platform" in data: self._platform_mesh = data["platform"] if "platform_texture" in data: self._platform_texture = data["platform_texture"] if "version" in data: self._type_version = str(data["version"]) if "icon" in data: self._icon = data["icon"] if "inherits" in data: inherits_from = MachineSettings() inherits_from.loadSettingsFromFile( os.path.dirname(file_name) + "/" + data["inherits"]) self._machine_settings = inherits_from._machine_settings self._categories = inherits_from._categories if "machine_settings" in data: for key, value in data["machine_settings"].items(): setting = self.getSettingByKey(key) if not setting: setting = Setting(key, self._i18n_catalog) self.addSetting(setting) setting.fillByDict(value) if "categories" in data: for key, value in data["categories"].items(): category = self.getSettingsCategory(key) if not category: category = SettingsCategory(key, self._i18n_catalog, self) self.addSettingsCategory(category) category.fillByDict(value) if "overrides" in data: for key, value in data["overrides"].items(): setting = self.getSettingByKey(key) if not setting: continue setting.fillByDict(value) for setting in self.getAllSettings(): setting.valueChanged.connect(self.settingChanged) self.settingsLoaded.emit( ) #Emit signal that all settings are loaded (some setting stuff can only be done when all settings are loaded (eg; the conditional stuff)