Example #1
0
    def test_profiles(self, machine_manager):
        profile_1 = Profile(machine_manager)
        profile_2 = Profile(machine_manager)
        definition = MachineDefinition(
            machine_manager,
            self._getDefinitionsFilePath("simple_machine.json"))
        definition.loadMetaData()
        machine_manager.addMachineDefinition(definition)

        machine_instance = MachineInstance(machine_manager,
                                           definition=definition,
                                           name="Basic Test")
        machine_instance.loadFromFile(
            self._getInstancesFilePath("simple_machine.cfg"))
        machine_manager.addMachineInstance(machine_instance)
        profile_1._active_instance = machine_instance
        profile_2._active_instance = machine_instance

        profile_1.loadFromFile(
            self._getProfileFilePath("simple_machine_with_overrides.cfg"))
        profile_2.loadFromFile(
            self._getProfileFilePath("simple_machine_with_overrides.cfg"))
        machine_manager.addProfile(profile_1)
        assert machine_manager.getProfiles() == [profile_1]

        # Check if adding again has no effect
        machine_manager.addProfile(profile_1)
        assert machine_manager.getProfiles() == [profile_1]

        # Check that adding another profile with same name does not work
        with pytest.raises(DuplicateProfileError):
            machine_manager.addProfile(profile_2)

        # Changing the name and then adding it should work
        profile_2.setName("test")
        machine_manager.addProfile(profile_2)
        assert profile_1 in machine_manager.getProfiles(
        ) and profile_2 in machine_manager.getProfiles()

        assert machine_manager.findProfile("test") == profile_2

        # Check if removing one of the profiles works
        machine_manager.removeProfile(profile_1)
        assert machine_manager.getProfiles() == [profile_2]

        machine_manager.setActiveProfile(profile_2)
        assert machine_manager.getActiveProfile() == profile_2

        machine_manager.removeProfile(profile_2)

        assert machine_manager.getProfiles() == []
Example #2
0
    def test_profiles(self, machine_manager):
        profile_1 = Profile(machine_manager)
        profile_2 = Profile(machine_manager)
        definition = MachineDefinition(machine_manager, self._getDefinitionsFilePath("simple_machine.json"))
        definition.loadMetaData()
        machine_manager.addMachineDefinition(definition)

        machine_instance = MachineInstance(machine_manager, definition = definition, name = "Basic Test")
        machine_instance.loadFromFile(self._getInstancesFilePath("simple_machine.cfg"))
        machine_manager.addMachineInstance(machine_instance)
        profile_1._active_instance = machine_instance
        profile_2._active_instance = machine_instance

        profile_1.loadFromFile(self._getProfileFilePath("simple_machine_with_overrides.cfg"))
        profile_2.loadFromFile(self._getProfileFilePath("simple_machine_with_overrides.cfg"))
        machine_manager.addProfile(profile_1)
        assert machine_manager.getProfiles() == [profile_1]

        # Check if adding again has no effect
        machine_manager.addProfile(profile_1)
        assert machine_manager.getProfiles() == [profile_1]

        # Check that adding another profile with same name does not work
        with pytest.raises(DuplicateProfileError):
            machine_manager.addProfile(profile_2)

        # Changing the name and then adding it should work
        profile_2.setName("test")
        machine_manager.addProfile(profile_2)
        assert profile_1 in machine_manager.getProfiles() and profile_2 in machine_manager.getProfiles()

        assert machine_manager.findProfile("test") == profile_2

        # Check if removing one of the profiles works
        machine_manager.removeProfile(profile_1)
        assert machine_manager.getProfiles() == [profile_2]

        machine_manager.setActiveProfile(profile_2)
        assert machine_manager.getActiveProfile() == profile_2

        machine_manager.removeProfile(profile_2)
        
        assert machine_manager.getProfiles() == []
Example #3
0
    def importProfile(self, url):
        path = url.toLocalFile()
        if not path:
            return

        profile = Profile(self._manager, read_only=False)
        try:
            profile.loadFromFile(path)
            self._manager.addProfile(profile)
        except SettingsError.DuplicateProfileError as e:
            count = 2
            name = "{0} {1}".format(profile.getName(), count)
            while (self._manager.findProfile(name) != None):
                count += 1
                name = "{0} {1}".format(profile.getName(), count)
            profile.setName(name)
            self._manager.addProfile(profile)
            return {
                "status":
                "duplicate",
                "message":
                catalog.i18nc("@info:status", "Profile was imported as {0}",
                              name)
            }
        except Exception as e:
            return {
                "status":
                "error",
                "message":
                catalog.i18nc(
                    "@info:status",
                    "Failed to import profile from file <filename>{0}</filename>: <message>{1}</message>",
                    path, str(e))
            }
        else:
            return {
                "status":
                "ok",
                "message":
                catalog.i18nc("@info:status",
                              "Successfully imported profile {0}",
                              profile.getName())
            }
