<commit_before>__copyright__ = "Copyright (C) 2013 <NAME> - Released under terms of the AGPLv3 License"

import wx
import power
import time
import sys
import os
import ctypes

#TODO: This does not belong here!
if sys.platform.startswith('win'):
	def preventComputerFromSleeping(prevent):
		"""
		Function used to prevent the computer from going into sleep mode.
		:param prevent: True = Prevent the system from going to sleep from this point on.
		:param prevent: False = No longer prevent the system from going to sleep.
		"""
		ES_CONTINUOUS = 0x80000000
		ES_SYSTEM_REQUIRED = 0x00000001
		#SetThreadExecutionState returns 0 when failed, which is ignored. The function should be supported from windows XP and up.
		if prevent:
			ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED)
		else:
			ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS)

else:
	def preventComputerFromSleeping(prevent):
		#No preventComputerFromSleeping for MacOS and Linux yet.
		pass

class printWindowPlugin(wx.Frame):
	def __init__(self, parent, printerConnection, filename):
		super(printWindowPlugin, self).__init__(parent, -1, style=wx.CLOSE_BOX|wx.CLIP_CHILDREN|wx.CAPTION|wx.SYSTEM_MENU|wx.FRAME_FLOAT_ON_PARENT|wx.MINIMIZE_BOX, title=_("Printing on %s") % (printerConnection.getName()))
		self._printerConnection = printerConnection
		self._basePath = os.path.dirname(filename)
		self._backgroundImage = None
		self._colorCommandMap = {}
		self._buttonList = []
		self._termLog = None
		self._termInput = None
		self._termHistory = []
		self._termHistoryIdx = 0
		self._progressBar = None
		self._tempGraph = None
		self._infoText = None
		self._lastUpdateTime = time.time()

		variables = {
			'setImage': self.script_setImage,
			'addColorCommand': self.script_addColorCommand,
			'addTerminal': self.script_addTerminal,
			'addTemperatureGraph': self.script_addTemperatureGraph,
			'addProgressbar': self.script_addProgressbar,
			'addButton': self.script_addButton,
			'addSpinner': self.script_addSpinner,
			'addTextButton': self.script_addTextButton,

			'sendGCode': self.script_sendGCode,
			'connect': self.script_connect,
			'startPrint': self.script_startPrint,
			'pausePrint': self.script_pausePrint,
			'cancelPrint': self.script_cancelPrint,
			'showErrorLog': self.script_showErrorLog,
		}
		execfile(filename, variables, variables)

		self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
		self.Bind(wx.EVT_PAINT, self.OnDraw)
		self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftClick)
		self.Bind(wx.EVT_CLOSE, self.OnClose)

		self._updateButtonStates()

		self._printerConnection.addCallback(self._doPrinterConnectionUpdate)

		if self._printerConnection.hasActiveConnection() and not self._printerConnection.isActiveConnectionOpen():
			self._printerConnection.openActiveConnection()
		preventComputerFromSleeping(True)

	def script_setImage(self, guiImage, mapImage):
		self._backgroundImage = wx.BitmapFromImage(wx.Image(os.path.join(self._basePath, guiImage)))
		self._mapImage = wx.Image(os.path.join(self._basePath, mapImage))
		self.SetClientSize(self._mapImage.GetSize())

	def script_addColorCommand(self, r, g, b, command, data = None):
		self._colorCommandMap[(r, g, b)] = (command, data)

	def script_addTerminal(self, r, g, b):
		x, y, w, h = self._getColoredRect(r, g, b)
		if x < 0 or self._termLog is not None:
			return
		f = wx.Font(8, wx.FONTFAMILY_MODERN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False)
		self._termLog = wx.TextCtrl(self, style=wx.TE_MULTILINE | wx.TE_DONTWRAP)
		self._termLog.SetFont(f)
		self._termLog.SetEditable(0)
		self._termInput = wx.TextCtrl(self, style=wx.TE_PROCESS_ENTER)
		self._termInput.SetFont(f)

		self._termLog.SetPosition((x, y))
		self._termLog.SetSize((w, h - self._termInput.GetSize().GetHeight()))
		self._termInput.SetPosition((x, y + h - self._termInput.GetSize().GetHeight()))
		self._termInput.SetSize((w, self._termInput.GetSize().GetHeight()))
		self.Bind(wx.EVT_TEXT_ENTER, self.OnTermEnterLine, self._termInput)
		self._termInput.Bind(wx.EVT_CHAR, self.OnTermKey)

	def script_addTemperatureGraph(self, r, g, b):
		x, y, w, h = self._getColoredRect(r, g, b)
		if x < 0 or self._tempGraph is not None:
			return
		self._tempGraph = TemperatureGraph(self)

		self._tempGraph.SetPosition((x, y))
		self._tempGraph.SetSize((w, h))

	def script_addProgressbar(self, r, g, b):
		x, y, w, h = self._getColoredRect(r, g, b)
		if x < 0:
			return
		self._progressBar = wx.Gauge(self, -1, range=1000)

		self._progressBar.SetPosition((x, y))
		self._progressBar.SetSize((w, h))

	def script_addButton(self, r, g, b, text, command, data = None):
		x, y, w, h = self._getColoredRect(r, g, b)
		if x < 0:
			return
		button = wx.Button(self, -1, _(text))
		button.SetPosition((x, y))
		button.SetSize((w, h))
		button.command = command
		button.data = data
		self._buttonList.append(button)
		self.Bind(wx.EVT_BUTTON, lambda e: command(data), button)

	def script_addSpinner(self, r, g, b, command, data):
		x, y, w, h = self._getColoredRect(r, g, b)
		if x < 0:
			return

		def run_command(spinner):
			value = spinner.GetValue()
			print "Value (%s) and (%s)" % (spinner.last_value, value)
			if spinner.last_value != '' and value != 0:
				spinner.command(spinner.data % value)
				spinner.last_value = value

		spinner = wx.SpinCtrl(self, -1, style=wx.TE_PROCESS_ENTER)
		spinner.SetRange(0, 300)
		spinner.SetPosition((x, y))
		spinner.SetSize((w, h))
		spinner.SetValue(0)
		spinner.command = command
		spinner.data = data
		spinner.last_value = ''
		self._buttonList.append(spinner)
		self.Bind(wx.EVT_SPINCTRL, lambda e: run_command(spinner), spinner)

	def script_addTextButton(self, r_text, g_text, b_text, r_button, g_button, b_button, text, command, data):
		x_text, y_text, w_text, h_text = self._getColoredRect(r_text, g_text, b_text)
		if x_text < 0:
			return
		x_button, y_button, w_button, h_button = self._getColoredRect(r_button, g_button, b_button)
		if x_button < 0:
			return
		from wx.lib.intctrl import IntCtrl
		text = IntCtrl(self, -1)
		text.SetBounds(0, 300)
		text.SetPosition((x_text, y_text))
		text.SetSize((w_text, h_text))
		
		button = wx.Button(self, -1, _(text))
		button.SetPosition((x_button, y_button))
		button.SetSize((w_button, h_button))
		button.command = command
		button.data = data
		self._buttonList.append(button)
		self.Bind(wx.EVT_BUTTON, lambda e: command(data % text.GetValue()), button)

	def _getColoredRect(self, r, g, b):
		for x in xrange(0, self._mapImage.GetWidth()):
			for y in xrange(0, self._mapImage.GetHeight()):<commit_msg>Fix for new temperature button name<commit_after>__copyright__ = "Copyright (C) 2013 <NAME> - Released under terms of the AGPLv3 License"

