#!/usr/bin/python

title="Komunikat LinuxCNC"

fontsize = 15

import sys
import os
import ConfigParser
import Tkinter as tk

MessageFilePath = __file__ + "messages.ini"

proceed = "Y"

if not os.path.isfile(MessageFilePath):
	instruction = MessageFilePath + "    does not exist"
	proceed = "N"
else:

	if len(sys.argv) == 1:
		instruction = "M100 was called with no P value"
		proceed = "N"
	else:
		P=sys.argv[1]

		if len(P)<7:
			ps=P
		else:
			ps=P[0:len(P)-7]

		cfg = ConfigParser.ConfigParser()

		try:
			cfg.read(MessageFilePath)	
			instruction = cfg.get("Messages", ps)
			instruction = instruction.decode("unicode_escape") 
		except (ConfigParser.MissingSectionHeaderError), Argument:
			proceed = "N"
			instruction = "First line of\n" + MessageFilePath + "\nmust be:\n[Messages]"
			pass
		except (ConfigParser.MissingSectionHeaderError, ConfigParser.NoOptionError), Argument:
			proceed = "N"
			instruction = "There is no message for number\n" + P + "\n in the file\n" + MessageFilePath

if instruction == "":
	instruction = "The message for P" + P + " is blank"
	proceed = "N"

mess = os.linesep + instruction.replace("\n", os.linesep) + os.linesep

window = tk.Tk()

window.title(title)
window.geometry("300x200")

msg = tk.Message(window, text = mess,width=1000)
msg.config(fg="Black", font=("times", fontsize, "bold"))

def close(event):
	exit(1)
window.bind("<Escape>",close)

if proceed == "Y":

	def go_on(event):
		exit(0)
	window.bind("<Return>",go_on)
	
	def Continue_clicked():
		exit(0)
	
	btnContinue = tk.Button(window,text="Kontynuuj",command=Continue_clicked,width=60,height=1,font=("Helvetica", "20"))
	btnContinue.configure(bg = "gray")

def Stop_clicked():
	exit(1)
msg.pack()

if proceed == "Y":
	btnContinue.pack()

window.mainloop()

