Make things neater.

CURA-2953 Version upgrade 2.3 to 2.4
This commit is contained in:
Simon Edwards 2016-12-05 13:31:24 +01:00
parent 13af609830
commit 724cc99f59

View file

@ -11,6 +11,11 @@ from UM.VersionUpgrade import VersionUpgrade # Superclass of the plugin.
class VersionUpgrade22to24(VersionUpgrade): class VersionUpgrade22to24(VersionUpgrade):
def upgradeMachineInstance(self, serialised, filename): 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 = configparser.ConfigParser(interpolation = None)
config.read_string(serialised) # Read the input string as config file. config.read_string(serialised) # Read the input string as config file.
config.set("general", "version", "3") config.set("general", "version", "3")
@ -26,32 +31,8 @@ class VersionUpgrade22to24(VersionUpgrade):
if len(user_variant_names): if len(user_variant_names):
# One of the user defined variants appears in the list of containers in the stack. # One of the user defined variants appears in the list of containers in the stack.
for variant_name in user_variant_names: for variant_name in user_variant_names: # really there should just be one variant to convert.
# Copy the variant to the machine_instances/*_settings.inst.cfg config_name = self.__convertVariant(name_path_dict.get(variant_name))
variant_config = configparser.ConfigParser(interpolation=None)
variant_path = name_path_dict.get(variant_name)
with open(variant_path, "r") as fhandle:
variant_config.read_file(fhandle)
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")
# "_settings.inst.cfg"
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)
# Change the name of variant and insert empty_variant into the stack. # Change the name of variant and insert empty_variant into the stack.
new_container_list = [] new_container_list = []
@ -70,6 +51,33 @@ class VersionUpgrade22to24(VersionUpgrade):
config.write(output) config.write(output)
return [filename], [output.getvalue()] 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)
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): def __getUserVariants(self):
resource_path = Resources.getDataStoragePath() resource_path = Resources.getDataStoragePath()
variants_dir = os.path.join(resource_path, "variants") variants_dir = os.path.join(resource_path, "variants")