<commit_before>__author__ = "AstroPrint Product Team <<EMAIL>>"
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
__copyright__ = "Copyright (C) 2016-2020 3DaGoGo, Inc - Released under terms of the AGPLv3 License"

import logging

from copy import copy

class MaterialCounter(object):
	#Extrusion modes
	EXTRUSION_MODE_ABSOLUTE = 1
	EXTRUSION_MODE_RELATIVE = 2

	def __init__(self):
		self._logger = logging.getLogger(__name__)
		self._extrusionMode = self.EXTRUSION_MODE_ABSOLUTE
		self._activeTool = "0"
		self._lastExtruderLengthReset = {"0": 0}
		self._consumedFilament = {"0": 0}
		self._lastExtrusion = {"0": 0}

	def startPrint(self):
		tool = self._activeTool

		self._lastExtruderLengthReset = {tool: 0}
		self._consumedFilament = {tool: 0}
		self._lastExtrusion = {tool: 0}

	@property
	def extrusionMode(self):
		return self._extrusionMode

	@property
	def consumedFilament(self):
		if self._consumedFilament and self._extrusionMode == self.EXTRUSION_MODE_ABSOLUTE:
			tool = self._activeTool
			consumedFilament = copy(self._consumedFilament)

			try:
				consumedFilament[tool] += ( self._lastExtrusion[tool] - self._lastExtruderLengthReset[tool] )

			except KeyError:
				return None

			return consumedFilament

		else:
			return { k: max(v,0) for k, v in self._consumedFilament.items() } #It can be negative because of retraction but we can't "consume" negative filament

	@property
	def totalConsumedFilament(self):
		consumedFilament = self.consumedFilament
		return sum([consumedFilament[k] for k in consumedFilament.keys()])

	def changeActiveTool(self, newTool, oldTool):
		#Make sure the head is registered
		if newTool not in self._consumedFilament:
			self._consumedFilament[newTool] = 0
			self._lastExtruderLengthReset[newTool] = 0
			self._lastExtrusion[newTool] = 0

		if self._extrusionMode == self.EXTRUSION_MODE_ABSOLUTE:
			if oldTool in self.consumedFilament and oldTool in self._lastExtrusion and oldTool in self._lastExtruderLengthReset:
				self.consumedFilament[oldTool] += ( self._lastExtrusion[oldTool] - self._lastExtruderLengthReset[oldTool] )
				self._lastExtruderLengthReset[oldTool] = self.consumedFilament[oldTool]
			else:
				self._logger.error('Unkonwn previous tool %s when trying to change to new tool %s' % (oldTool, newTool))

		self._activeTool = newTool

	def changeExtrusionMode(self, mode):
		self._extrusionMode = mode

		if mode == self.EXTRUSION_MODE_RELATIVE:
			tool = self._activeTool

			#it was absolute before so we add what we had to the active head counter
			self._consumedFilament[tool] += ( self._lastExtrusion[tool] - self._lastExtruderLengthReset[tool] )

	def resetExtruderLength(self, newLength):
		tool = self._activeTool

		if self._extrusionMode == self.EXTRUSION_MODE_ABSOLUTE:
			# We add what we have to the total for the his tool head
			self._consumedFilament[tool] += ( self._lastExtrusion[tool] - self._lastExtruderLengthReset[tool] )

		self._lastExtruderLengthReset[tool] = newLength
		self._lastExtrusion[tool] = newLength

	def reportExtrusion(self, length):
		if self._extrusionMode == self.EXTRUSION_MODE_RELATIVE:
			#if length > 0: #never report retractions
			self._consumedFilament[self._activeTool] += length

		else: # EXTRUSION_MODE_ABSOLUTE
			tool = self._activeTool

			if length > self._lastExtrusion[tool]: #never report retractions
				self._lastExtrusion[tool] = length
<commit_msg>we can't ignote retractions<commit_after>__author__ = "AstroPrint Product Team <<EMAIL>>"
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
__copyright__ = "Copyright (C) 2016-2020 3DaGoGo, Inc - Released under terms of the AGPLv3 License"