Example #4
0
    def importProfile(self, url):
        path = url.toLocalFile()
        if not path:
            return

        profile = Profile(self._manager, read_only = False)
        try:
            profile.loadFromFile(path)
            self._manager.addProfile(profile)
        except SettingsError.DuplicateProfileError as e:
            count = 2
            name = "{0} {1}".format(profile.getName(), count)
            while(self._manager.findProfile(name) != None):
                count += 1
                name = "{0} {1}".format(profile.getName(), count)
            profile.setName(name)
            self._manager.addProfile(profile)
            return { "status": "duplicate", "message": catalog.i18nc("@info:status", "Profile was imported as {0}", name) }
        except Exception as e:
            return { "status": "error", "message": catalog.i18nc("@info:status", "Failed to import profile from file <filename>{0}</filename>: <message>{1}</message>", path, str(e)) }
        else:
            return { "status": "ok", "message": catalog.i18nc("@info:status", "Successfully imported profile {0}", profile.getName()) }
Example #5
0
    def read(self, file_name):
        Logger.log("i", "Importing legacy profile from file " + file_name + ".")
        profile = Profile(machine_manager = Application.getInstance().getMachineManager(), read_only = False) #Create an empty profile.
        profile.setName("Imported Legacy Profile")

        parser = configparser.ConfigParser(interpolation = None)
        try:
            with open(file_name) as f:
                parser.readfp(f) #Parse the INI file.
        except Exception as e:
            Logger.log("e", "Unable to open legacy profile %s: %s", file_name, str(e))
            return None

        #Legacy Cura saved the profile under the section "profile_N" where N is the ID of a machine, except when you export in which case it saves it in the section "profile".
        #Since importing multiple machine profiles is out of scope, just import the first section we find.
        section = ""
        for found_section in parser.sections():
            if found_section.startswith("profile"):
                section = found_section
                break
        if not section: #No section starting with "profile" was found. Probably not a proper INI file.
            return None

        try:
            with open(os.path.join(PluginRegistry.getInstance().getPluginPath("LegacyProfileReader"), "DictionaryOfDoom.json"), "r", -1, "utf-8") as f:
                dict_of_doom = json.load(f) #Parse the Dictionary of Doom.
        except IOError as e:
            Logger.log("e", "Could not open DictionaryOfDoom.json for reading: %s", str(e))
            return None
        except Exception as e:
            Logger.log("e", "Could not parse DictionaryOfDoom.json: %s", str(e))
            return None

        defaults = self.prepareDefaults(dict_of_doom)
        legacy_settings = self.prepareLocals(parser, section, defaults) #Gets the settings from the legacy profile.

        #Check the target version in the Dictionary of Doom with this application version.
        if "target_version" not in dict_of_doom:
            Logger.log("e", "Dictionary of Doom has no target version. Is it the correct JSON file?")
            return None
        if Profile.ProfileVersion != dict_of_doom["target_version"]:
            Logger.log("e", "Dictionary of Doom of legacy profile reader (version %s) is not in sync with the profile version (version %s)!", dict_of_doom["target_version"], str(Profile.ProfileVersion))
            return None

        if "translation" not in dict_of_doom:
            Logger.log("e", "Dictionary of Doom has no translation. Is it the correct JSON file?")
            return None
        for new_setting in dict_of_doom["translation"]: #Evaluate all new settings that would get a value from the translations.
            old_setting_expression = dict_of_doom["translation"][new_setting]
            compiled = compile(old_setting_expression, new_setting, "eval")
            try:
                new_value = eval(compiled, {"math": math}, legacy_settings) #Pass the legacy settings as local variables to allow access to in the evaluation.
                value_using_defaults = eval(compiled, {"math": math}, defaults) #Evaluate again using only the default values to try to see if they are default.
            except Exception as e: #Probably some setting name that was missing or something else that went wrong in the ini file.
                Logger.log("w", "Setting " + new_setting + " could not be set because the evaluation failed. Something is probably missing from the imported legacy profile.")
                continue
            if new_value != value_using_defaults and profile.getSettingValue(new_setting) != new_value: #Not equal to the default in the new Cura OR the default in the legacy Cura.
                profile.setSettingValue(new_setting, new_value) #Store the setting in the profile!

        if len(profile.getChangedSettings()) == 0:
            Logger.log("i", "A legacy profile was imported but everything evaluates to the defaults, creating an empty profile.")

        return profile