Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CuraProfileReader.py « CuraProfileReader « plugins - github.com/Ultimaker/Cura.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2198d73b2266ee5d04e94ac48cf066d2ec75966c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Copyright (c) 2016 Ultimaker B.V.
# Cura is released under the terms of the AGPLv3 or higher.
import configparser

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

import zipfile

##  A plugin that reads profile data from Cura profile files.
#
#   It reads a profile from a .curaprofile file, and returns it as a profile
#   instance.
class CuraProfileReader(ProfileReader):
    ##  Initialises the cura profile reader.
    #   This does nothing since the only other function is basically stateless.
    def __init__(self):
        super().__init__()

    ##  Reads a cura profile from a file and returns it.
    #
    #   \param file_name The file to read the cura profile from.
    #   \return The cura profile that was in the file, if any. If the file could
    #   not be read or didn't contain a valid profile, \code None \endcode is
    #   returned.
    def read(self, file_name):
        try:
            with zipfile.ZipFile(file_name, "r") as archive:
                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 []
        if not "version" in parser["general"]:
            Logger.log("w", "Missing required 'version' property")
            return []

        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 []

        filenames, outputs = profile_convert_funcs[0](serialized, profile_id)
        if filenames is None and outputs is None:
            return []
        return list(zip(outputs, filenames))