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

from PyQt5.QtCore import pyqtSignal, pyqtProperty, pyqtSlot, QObject

import UM.Application #To get the global container stack to find the current machine.
import UM.Logger
import UM.Settings.ContainerRegistry #Finding containers by ID.
import UM.Signal #To notify other components of changes in the extruders.


##  Class that handles the current extruder stack.
#
#   This finds the extruders that are available for the currently active machine
#   and makes sure that whenever the machine is swapped, this list is kept up to
#   date. It also contains and updates the setting stacks for the extruders.
class ExtruderManager(QObject):
    ##  The singleton instance of this manager.
    __instance = None

    ##  Signal to notify other components when the list of extruders changes.
    extrudersChanged = UM.Signal()

    ##  Notify when the user switches the currently active extruder.
    activeExtruderChanged = pyqtSignal()

    ##  Registers listeners and such to listen to changes to the extruders.
    def __init__(self, parent = None):
        super().__init__(parent)
        self._extruder_trains = { } #Extruders for the current machine.
        self._next_item = 0 #For when you use this class as iterator.
        self._active_extruder_index = 0

        self._repopulate()

    ##  Gets an instance of this extruder manager.
    #
    #   If an instance was already created, the old instance is returned. This
    #   implements the singleton pattern.
    @classmethod
    def getInstance(cls):
        if not cls.__instance:
            cls.__instance = ExtruderManager()
        return cls.__instance

    ##  Creates an iterator over the extruders in this manager.
    #
    #   \return An iterator over the extruders in this manager.
    def __iter__(self):
        return iter(self._extruders)

    @pyqtProperty(str, notify = activeExtruderChanged)
    def activeExtruderStackId(self):
        if UM.Application.getInstance().getGlobalContainerStack():
            try:
                return self._extruder_trains[UM.Application.getInstance().getGlobalContainerStack().getId()][str(self._active_extruder_index)]
            except KeyError:
                pass

    @pyqtSlot(int)
    def setActiveExtruderIndex(self, index):
        self._active_extruder_index = index
        self.activeExtruderChanged.emit()

    ##  (Re)populates the collections of extruders by machine.
    def _repopulate(self):
        self._extruder_trains = { }
        if not UM.Application.getInstance().getGlobalContainerStack(): #No machine has been added yet.
            self.extrudersChanged.emit() #Yes, we just cleared the _extruders list!
            return #Then leave them empty!

        extruder_trains = UM.Settings.ContainerRegistry.getInstance().findContainerStacks(type = "extruder_train")
        for extruder_train in extruder_trains:
            machine_id = extruder_train.getMetaDataEntry("machine")
            if not machine_id:
                continue
            if machine_id not in self._extruder_trains:
                self._extruder_trains[machine_id] = { }
            self._extruder_trains[machine_id][extruder_train.getMetaDataEntry("position")] = extruder_train.getId()
        self.extrudersChanged.emit()

    def createExtruderTrain(self, extruder_definition, machine_definition, extruder_id):<commit_msg>Explicitly return None if no global container stack<commit_after># Copyright (c) 2016 Ultimaker B.V.
# Cura is released under the terms of the AGPLv3 or higher.

from PyQt5.QtCore import pyqtSignal, pyqtProperty, pyqtSlot, QObject

import UM.Application #To get the global container stack to find the current machine.
import UM.Logger
import UM.Settings.ContainerRegistry #Finding containers by ID.
import UM.Signal #To notify other components of changes in the extruders.


##  Class that handles the current extruder stack.
#
#   This finds the extruders that are available for the currently active machine
#   and makes sure that whenever the machine is swapped, this list is kept up to
#   date. It also contains and updates the setting stacks for the extruders.
class ExtruderManager(QObject):
    ##  The singleton instance of this manager.
    __instance = None

    ##  Signal to notify other components when the list of extruders changes.
    extrudersChanged = UM.Signal()

    ##  Notify when the user switches the currently active extruder.
    activeExtruderChanged = pyqtSignal()

    ##  Registers listeners and such to listen to changes to the extruders.
    def __init__(self, parent = None):
        super().__init__(parent)
        self._extruder_trains = { } #Extruders for the current machine.
        self._next_item = 0 #For when you use this class as iterator.
        self._active_extruder_index = 0

        self._repopulate()

    ##  Gets an instance of this extruder manager.
    #
    #   If an instance was already created, the old instance is returned. This
    #   implements the singleton pattern.
    @classmethod
    def getInstance(cls):
        if not cls.__instance:
            cls.__instance = ExtruderManager()
        return cls.__instance

    ##  Creates an iterator over the extruders in this manager.
    #
    #   \return An iterator over the extruders in this manager.
    def __iter__(self):
        return iter(self._extruders)

    @pyqtProperty(str, notify = activeExtruderChanged)
    def activeExtruderStackId(self):
        if not UM.Application.getInstance().getGlobalContainerStack():
            return None #Whatta ya gonna do?
        try:
            return self._extruder_trains[UM.Application.getInstance().getGlobalContainerStack().getId()][str(self._active_extruder_index)]
        except KeyError:
            return None

    @pyqtSlot(int)
    def setActiveExtruderIndex(self, index):
        self._active_extruder_index = index
        self.activeExtruderChanged.emit()

    ##  (Re)populates the collections of extruders by machine.
    def _repopulate(self):
        self._extruder_trains = { }
        if not UM.Application.getInstance().getGlobalContainerStack(): #No machine has been added yet.
            self.extrudersChanged.emit() #Yes, we just cleared the _extruders list!
            return #Then leave them empty!

        extruder_trains = UM.Settings.ContainerRegistry.getInstance().findContainerStacks(type = "extruder_train")
        for extruder_train in extruder_trains:
            machine_id = extruder_train.getMetaDataEntry("machine")
            if not machine_id:
                continue
            if machine_id not in self._extruder_trains:
                self._extruder_trains[machine_id] = { }
            self._extruder_trains[machine_id][extruder_train.getMetaDataEntry("position")] = extruder_train.getId()
        self.extrudersChanged.emit()

    def createExtruderTrain(self, extruder_definition, machine_definition, extruder_id):
{'fraction_non_alphanumeric': 0.0713774853382921, 'fraction_numerical': 0.0022886568445143756, 'mean_word_length': 3.0984759671746778, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2847027', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1896, 'n_words': 624}