<commit_before># Copyright (c) 2016 <NAME>.
# Cura is released under the terms of the AGPLv3 or higher.

import math
import copy
import io
import xml.etree.ElementTree as ET
import uuid

from UM.Logger import Logger

import UM.Dictionary

import UM.Settings

##  Handles serializing and deserializing material containers from an XML file
class XmlMaterialProfile(UM.Settings.InstanceContainer):
    def __init__(self, container_id, *args, **kwargs):
        super().__init__(container_id, *args, **kwargs)

    ##  Overridden from InstanceContainer
    def duplicate(self, new_id, new_name = None):
        base_file = self.getMetaDataEntry("base_file", None)
        new_uuid = str(uuid.uuid4())

        if base_file:
            containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = base_file)
            if containers:
                new_basefile = containers[0].duplicate(self.getMetaDataEntry("brand") + "_" + new_id, new_name)
                new_basefile.setMetaDataEntry("GUID", new_uuid)
                base_file = new_basefile.id
                UM.Settings.ContainerRegistry.getInstance().addContainer(new_basefile)

                new_id = self.getMetaDataEntry("brand") + "_" + new_id + "_" + self.getDefinition().getId()
                variant = self.getMetaDataEntry("variant")
                if variant:
                    variant_containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = variant)
                    if variant_containers:
                        new_id += "_" + variant_containers[0].getName().replace(" ", "_")

        result = super().duplicate(new_id, new_name)
        result.setMetaDataEntry("GUID", new_uuid)
        if result.getMetaDataEntry("base_file", None):
            result.setMetaDataEntry("base_file", base_file)
        return result

    ##  Overridden from InstanceContainer
    def setReadOnly(self, read_only):
        super().setReadOnly(read_only)

        for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(GUID = self.getMetaDataEntry("GUID")):
            container._read_only = read_only

    ##  Overridden from InstanceContainer
    def setMetaDataEntry(self, key, value):
        if self.isReadOnly():
            return

        super().setMetaDataEntry(key, value)

        for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(GUID = self.getMetaDataEntry("GUID")):
            container.setMetaData(copy.deepcopy(self._metadata))

    ##  Overridden from InstanceContainer
    def setProperty(self, key, property_name, property_value, container = None):
        if self.isReadOnly():
            return

        super().setProperty(key, property_name, property_value)

        for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(GUID = self.getMetaDataEntry("GUID")):
            container._dirty = True

    ##  Overridden from InstanceContainer
    def serialize(self):
        registry = UM.Settings.ContainerRegistry.getInstance()
<commit_msg>Update material container's name when its "material" metadata key changes<commit_after># Copyright (c) 2016 <NAME>.
# Cura is released under the terms of the AGPLv3 or higher.

import math
import copy
import io
import xml.etree.ElementTree as ET
import uuid

from UM.Logger import Logger

import UM.Dictionary

import UM.Settings

##  Handles serializing and deserializing material containers from an XML file
class XmlMaterialProfile(UM.Settings.InstanceContainer):
    def __init__(self, container_id, *args, **kwargs):
        super().__init__(container_id, *args, **kwargs)

    ##  Overridden from InstanceContainer
    def duplicate(self, new_id, new_name = None):
        base_file = self.getMetaDataEntry("base_file", None)
        new_uuid = str(uuid.uuid4())

        if base_file:
            containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = base_file)
            if containers:
                new_basefile = containers[0].duplicate(self.getMetaDataEntry("brand") + "_" + new_id, new_name)
                new_basefile.setMetaDataEntry("GUID", new_uuid)
                base_file = new_basefile.id
                UM.Settings.ContainerRegistry.getInstance().addContainer(new_basefile)

                new_id = self.getMetaDataEntry("brand") + "_" + new_id + "_" + self.getDefinition().getId()
                variant = self.getMetaDataEntry("variant")
                if variant:
                    variant_containers = UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(id = variant)
                    if variant_containers:
                        new_id += "_" + variant_containers[0].getName().replace(" ", "_")

        result = super().duplicate(new_id, new_name)
        result.setMetaDataEntry("GUID", new_uuid)
        if result.getMetaDataEntry("base_file", None):
            result.setMetaDataEntry("base_file", base_file)
        return result

    ##  Overridden from InstanceContainer
    def setReadOnly(self, read_only):
        super().setReadOnly(read_only)

        for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(GUID = self.getMetaDataEntry("GUID")):
            container._read_only = read_only

    ##  Overridden from InstanceContainer
    def setMetaDataEntry(self, key, value):
        if self.isReadOnly():
            return

        super().setMetaDataEntry(key, value)

        if key == "material":
            self.setName(value)

        for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(GUID = self.getMetaDataEntry("GUID")):
            container.setMetaData(copy.deepcopy(self._metadata))
            if key == "material":
                container.setName(value)

    ##  Overridden from InstanceContainer
    def setProperty(self, key, property_name, property_value, container = None):
        if self.isReadOnly():
            return

        super().setProperty(key, property_name, property_value)

        for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(GUID = self.getMetaDataEntry("GUID")):
            container._dirty = True

    ##  Overridden from InstanceContainer
    def serialize(self):
        registry = UM.Settings.ContainerRegistry.getInstance()

{'fraction_non_alphanumeric': 0.09896161107614852, 'fraction_numerical': 0.0025173064820641915, 'mean_word_length': 3.0157927984838913, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5100468', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1688, 'n_words': 365}