File: VersionUpgrade49to410.py

package info (click to toggle)
cura 5.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 122,920 kB
  • sloc: python: 44,572; sh: 81; xml: 32; makefile: 16
file content (197 lines) | stat: -rw-r--r-- 8,783 bytes parent folder | download | duplicates (3)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Copyright (c) 2019 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.

import configparser
import io
import os.path  # To get the file ID.
from typing import Dict, List, Tuple

from UM.VersionUpgrade import VersionUpgrade


class VersionUpgrade49to410(VersionUpgrade):
    """
    Upgrades configurations from the state they were in at version 4.9 to the state they should be in at version 4.10.
    """

    _renamed_profiles = {
        # Definitions.
        "twotrees_bluer"           : "two_trees_bluer",

        # Upgrade for people who had the original TwoTrees Bluer profiles from 4.9 and earlier.
        "twotrees_bluer_extruder_0": "two_trees_base_extruder_0",
        "twotrees_bluer_extruder_1": "two_trees_base_extruder_0"
    }

    _quality_changes_to_two_trees_base = {
        "twotrees_bluer_extruder_0",
        "twotrees_bluer_extruder_1",
        "twotrees_bluer"
    }

    # Default variant to select for legacy TwoTrees printers, now that we have variants.
    _default_variants = {
        "twotrees_bluer_extruder_0"  : "two_trees_bluer_0.4",
        "twotrees_bluer_extruder_1"  : "two_trees_bluer_0.4"
    }

    _two_trees_bluer_quality_type_conversion = {
        "high"        : "ultra",
        "normal"      : "super",
        "fast"        : "standard",
        "draft"       : "standard",
        "extra_fast"  : "low",
        "coarse"      : "draft",
        "extra_coarse": "draft"
    }

    _two_trees_quality_per_material = {
        # Since legacy TwoTrees printers didn't have different variants, we always pick the 0.4mm variant.
        "generic_abs_175" : {
            "high"        : "two_trees_0.4_ABS_super",
            "normal"      : "two_trees_0.4_ABS_super",
            "fast"        : "two_trees_0.4_ABS_super",
            "draft"       : "two_trees_0.4_ABS_standard",
            "extra_fast"  : "two_trees_0.4_ABS_low",
            "coarse"      : "two_trees_0.4_ABS_low",
            "extra_coarse": "two_trees_0.4_ABS_low"
        },
        "generic_petg_175": {
            "high"        : "two_trees_0.4_PETG_super",
            "normal"      : "two_trees_0.4_PETG_super",
            "fast"        : "two_trees_0.4_PETG_super",
            "draft"       : "two_trees_0.4_PETG_standard",
            "extra_fast"  : "two_trees_0.4_PETG_low",
            "coarse"      : "two_trees_0.4_PETG_low",
            "extra_coarse": "two_trees_0.4_PETG_low"
        },
        "generic_pla_175" : {
            "high"        : "two_trees_0.4_PLA_super",
            "normal"      : "two_trees_0.4_PLA_super",
            "fast"        : "two_trees_0.4_PLA_super",
            "draft"       : "two_trees_0.4_PLA_standard",
            "extra_fast"  : "two_trees_0.4_PLA_low",
            "coarse"      : "two_trees_0.4_PLA_low",
            "extra_coarse": "two_trees_0.4_PLA_low"
        },
        "generic_tpu_175" : {
            "high"        : "two_trees_0.4_TPU_super",
            "normal"      : "two_trees_0.4_TPU_super",
            "fast"        : "two_trees_0.4_TPU_super",
            "draft"       : "two_trees_0.4_TPU_standard",
            "extra_fast"  : "two_trees_0.4_TPU_standard",
            "coarse"      : "two_trees_0.4_TPU_standard",
            "extra_coarse": "two_trees_0.4_TPU_standard"
        },
        "empty_material"  : {  # For the global stack.
            "high"        : "two_trees_global_super",
            "normal"      : "two_trees_global_super",
            "fast"        : "two_trees_global_super",
            "draft"       : "two_trees_global_standard",
            "extra_fast"  : "two_trees_global_low",
            "coarse"      : "two_trees_global_low",
            "extra_coarse": "two_trees_global_low"
        }
    }

    _deltacomb_quality_type_conversion = {
        "a" : "D005",
        "b" : "D010",
        "c" : "D015",
        "d" : "D020",
        "e" : "D030",
        "f" : "D045",
        "g" : "D060"
    }

    def upgradePreferences(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
        """
        Upgrades preferences to have the new version number.
        :param serialized: The original contents of the preferences file.
        :param filename: The file name of the preferences file.
        :return: A list of new file names, and a list of the new contents for
        those files.
        """
        parser = configparser.ConfigParser(interpolation = None)
        parser.read_string(serialized)

        # Update version number.
        parser["metadata"]["setting_version"] = "17"

        result = io.StringIO()
        parser.write(result)
        return [filename], [result.getvalue()]


    def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
        """Upgrades instance containers to have the new version number.

        This renames the renamed settings in the containers.
        """
        parser = configparser.ConfigParser(interpolation = None, comment_prefixes = ())
        parser.read_string(serialized)

        # Update setting version number.
        if "metadata" not in parser:
            parser["metadata"] = {}
        parser["metadata"]["setting_version"] = "17"

        if "general" not in parser:
            parser["general"] = {}
        # Certain instance containers (such as definition changes) reference to a certain definition container
        # Since a number of those changed name, we also need to update those.
        old_definition = parser.get("general", "definition", fallback = "")
        if old_definition in self._renamed_profiles:
            parser["general"]["definition"] = self._renamed_profiles[old_definition]

        # For quality-changes profiles made for TwoTrees Bluer printers, change the definition to the two_trees_base and make sure that the quality is something we have a profile for.
        if parser.get("metadata", "type", fallback = "") == "quality_changes":
            for possible_printer in self._quality_changes_to_two_trees_base:
                if os.path.basename(filename).startswith(possible_printer + "_"):
                    parser["general"]["definition"] = "two_trees_base"
                    parser["metadata"]["quality_type"] = self._two_trees_bluer_quality_type_conversion.get(parser.get("metadata", "quality_type", fallback = "fast"), "standard")
                    break

                if os.path.basename(filename).startswith("deltacomb_"):
                    parser["general"]["definition"] = "deltacomb_base"
                    parser["metadata"]["quality_type"] = self._deltacomb_quality_type_conversion.get(parser.get("metadata", "quality_type", fallback = "c"), "D015")
                    break

        result = io.StringIO()
        parser.write(result)
        return [filename], [result.getvalue()]

    def upgradeStack(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
        """Upgrades stacks to have the new version number."""

        parser = configparser.ConfigParser(interpolation = None)
        parser.read_string(serialized)

        # Update setting version number.
        if "metadata" not in parser:
            parser["metadata"] = {}
        parser["metadata"]["setting_version"] = "17"

        # Change renamed profiles.
        if "containers" in parser:
            definition_id = parser["containers"]["7"]
            if definition_id in self._quality_changes_to_two_trees_base:
                material_id = parser["containers"]["4"]
                old_quality_id = parser["containers"]["3"]
                if parser["metadata"].get("type", "machine") == "extruder_train":
                    if parser["containers"]["5"] == "empty_variant":
                        if definition_id in self._default_variants:
                            parser["containers"]["5"] = self._default_variants[definition_id]  # For legacy TwoTrees printers, change the variant to 0.4.

                # Also change the quality to go along with it.
                if material_id in self._two_trees_quality_per_material and old_quality_id in self._two_trees_quality_per_material[material_id]:
                    parser["containers"]["3"] = self._two_trees_quality_per_material[material_id][old_quality_id]
                stack_copy = {}  # type: Dict[str, str]  # Make a copy so that we don't modify the dict we're iterating over.
                stack_copy.update(parser["containers"])
                for position, profile_id in stack_copy.items():
                    if profile_id in self._renamed_profiles:
                        parser["containers"][position] = self._renamed_profiles[profile_id]

        result = io.StringIO()
        parser.write(result)
        return [filename], [result.getvalue()]