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

VersionUpgrade.py « VersionUpgrade22to24 « VersionUpgrade « plugins - github.com/Ultimaker/Cura.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e1da456c48f1f80beea41da8f1c70848d3c212e9 (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
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
# Copyright (c) 2016 Ultimaker B.V.
# Cura is released under the terms of the AGPLv3 or higher.

import configparser #To get version numbers from config files.
import os
import os.path
import io

from UM.Resources import Resources
from UM.VersionUpgrade import VersionUpgrade # Superclass of the plugin.
import UM.VersionUpgrade

class VersionUpgrade22to24(VersionUpgrade):

    def upgradeMachineInstance(self, serialised, filename):
        # All of this is needed to upgrade custom variant machines from old Cura to 2.4 where
        # `definition_changes` instance container has been introduced. Variant files which
        # look like the the handy work of the old machine settings plugin are converted directly
        # on disk.

        config = configparser.ConfigParser(interpolation = None)
        config.read_string(serialised) # Read the input string as config file.
        if config.get("metadata", "type") == "definition_changes":
            # This is not a container stack, don't upgrade it here
            return

        config.set("general", "version", "3")

        container_list = []
        if config.has_section("containers"):
            for index, container_id in config.items("containers"):
                container_list.append(container_id)
        elif config.has_option("general", "containers"):
            containers = config.get("general", "containers")
            container_list = containers.split(",")

        user_variants = self.__getUserVariants()
        name_path_dict = {}
        for variant in user_variants:
            name_path_dict[variant.get("name")] = variant.get("path")

        user_variant_names = set(container_list).intersection(name_path_dict.keys())
        if len(user_variant_names):
            # One of the user defined variants appears in the list of containers in the stack.

            for variant_name in user_variant_names: # really there should just be one variant to convert.
                config_name = self.__convertVariant(name_path_dict.get(variant_name))

                # Change the name of variant and insert empty_variant into the stack.
                new_container_list = []
                for item in container_list:
                    if not item: # the last item may be an empty string
                        continue
                    if item == variant_name:
                        new_container_list.append("empty_variant")
                        new_container_list.append(config_name)
                    else:
                        new_container_list.append(item)

                container_list = new_container_list

            if not config.has_section("containers"):
                config.add_section("containers")

            config.remove_option("general", "containers")

            for index in range(len(container_list)):
                config.set("containers", str(index), container_list[index])

        output = io.StringIO()
        config.write(output)
        return [filename], [output.getvalue()]

    def __convertVariant(self, variant_path):
        # Copy the variant to the machine_instances/*_settings.inst.cfg
        variant_config = configparser.ConfigParser(interpolation=None)
        with open(variant_path, "r") as fhandle:
            variant_config.read_file(fhandle)

        config_name = "Unknown Variant"
        if variant_config.has_section("general") and variant_config.has_option("general", "name"):
            config_name = variant_config.get("general", "name")
            if config_name.endswith("_variant"):
                config_name = config_name[:-len("_variant")] + "_settings"
                variant_config.set("general", "name", config_name)

        if not variant_config.has_section("metadata"):
            variant_config.add_section("metadata")
        variant_config.set("metadata", "type", "definition_changes")

        resource_path = Resources.getDataStoragePath()
        machine_instances_dir = os.path.join(resource_path, "machine_instances")

        if variant_path.endswith("_variant.inst.cfg"):
            variant_path = variant_path[:-len("_variant.inst.cfg")] + "_settings.inst.cfg"

        with open(os.path.join(machine_instances_dir, os.path.basename(variant_path)), "w") as fp:
            variant_config.write(fp)

        return config_name

    def __getUserVariants(self):
        resource_path = Resources.getDataStoragePath()
        variants_dir = os.path.join(resource_path, "variants")

        result = []
        for entry in os.scandir(variants_dir):
            if entry.name.endswith('.inst.cfg') and entry.is_file():
                config = configparser.ConfigParser(interpolation = None)
                with open(entry.path, "r") as fhandle:
                    config.read_file(fhandle)
                if config.has_section("general") and config.has_option("general", "name"):
                    result.append( { "path": entry.path, "name": config.get("general", "name") } )
        return result

    def upgradeExtruderTrain(self, serialised, filename):
        config = configparser.ConfigParser(interpolation = None)
        config.read_string(serialised) # Read the input string as config file.
        config.set("general", "version", "3")   # Just bump the version number. That is all we need for now.

        output = io.StringIO()
        config.write(output)
        return [filename], [output.getvalue()]

    def upgradePreferences(self, serialised, filename):
        config = configparser.ConfigParser(interpolation = None)
        config.read_string(serialised)

        if not config.has_section("general"):
            raise UM.VersionUpgrade.FormatException("No \"general\" section.")

        # Make z_seam_x and z_seam_y options visible. In a clean 2.4 they are visible by default.
        if config.has_option("general", "visible_settings"):
            visible_settings = config.get("general", "visible_settings")
            visible_set = set(visible_settings.split(";"))
            visible_set.add("z_seam_x")
            visible_set.add("z_seam_y")
            config.set("general", "visible_settings", ";".join(visible_set))
        config.set("general", "version", value="4")

        output = io.StringIO()
        config.write(output)
        return [filename], [output.getvalue()]

    def getCfgVersion(self, serialised):
        parser = configparser.ConfigParser(interpolation = None)
        parser.read_string(serialised)
        format_version = int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
        setting_version = int(parser.get("general", "version", fallback = 0))
        return format_version * 1000000 + setting_version