2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2011 Pierre Ratinaud
7 from chemins import ffr, FFF
8 from functions import exec_rcode, check_Rresult
11 from time import sleep
15 def __init__(self, parent):
16 #wx.Frame.__init__(self, parent, size=wx.Size(400,300))
17 self.tempdir = parent.TEMPDIR
18 self.fileforR = parent.tableau.parametre['csvfile']
20 self.Rpath = parent.PathPath.get('PATHS', 'rpath')
21 self.panel = wx.Panel(self.parent.nb, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
22 self.Intro = wx.StaticText(self.panel, -1, """dm <- read.csv2("%s", header=TRUE, row.names=1, na.string='')""" % (self.fileforR))
23 self.splitter = wx.SplitterWindow(self.panel, -1)
24 self.TextPage=wx.TextCtrl(self.splitter, style=wx.TE_MULTILINE | wx.TE_RICH2)
25 self.TextPage.SetFont(wx.Font(11, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "courier"))
26 self.OutPage = wx.TextCtrl(self.splitter, style=wx.TE_MULTILINE | wx.TE_RICH2 | wx.TE_READONLY)
27 self.OutPage.SetFont(wx.Font(11, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "courier"))
28 self.OutPage.SetBackgroundColour(wx.BLACK)
29 self.OutPage.SetForegroundColour(wx.WHITE)
30 self.SimpleCommand = wx.TextCtrl(self.panel, style=wx.TE_RICH2|wx.TE_PROCESS_ENTER)
31 self.SimpleCommand.SetFont(wx.Font(11, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "courier"))
32 self.splitter.SplitVertically(self.TextPage, self.OutPage)
33 self.button_1 = wx.Button(self.panel, -1, "Executer")
34 self.button_2 = wx.Button(self.panel, -1, u"Enregistrer...")
36 self.parent.nb.AddPage(self.panel, 'R Code...')
37 self.parent.nb.SetSelection(parent.nb.GetPageCount() - 1)
38 self.parent.ShowAPane("Tab_content")
40 self.panel.Bind(wx.EVT_BUTTON, self.OnSavePage, self.button_2)
41 self.panel.Bind(wx.EVT_BUTTON, self.OnExecute, self.button_1)
42 self.panel.Bind(wx.EVT_TEXT_ENTER, self.OnEnter, self.SimpleCommand)
43 #self.parent.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
45 def __do_layout(self):
46 sizer_3 = wx.BoxSizer(wx.VERTICAL)
47 sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
48 sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
49 #sizer_1.Add(self.TextPage, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
50 #sizer_1.Add(self.OutPage, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
51 sizer_3.Add(self.Intro, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
52 sizer_1.Add(self.splitter, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
53 sizer_2.Add(self.button_1, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
54 sizer_2.Add(self.button_2, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
55 sizer_3.Add(sizer_1, 5, wx.EXPAND, 0)
56 sizer_3.Add(sizer_2, 0, wx.EXPAND, 0)
57 sizer_3.Add(self.SimpleCommand, 0, wx.EXPAND, 0)
58 #self.SetAutoLayout(True)
59 self.panel.SetSizer(sizer_3)
62 def OnSavePage(self, evt) :
64 self.parent, message="Enregistrer sous...", defaultDir=os.getcwd(),
65 defaultFile="script.R", wildcard="Rscript|*.R", style=wx.SAVE | wx.OVERWRITE_PROMPT
69 if dlg.ShowModal() == wx.ID_OK:
71 with open(path, 'w') as f :
74 def OnExecute(self, event, cmdtxt = False):
75 tmpfile = tempfile.mktemp(dir=self.tempdir)
76 tempres = tempfile.mktemp(dir=self.tempdir)
79 dm <- read.csv2("%s", header=TRUE, row.names=1, na.string='')
80 """ % (ffr(tempres), ffr(self.fileforR))
85 text = text + cmdtxt + end
87 text = text + self.TextPage.GetValue() + end
88 self.text = self.TextPage.GetValue()
89 with open(tmpfile, 'w') as tmpscript :
90 tmpscript.write(text + text)
91 pid = exec_rcode(self.Rpath, tmpfile, wait = False)
92 while pid.poll() == None :
95 check_Rresult(self.parent,pid)
97 except Exception as prob :
99 print 'zerzerzerzer', prob
100 mess = wx.MessageDialog(self.parent, prob[1], 'Erreur dans le code R',wx.OK|wx.ICON_INFORMATION)
103 with open(tempres, 'r') as tmpin :
105 self.OutPage.write(res)
106 self.OutPage.ScrollLines(-1)
108 def OnEnter(self, evt) :
109 self.OnExecute(evt, self.SimpleCommand.GetValue())
111 self.TextPage.write('\n' + self.SimpleCommand.GetValue())
112 self.SimpleCommand.Clear()
114 def OnCloseWindow(self, event):