import wx
import power
import time
import sys
import os
import ctypes

#TODO: This does not belong here!
if sys.platform.startswith('win'):
	def preventComputerFromSleeping(prevent):
		"""
		Function used to prevent the computer from going into sleep mode.
		:param prevent: True = Prevent the system from going to sleep from this point on.
		:param prevent: False = No longer prevent the system from going to sleep.
		"""
		ES_CONTINUOUS = 0x80000000
		ES_SYSTEM_REQUIRED = 0x00000001
		#SetThreadExecutionState returns 0 when failed, which is ignored. The function should be supported from windows XP and up.
		if prevent:
			ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED)
		else:
			ctypes.windll.kernel32.SetThreadExecutionState(ES_CONTINUOUS)

else:
	def preventComputerFromSleeping(prevent):
		#No preventComputerFromSleeping for MacOS and Linux yet.
		pass

class printWindowPlugin(wx.Frame):
	def __init__(self, parent, printerConnection, filename):
		super(printWindowPlugin, self).__init__(parent, -1, style=wx.CLOSE_BOX|wx.CLIP_CHILDREN|wx.CAPTION|wx.SYSTEM_MENU|wx.FRAME_FLOAT_ON_PARENT|wx.MINIMIZE_BOX, title=_("Printing on %s") % (printerConnection.getName()))
		self._printerConnection = printerConnection
		self._basePath = os.path.dirname(filename)
		self._backgroundImage = None
		self._colorCommandMap = {}
		self._buttonList = []
		self._termLog = None
		self._termInput = None
		self._termHistory = []
		self._termHistoryIdx = 0
		self._progressBar = None
		self._tempGraph = None
		self._infoText = None
		self._lastUpdateTime = time.time()

		variables = {
			'setImage': self.script_setImage,
			'addColorCommand': self.script_addColorCommand,
			'addTerminal': self.script_addTerminal,
			'addTemperatureGraph': self.script_addTemperatureGraph,
			'addProgressbar': self.script_addProgressbar,
			'addButton': self.script_addButton,
			'addSpinner': self.script_addSpinner,
			'addTextButton': self.script_addTextButton,

			'sendGCode': self.script_sendGCode,
			'connect': self.script_connect,
			'startPrint': self.script_startPrint,
			'pausePrint': self.script_pausePrint,
			'cancelPrint': self.script_cancelPrint,
			'showErrorLog': self.script_showErrorLog,
		}
		execfile(filename, variables, variables)

		self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
		self.Bind(wx.EVT_PAINT, self.OnDraw)
		self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftClick)
		self.Bind(wx.EVT_CLOSE, self.OnClose)

		self._updateButtonStates()

		self._printerConnection.addCallback(self._doPrinterConnectionUpdate)

		if self._printerConnection.hasActiveConnection() and not self._printerConnection.isActiveConnectionOpen():
			self._printerConnection.openActiveConnection()
		preventComputerFromSleeping(True)

	def script_setImage(self, guiImage, mapImage):
		self._backgroundImage = wx.BitmapFromImage(wx.Image(os.path.join(self._basePath, guiImage)))
		self._mapImage = wx.Image(os.path.join(self._basePath, mapImage))
		self.SetClientSize(self._mapImage.GetSize())

	def script_addColorCommand(self, r, g, b, command, data = None):
		self._colorCommandMap[(r, g, b)] = (command, data)

	def script_addTerminal(self, r, g, b):
		x, y, w, h = self._getColoredRect(r, g, b)
		if x < 0 or self._termLog is not None:
			return
		f = wx.Font(8, wx.FONTFAMILY_MODERN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False)
		self._termLog = wx.TextCtrl(self, style=wx.TE_MULTILINE | wx.TE_DONTWRAP)
		self._termLog.SetFont(f)
		self._termLog.SetEditable(0)
		self._termInput = wx.TextCtrl(self, style=wx.TE_PROCESS_ENTER)
		self._termInput.SetFont(f)

		self._termLog.SetPosition((x, y))
		self._termLog.SetSize((w, h - self._termInput.GetSize().GetHeight()))
		self._termInput.SetPosition((x, y + h - self._termInput.GetSize().GetHeight()))
		self._termInput.SetSize((w, self._termInput.GetSize().GetHeight()))
		self.Bind(wx.EVT_TEXT_ENTER, self.OnTermEnterLine, self._termInput)
		self._termInput.Bind(wx.EVT_CHAR, self.OnTermKey)

	def script_addTemperatureGraph(self, r, g, b):
		x, y, w, h = self._getColoredRect(r, g, b)
		if x < 0 or self._tempGraph is not None:
			return
		self._tempGraph = TemperatureGraph(self)

		self._tempGraph.SetPosition((x, y))
		self._tempGraph.SetSize((w, h))

	def script_addProgressbar(self, r, g, b):
		x, y, w, h = self._getColoredRect(r, g, b)
		if x < 0:
			return
		self._progressBar = wx.Gauge(self, -1, range=1000)

		self._progressBar.SetPosition((x, y))
		self._progressBar.SetSize((w, h))

	def script_addButton(self, r, g, b, text, command, data = None):
		x, y, w, h = self._getColoredRect(r, g, b)
		if x < 0:
			return
		button = wx.Button(self, -1, _(text))
		button.SetPosition((x, y))
		button.SetSize((w, h))
		button.command = command
		button.data = data
		self._buttonList.append(button)
		self.Bind(wx.EVT_BUTTON, lambda e: command(data), button)

	def script_addSpinner(self, r, g, b, command, data):
		x, y, w, h = self._getColoredRect(r, g, b)
		if x < 0:
			return

		def run_command(spinner):
			value = spinner.GetValue()
			print "Value (%s) and (%s)" % (spinner.last_value, value)
			if spinner.last_value != '' and value != 0:
				spinner.command(spinner.data % value)
				spinner.last_value = value

		spinner = wx.SpinCtrl(self, -1, style=wx.TE_PROCESS_ENTER)
		spinner.SetRange(0, 300)
		spinner.SetPosition((x, y))
		spinner.SetSize((w, h))
		spinner.SetValue(0)
		spinner.command = command
		spinner.data = data
		spinner.last_value = ''
		self._buttonList.append(spinner)
		self.Bind(wx.EVT_SPINCTRL, lambda e: run_command(spinner), spinner)

	def script_addTextButton(self, r_text, g_text, b_text, r_button, g_button, b_button, button_text, command, data):
		x_text, y_text, w_text, h_text = self._getColoredRect(r_text, g_text, b_text)
		if x_text < 0:
			return
		x_button, y_button, w_button, h_button = self._getColoredRect(r_button, g_button, b_button)
		if x_button < 0:
			return
		from wx.lib.intctrl import IntCtrl
		text = IntCtrl(self, -1)
		text.SetBounds(0, 300)
		text.SetPosition((x_text, y_text))
		text.SetSize((w_text, h_text))
		
		button = wx.Button(self, -1, _(button_text))
		button.SetPosition((x_button, y_button))
		button.SetSize((w_button, h_button))
		button.command = command
		button.data = data
		self._buttonList.append(button)
		self.Bind(wx.EVT_BUTTON, lambda e: command(data % text.GetValue()), button)

	def _getColoredRect(self, r, g, b):
		for x in xrange(0, self._mapImage.GetWidth()):
			for y in xrange(0, self._mapImage.GetHeight()):
{'fraction_non_alphanumeric': 0.1228839177750907, 'fraction_numerical': 0.009068923821039904, 'mean_word_length': 4.860496014171834, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '319207', 'n_tokens_mistral': 5188, 'n_tokens_neox': 4701, 'n_words': 1010}