clExport.py 6.6 KB
#! /bin/env python
# coding: utf8

import Tkinter as tk
import tkFileDialog as tkdia
import pdb
import os

from clcom import tt_all
from ihm import HoverInfo


def clExport():
  
	p = clExportClass(None)
	p.title('Tools')
	p.mainloop()


class clExportClass(tk.Tk):
	def __init__(self, parent):
		tk.Tk.__init__(self, parent)
		self.parent = parent
		self._init_tkoptions()
		
		self.info = {}
		self.label = {}
		self.button = {}
		self.hover = []
		
		self.initialize()
		
	def init_strinfo(self, key, value) :
	      self.info[key] = tk.StringVar()
	      self.info[key].set(value)
	
	def init_label(self, key, init_value, hovertext = 'label') :
	      if key in self.info : print "BEWARE DUPLICATED INFO"
	      self.info[key] = tk.StringVar()
	      self.info[key].set(init_value)

	      self.label[key] = tk.Label(self, textvariable = self.info[key], **self.labeloptions)
	      self.label[key].grid(column = 0, row = self.row_count, columnspan = 1, sticky = 'EW')
	      self.hover.append(HoverInfo(self.label[key], hovertext, self.status['message']))
	
	def init_button(self, key, command = None, text = '', hovertext = 'button', column = 1) :
	      self.button[key] = tk.Button(self, text = text, command = command)
	      self.button[key].grid(column = column, row = self.row_count, **self.gridoptions)
	      self.hover.append(HoverInfo(self.button[key], hovertext, self.status['message']))
	
	def init_cbutton(self, key, column = 2, text = '', hovertext = 'check button'):
	      self.info[key] = tk.StringVar()
	      self.info[key].set(0)
	      self.button[key] = tk.Checkbutton(self, text = text, variable = self.info[key], onvalue = 1, offvalue = 0, command = None)
	      self.button[key].grid(column = column, row = self.row_count, **self.gridoptions)
	      self.hover.append(HoverInfo(self.button[key], hovertext, self.status['message']))
	      
	def test(self) :
		print self.info['dataexport'].get()
		
	def initialize(self) :
	
		self.grid()
		
		#### HoverInfo init
		self.status = {}
		self.status['message'] = tk.StringVar() #message pour bar d'état (statusbar)
		self.status['message'].set("barre d'état")
		
		
		#1ere ligne (repertoire de base)
		self.row_count = 0
		self.init_label('baserep', os.curdir, hovertext = 'Base directory, required to load data')
		self.init_button('baserep', command = lambda : self.getrep('baserep'), text = '...', hovertext = 'choose working directory')
		self.init_button('clexport', text = "launch CL export", command = self.generation, column = 2, hovertext = 'will generated data from CL once CL file and TT are set' )
		
		#2eme ligne a (fichier time_table (optionnel))
		self.row_count += 1
		self.init_label('time_table', os.curdir, hovertext = 'TimeTable file, used with generation and CL')
		self.init_button('time_table', text = "...", command = lambda : self.getfile('time_table'), hovertext = 'Browse time_table file')
		#b : debug console
		self.init_button('debug', column = 2, text = 'Console pdb.set_trace', command = self.activate_console, hovertext = 'Spawn the debugging console')
		
		#3eme ligne (fichier config CL (optionnel))
		self.row_count+=1
		self.init_label('clconfig', os.curdir, hovertext = 'CL config file')
		self.init_button('clconfig', text = "...", command = lambda : self.getfile('clconfig'), hovertext = 'Browse for CL config file')
		
		#self.init_button('saveconfig', text = 'Save config', command = , hovertext = 'Save actual configuration')
		
		#4th Row: TimeComment file (optional)
		self.row_count+=1
		self.init_label('TimeComment', os.curdir, hovertext = 'input TimeComment file')
		self.init_button('TimeComment', text = '...', command = lambda : self.getfile('TimeComment'), hovertext = 'Browse for CL TimeComment file')
		
		self.init_cbutton('cefexport', text = 'export cef', column = 2, hovertext = 'if checked, will export data as cef file format')
		self.init_cbutton('asciiexport', text = 'export ascii', column = 3, hovertext = 'if checked, will export data as ascii files (Used for separated header for IDL mms tools)')
		
		
		#### HoverInfo window
		self.row_count+=1
		self.labeloptions['width'] = 0
		self.labeloptions['justify'] = 'center'
		self.label['status'] = tk.Label(self, textvariable = self.status['message'], **self.labeloptions)
		self.label['status'].grid(column = 0, row = self.row_count, columnspan = 3, **self.gridoptions)
		
		self.grid_columnconfigure(0,weight=1)
		#self.resizable(True,False)
		
	def getrep(self, key):
		"""
		Open dialog box asking for a directory.
		Resulting directory is put in self.info[key]
		"""
		options = {}
		options['initialdir'] = self.info[key].get()
		options['mustexist'] = False
		#options['parent'] = root
		options['title'] = 'Choose directory'
		dirname = tkdia.askdirectory(**options)
		if dirname :
			self.info[key].set(dirname)
			if not os.path.exists(dirname) : os.mkdir(dirname)
	
	def getfile(self, key): #Selection de fichier (Boite de dialogue)
		"""
		Open dialog box asking for a file
		resulting file is put in self.info[key]
		"""
		options = {}
		options['defaultextension'] = '.txt'
		options['filetypes'] = [('all files', '.*'), ('text files', '.txt'), ('cl files', '.cl')]
		if 'baserep' in self.info : options['initialdir'] = self.info['baserep'].get()
		options['initialfile'] = self.info[key].get()
		#options['parent'] = root
		options['title'] = 'Choose file'
		filename = tkdia.askopenfilename(**options)
		if filename :
			self.info[key].set(filename)
			
	#def saveconfig(self):
		
		#fichier = self.info['saveconfig']
		#with open(fichier) as fichier : 
			#fichier.write()

	def generation(self): #Generation des données par CL
		self.tt_all = tt_all(ttfile = self.info['time_table'].get(), config = self.info['clconfig'].get(), path = self.info['baserep'].get(), TimeCommentFile = self.info['TimeComment'].get())
		self.tt_all.gen_clout(cef = self.info['cefexport'].get(), ascii = self.info['asciiexport'].get())
		
	def onpressenter(self,event):
		print "You pressed enter !"

	def activate_console(self) :
		print 'You have activated the debugging console with pdb.set_trace'
		pdb.set_trace()

	def HelloWorld(self):
		print 'Hello World'
	

	def _init_tkoptions(self) :

		self.labeloptions = {}  #options des boites de label
		self.labeloptions["anchor"] = "e"
		self.labeloptions['fg'] = "white"
		self.labeloptions["bg"] = "grey"
		self.labeloptions["width"] = 40
		self.labeloptions["takefocus"] = True
		self.labeloptions["bd"] = 2
		self.labeloptions["relief"] = "ridge" #flat, raised, groove, ridge, solid
		self.labeloptions["state"] = "normal"
		self.labeloptions["justify"] = "right"
		
		self.gridoptions = {} #options de la grille (espacements entre les boites)
		self.gridoptions["ipadx"] = 2
		self.gridoptions["ipady"] = 2