1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2020 Pierre Ratinaud
4 #modification pour python 3 : Laurent Mérat, 6x7 - mai 2020
7 #------------------------------------
8 # import des modules python
9 #------------------------------------
11 from time import sleep
14 #------------------------------------
15 # import des modules wx
16 #------------------------------------
19 #------------------------------------
20 # import des fichiers du projet
21 #------------------------------------
22 from chemins import ffr, FFF
23 from functions import exec_rcode, check_Rresult
28 def __init__(self, parent):
29 #wx.Frame.__init__(self, parent, size=wx.Size(400,300))
30 self.tempdir = parent.TEMPDIR
31 self.fileforR = parent.tableau.parametre['csvfile']
33 self.Rpath = parent.PathPath.get('PATHS', 'rpath')
34 self.panel = wx.Panel(self.parent.nb, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
35 self.Intro = wx.StaticText(self.panel, -1, """dm <- read.csv2("%s", header=TRUE, row.names=1, na.string='')""" % (self.fileforR))
36 self.splitter = wx.SplitterWindow(self.panel, -1)
37 self.TextPage=wx.TextCtrl(self.splitter, style=wx.TE_MULTILINE | wx.TE_RICH2)
38 self.TextPage.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, "courier"))
39 self.OutPage = wx.TextCtrl(self.splitter, style=wx.TE_MULTILINE | wx.TE_RICH2 | wx.TE_READONLY)
40 self.OutPage.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, "courier"))
41 self.OutPage.SetBackgroundColour(wx.BLACK)
42 self.OutPage.SetForegroundColour(wx.WHITE)
43 self.SimpleCommand = wx.TextCtrl(self.panel, style=wx.TE_RICH2|wx.TE_PROCESS_ENTER)
44 self.SimpleCommand.SetFont(wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, "courier"))
45 self.splitter.SplitVertically(self.TextPage, self.OutPage)
46 self.button_1 = wx.Button(self.panel, -1, "Executer")
47 self.button_2 = wx.Button(self.panel, -1, "Enregistrer...")
49 self.parent.nb.AddPage(self.panel, 'R Code...')
50 self.parent.nb.SetSelection(parent.nb.GetPageCount() - 1)
51 self.parent.ShowAPane("Tab_content")
52 self.panel.Bind(wx.EVT_BUTTON, self.OnSavePage, self.button_2)
53 self.panel.Bind(wx.EVT_BUTTON, self.OnExecute, self.button_1)
54 self.panel.Bind(wx.EVT_TEXT_ENTER, self.OnEnter, self.SimpleCommand)
55 #self.parent.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
57 def __do_layout(self):
58 sizer_3 = wx.BoxSizer(wx.VERTICAL)
59 sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
60 sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
61 #sizer_1.Add(self.TextPage, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
62 #sizer_1.Add(self.OutPage, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
63 sizer_3.Add(self.Intro, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
64 sizer_1.Add(self.splitter, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
65 sizer_2.Add(self.button_1, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
66 sizer_2.Add(self.button_2, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
67 sizer_3.Add(sizer_1, 5, wx.EXPAND, 0)
68 sizer_3.Add(sizer_2, 0, wx.EXPAND, 0)
69 sizer_3.Add(self.SimpleCommand, 0, wx.EXPAND, 0)
70 #self.SetAutoLayout(True)
71 self.panel.SetSizer(sizer_3)
74 def OnSavePage(self, evt) :
76 self.parent, message="Enregistrer sous...", defaultDir=os.getcwd(),
77 defaultFile="script.R", wildcard="Rscript|*.R", style=wx.SAVE | wx.OVERWRITE_PROMPT
81 if dlg.ShowModal() == wx.ID_OK:
83 with open(path, 'w') as f :
86 def OnExecute(self, event, cmdtxt = False):
87 tmpfile = tempfile.mktemp(dir=self.tempdir)
88 tempres = tempfile.mktemp(dir=self.tempdir)
91 dm <- read.csv2("%s", header=TRUE, row.names=1, na.string='')
92 """ % (ffr(tempres), ffr(self.fileforR))
97 text = text + cmdtxt + end
99 text = text + self.TextPage.GetValue() + end
100 self.text = self.TextPage.GetValue()
101 with open(tmpfile, 'w') as tmpscript :
102 tmpscript.write(text + text)
103 pid = exec_rcode(self.Rpath, tmpfile, wait = False)
104 while pid.poll() == None :
107 check_Rresult(self.parent,pid)
109 except Exception as prob :
111 print('zerzerzerzer', prob)
112 mess = wx.MessageDialog(self.parent, prob[1], 'Erreur dans le code R',wx.OK|wx.ICON_INFORMATION)
114 with open(tempres, 'r') as tmpin :
116 self.OutPage.write(res)
117 self.OutPage.ScrollLines(-1)
119 def OnEnter(self, evt) :
120 self.OnExecute(evt, self.SimpleCommand.GetValue())
122 self.TextPage.write('\n' + self.SimpleCommand.GetValue())
123 self.SimpleCommand.Clear()
125 def OnCloseWindow(self, event):