diff options
author | Simon Edwards <s.edwards@ultimaker.com> | 2016-09-07 12:11:44 +0300 |
---|---|---|
committer | Simon Edwards <s.edwards@ultimaker.com> | 2016-09-07 12:13:25 +0300 |
commit | 86369ce1da14b91deb28ddc32d8cbdc1aab28b96 (patch) | |
tree | cdbc1c07bd23ca3c96c505688d91720a4a30869e /plugins/CuraProfileReader | |
parent | 1bafac94bd27817b5e5310618658809e0be3afeb (diff) |
Profile import now supports 2.1 profiles and does any needed conversion work.
Contributes to CURA-2252 Import ini profile fails in 2.3
Diffstat (limited to 'plugins/CuraProfileReader')
-rw-r--r-- | plugins/CuraProfileReader/CuraProfileReader.py | 92 |
1 files changed, 73 insertions, 19 deletions
diff --git a/plugins/CuraProfileReader/CuraProfileReader.py b/plugins/CuraProfileReader/CuraProfileReader.py index 772b11890b..4f5bb324c0 100644 --- a/plugins/CuraProfileReader/CuraProfileReader.py +++ b/plugins/CuraProfileReader/CuraProfileReader.py @@ -1,8 +1,8 @@ # Copyright (c) 2015 Ultimaker B.V. # Cura is released under the terms of the AGPLv3 or higher. +import configparser -import os.path - +from UM import PluginRegistry from UM.Logger import Logger from UM.Settings.InstanceContainer import InstanceContainer # The new profile to make. from cura.ProfileReader import ProfileReader @@ -28,21 +28,75 @@ class CuraProfileReader(ProfileReader): def read(self, file_name): try: archive = zipfile.ZipFile(file_name, "r") - except Exception: - # zipfile doesn't give proper exceptions, so we can only catch broad ones + results = [] + for profile_id in archive.namelist(): + with archive.open(profile_id) as f: + serialized = f.read() + profile = self._loadProfile(serialized.decode("utf-8"), profile_id) + if profile is not None: + results.append(profile) + return results + + except zipfile.BadZipFile: + # It must be an older profile from Cura 2.1. + with open(file_name, encoding="utf-8") as fhandle: + serialized = fhandle.read() + return [self._loadProfile(serialized, profile_id) for serialized, profile_id in self._upgradeProfile(serialized, file_name)] + + ## Convert a profile from an old Cura to this Cura if needed. + # + # \param serialized \type{str} The profile data to convert in the serialized on-disk format. + # \param profile_id \type{str} The name of the profile. + # \return \type{List[Tuple[str,str]]} List of serialized profile strings and matching profile names. + def _upgradeProfile(self, serialized, profile_id): + parser = configparser.ConfigParser(interpolation=None) + parser.read_string(serialized) + + if not "general" in parser: + Logger.log('w', "Missing required section 'general'.") + return None + if not "version" in parser["general"]: + Logger.log('w', "Missing required 'version' property") + return None + + version = int(parser["general"]["version"]) + if InstanceContainer.Version != version: + name = parser["general"]["name"] + return self._upgradeProfileVersion(serialized, name, version) + else: + return [(serialized, profile_id)] + + ## Load a profile from a serialized string. + # + # \param serialized \type{str} The profile data to read. + # \param profile_id \type{str} The name of the profile. + # \return \type{InstanceContainer|None} + def _loadProfile(self, serialized, profile_id): + # Create an empty profile. + profile = InstanceContainer(profile_id) + profile.addMetaDataEntry("type", "quality_changes") + try: + profile.deserialize(serialized) + except Exception as e: # Parsing error. This is not a (valid) Cura profile then. + Logger.log("e", "Error while trying to parse profile: %s", str(e)) + return None + return profile + + ## Upgrade a serialized profile to the current profile format. + # + # \param serialized \type{str} The profile data to convert. + # \param profile_id \type{str} The name of the profile. + # \param source_version \type{int} The profile version of 'serialized'. + # \return \type{List[Tuple[str,str]]} List of serialized profile strings and matching profile names. + def _upgradeProfileVersion(self, serialized, profile_id, source_version): + converter_plugins = PluginRegistry.getInstance().getAllMetaData(filter={"version_upgrade": {} }, active_only=True) + + source_format = ("profile", source_version) + profile_convert_funcs = [plugin["version_upgrade"][source_format][2] for plugin in converter_plugins + if source_format in plugin["version_upgrade"] and plugin["version_upgrade"][source_format][1] == InstanceContainer.Version] + + if not profile_convert_funcs: return [] - results = [] - for profile_id in archive.namelist(): - # Create an empty profile. - profile = InstanceContainer(profile_id) - profile.addMetaDataEntry("type", "quality_changes") - serialized = "" - with archive.open(profile_id) as f: - serialized = f.read() - try: - profile.deserialize(serialized.decode("utf-8") ) - except Exception as e: # Parsing error. This is not a (valid) Cura profile then. - Logger.log("e", "Error while trying to parse profile: %s", str(e)) - continue - results.append(profile) - return results
\ No newline at end of file + + filenames, outputs = profile_convert_funcs[0](serialized, profile_id) + return list(zip(outputs, filenames)) |