import logging

from copy import copy

class MaterialCounter(object):
	#Extrusion modes
	EXTRUSION_MODE_ABSOLUTE = 1
	EXTRUSION_MODE_RELATIVE = 2

	def __init__(self):
		self._logger = logging.getLogger(__name__)
		self._extrusionMode = self.EXTRUSION_MODE_ABSOLUTE
		self._activeTool = "0"
		self._lastExtruderLengthReset = {"0": 0}
		self._consumedFilament = {"0": 0}
		self._lastExtrusion = {"0": 0}

	def startPrint(self):
		tool = self._activeTool

		self._lastExtruderLengthReset = {tool: 0}
		self._consumedFilament = {tool: 0}
		self._lastExtrusion = {tool: 0}

	@property
	def extrusionMode(self):
		return self._extrusionMode

	@property
	def consumedFilament(self):
		if self._consumedFilament and self._extrusionMode == self.EXTRUSION_MODE_ABSOLUTE:
			tool = self._activeTool
			consumedFilament = copy(self._consumedFilament)

			try:
				consumedFilament[tool] += ( self._lastExtrusion[tool] - self._lastExtruderLengthReset[tool] )

			except KeyError:
				return None

			return consumedFilament

		else:
			return { k: max(v,0) for k, v in self._consumedFilament.items() } #It can be negative because of retraction but we can't "consume" negative filament

	@property
	def totalConsumedFilament(self):
		consumedFilament = self.consumedFilament
		return sum([consumedFilament[k] for k in consumedFilament.keys()])

	def changeActiveTool(self, newTool, oldTool):
		#Make sure the head is registered
		if newTool not in self._consumedFilament:
			self._consumedFilament[newTool] = 0
			self._lastExtruderLengthReset[newTool] = 0
			self._lastExtrusion[newTool] = 0

		if self._extrusionMode == self.EXTRUSION_MODE_ABSOLUTE:
			if oldTool in self.consumedFilament and oldTool in self._lastExtrusion and oldTool in self._lastExtruderLengthReset:
				self.consumedFilament[oldTool] += ( self._lastExtrusion[oldTool] - self._lastExtruderLengthReset[oldTool] )
				self._lastExtruderLengthReset[oldTool] = self.consumedFilament[oldTool]
			else:
				self._logger.error('Unkonwn previous tool %s when trying to change to new tool %s' % (oldTool, newTool))

		self._activeTool = newTool

	def changeExtrusionMode(self, mode):
		self._extrusionMode = mode

		if mode == self.EXTRUSION_MODE_RELATIVE:
			tool = self._activeTool

			#it was absolute before so we add what we had to the active head counter
			self._consumedFilament[tool] += ( self._lastExtrusion[tool] - self._lastExtruderLengthReset[tool] )

	def resetExtruderLength(self, newLength):
		tool = self._activeTool

		if self._extrusionMode == self.EXTRUSION_MODE_ABSOLUTE:
			# We add what we have to the total for the his tool head
			self._consumedFilament[tool] += ( self._lastExtrusion[tool] - self._lastExtruderLengthReset[tool] )

		self._lastExtruderLengthReset[tool] = newLength
		self._lastExtrusion[tool] = newLength

	def reportExtrusion(self, length):
		if self._extrusionMode == self.EXTRUSION_MODE_RELATIVE:
			#if length > 0: #never report retractions
			self._consumedFilament[self._activeTool] += length

		else: # EXTRUSION_MODE_ABSOLUTE
			tool = self._activeTool

			#if length > self._lastExtrusion[tool]: #never report retractions
			self._lastExtrusion[tool] = length

{'fraction_non_alphanumeric': 0.09263035246140403, 'fraction_numerical': 0.007864841246722983, 'mean_word_length': 4.884318766066838, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4906670', 'n_tokens_mistral': 2555, 'n_tokens_neox': 2265, 'n_words': 541}