goutput.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. """
  2. @package goutput
  3. @brief Command output log widget
  4. Classes:
  5. - GMConsole
  6. - GMStc
  7. - GMStdout
  8. - GMStderr
  9. (C) 2007-2008 by the GRASS Development Team
  10. This program is free software under the GNU General Public
  11. License (>=v2). Read the file COPYING that comes with GRASS
  12. for details.
  13. @author Michael Barton (Arizona State University)
  14. @author Martin Landa <landa.martin gmail.com>
  15. """
  16. import os
  17. import sys
  18. import textwrap
  19. import time
  20. import threading
  21. import Queue
  22. import wx
  23. import wx.stc
  24. from wx.lib.newevent import NewEvent
  25. import globalvar
  26. import gcmd
  27. from debug import Debug as Debug
  28. wxCmdOutput, EVT_CMD_OUTPUT = NewEvent()
  29. wxCmdProgress, EVT_CMD_PROGRESS = NewEvent()
  30. wxCmdDone, EVT_CMD_DONE = NewEvent()
  31. wxCmdAbort, EVT_CMD_ABORT = NewEvent()
  32. def GrassCmd(cmd, stdout, stderr):
  33. """Return GRASS command thread"""
  34. return gcmd.CommandThread(cmd=cmd,
  35. stdout=stdout, stderr=stderr)
  36. class CmdThread(threading.Thread):
  37. """Thread for GRASS commands"""
  38. requestId = 0
  39. def __init__(self, parent, requestQ, resultQ, **kwds):
  40. threading.Thread.__init__(self, **kwds)
  41. self.setDaemon(True)
  42. self.parent = parent # GMConsole
  43. self.requestQ = requestQ
  44. self.resultQ = resultQ
  45. self.start()
  46. def RunCmd(self, callable, *args, **kwds):
  47. CmdThread.requestId += 1
  48. self.requestTime = time.time()
  49. self.requestCmd = None
  50. self.requestQ.put((CmdThread.requestId, callable, args, kwds))
  51. return CmdThread.requestId
  52. def run(self):
  53. while True:
  54. requestId, callable, args, kwds = self.requestQ.get()
  55. self.requestCmd = callable(*args, **kwds)
  56. self.resultQ.put((requestId, self.requestCmd.run()))
  57. event = wxCmdDone(aborted=self.requestCmd.aborted,
  58. time=self.requestTime,
  59. pid=requestId)
  60. time.sleep(.1)
  61. wx.PostEvent(self.parent, event)
  62. def abort(self):
  63. self.requestCmd.abort()
  64. class GMConsole(wx.Panel):
  65. """
  66. Create and manage output console for commands entered on the
  67. GIS Manager command line.
  68. """
  69. def __init__(self, parent, id=wx.ID_ANY, margin=False, pageid=0,
  70. pos=wx.DefaultPosition, size=wx.DefaultSize,
  71. style=wx.TAB_TRAVERSAL | wx.FULL_REPAINT_ON_RESIZE):
  72. wx.Panel.__init__(self, parent, id, pos, size, style)
  73. # initialize variables
  74. self.Map = None
  75. self.parent = parent # GMFrame
  76. self.lineWidth = 80
  77. self.pageid = pageid
  78. #
  79. # create queues
  80. #
  81. self.requestQ = Queue.Queue()
  82. self.resultQ = Queue.Queue()
  83. #
  84. # progress bar
  85. #
  86. self.console_progressbar = wx.Gauge(parent=self, id=wx.ID_ANY,
  87. range=100, pos=(110, 50), size=(-1, 25),
  88. style=wx.GA_HORIZONTAL)
  89. self.console_progressbar.Bind(EVT_CMD_PROGRESS, self.OnCmdProgress)
  90. #
  91. # text control for command output
  92. #
  93. self.cmd_output = GMStc(parent=self, id=wx.ID_ANY, margin=margin,
  94. wrap=None)
  95. self.cmd_output_timer = wx.Timer(self.cmd_output, id=wx.ID_ANY)
  96. self.cmd_output.Bind(EVT_CMD_OUTPUT, self.OnCmdOutput)
  97. self.cmd_output.Bind(wx.EVT_TIMER, self.OnProcessPendingOutputWindowEvents)
  98. self.Bind(EVT_CMD_DONE, self.OnCmdDone)
  99. #
  100. # stream redirection
  101. #
  102. self.cmd_stdout = GMStdout(self)
  103. self.cmd_stderr = GMStderr(self)
  104. #
  105. # thread
  106. #
  107. self.cmdThread = CmdThread(self, self.requestQ, self.resultQ)
  108. #
  109. # buttons
  110. #
  111. self.console_clear = wx.Button(parent=self, id=wx.ID_CLEAR)
  112. self.console_save = wx.Button(parent=self, id=wx.ID_SAVE)
  113. self.Bind(wx.EVT_BUTTON, self.ClearHistory, self.console_clear)
  114. self.Bind(wx.EVT_BUTTON, self.SaveHistory, self.console_save)
  115. self.Bind(EVT_CMD_ABORT, self.OnCmdAbort)
  116. self.__layout()
  117. def __layout(self):
  118. """Do layout"""
  119. boxsizer1 = wx.BoxSizer(wx.VERTICAL)
  120. gridsizer1 = wx.GridSizer(rows=1, cols=2, vgap=0, hgap=0)
  121. boxsizer1.Add(item=self.cmd_output, proportion=1,
  122. flag=wx.EXPAND | wx.ADJUST_MINSIZE, border=0)
  123. gridsizer1.Add(item=self.console_clear, proportion=0,
  124. flag=wx.ALIGN_CENTER_HORIZONTAL | wx.ADJUST_MINSIZE, border=0)
  125. gridsizer1.Add(item=self.console_save, proportion=0,
  126. flag=wx.ALIGN_CENTER_HORIZONTAL | wx.ADJUST_MINSIZE, border=0)
  127. boxsizer1.Add(item=gridsizer1, proportion=0,
  128. flag=wx.EXPAND | wx.ALIGN_CENTRE_VERTICAL | wx.TOP | wx.BOTTOM,
  129. border=5)
  130. boxsizer1.Add(item=self.console_progressbar, proportion=0,
  131. flag=wx.EXPAND | wx.ADJUST_MINSIZE | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=5)
  132. boxsizer1.Fit(self)
  133. boxsizer1.SetSizeHints(self)
  134. # layout
  135. self.SetAutoLayout(True)
  136. self.SetSizer(boxsizer1)
  137. def Redirect(self):
  138. """Redirect stderr
  139. @return True redirected
  140. @return False failed
  141. """
  142. if Debug.get_level() == 0:
  143. # don't redirect when debugging is enabled
  144. #sys.stdout = self.cmd_stdout
  145. #sys.stderr = self.cmd_stderr
  146. pass
  147. return True
  148. return False
  149. def WriteLog(self, line, style=None, wrap=None):
  150. """Generic method for writing log message in
  151. given style
  152. @param line text line
  153. @param style text style (see GMStc)
  154. @param stdout write to stdout or stderr
  155. """
  156. if not style:
  157. style = self.cmd_output.StyleDefault
  158. self.cmd_output.GotoPos(self.cmd_output.GetLength())
  159. p1 = self.cmd_output.GetCurrentPos()
  160. # fill space
  161. if len(line) < self.lineWidth:
  162. diff = 80 - len(line)
  163. line += diff * ' '
  164. self.cmd_output.AddTextWrapped(line, wrap=wrap) # adds os.linesep
  165. p2 = self.cmd_output.GetCurrentPos()
  166. self.cmd_output.StartStyling(p1, 0xff)
  167. self.cmd_output.SetStyling(p2 - p1, style)
  168. self.cmd_output.EnsureCaretVisible()
  169. def WriteCmdLog(self, line, pid=None):
  170. """Write out line in selected style"""
  171. if pid:
  172. line = '(' + str(pid) + ') ' + line
  173. self.WriteLog(line, self.cmd_output.StyleCommand)
  174. def RunCmd(self, command):
  175. """
  176. Run in GUI GRASS (or other) commands typed into
  177. console command text widget, and send stdout output to output
  178. text widget.
  179. Command is transformed into a list for processing.
  180. TODO: Display commands (*.d) are captured and
  181. processed separately by mapdisp.py. Display commands are
  182. rendered in map display widget that currently has
  183. the focus (as indicted by mdidx).
  184. """
  185. # map display window available ?
  186. try:
  187. curr_disp = self.parent.curr_page.maptree.mapdisplay
  188. self.Map = curr_disp.GetRender()
  189. except:
  190. curr_disp = None
  191. if not self.requestQ.empty():
  192. # only one running command enabled (per GMConsole instance)
  193. busy = wx.BusyInfo(message=_("Unable to run the command, another command is running..."),
  194. parent=self)
  195. wx.Yield()
  196. time.sleep(3)
  197. busy.Destroy()
  198. return None
  199. # command given as a string ?
  200. try:
  201. cmdlist = command.strip().split(' ')
  202. except:
  203. cmdlist = command
  204. if cmdlist[0] in globalvar.grassCmd['all']:
  205. # send GRASS command without arguments to GUI command interface
  206. # except display commands (they are handled differently)
  207. if cmdlist[0][0:2] == "d.":
  208. #
  209. # display GRASS commands
  210. #
  211. try:
  212. layertype = {'d.rast' : 'raster',
  213. 'd.rgb' : 'rgb',
  214. 'd.his' : 'his',
  215. 'd.shaded' : 'shaded',
  216. 'd.legend' : 'rastleg',
  217. 'd.rast.arrow' : 'rastarrow',
  218. 'd.rast.num' : 'rastnum',
  219. 'd.vect' : 'vector',
  220. 'd.vect.thematic': 'thememap',
  221. 'd.vect.chart' : 'themechart',
  222. 'd.grid' : 'grid',
  223. 'd.geodesic' : 'geodesic',
  224. 'd.rhumbline' : 'rhumb',
  225. 'd.labels' : 'labels'}[cmdlist[0]]
  226. except KeyError:
  227. wx.MessageBox(message=_("Command '%s' not yet implemented.") % cmdlist[0])
  228. return None
  229. # add layer into layer tree
  230. self.parent.curr_page.maptree.AddLayer(ltype=layertype,
  231. lcmd=cmdlist)
  232. else:
  233. #
  234. # other GRASS commands (r|v|g|...)
  235. #
  236. # activate computational region (set with g.region)
  237. # for all non-display commands.
  238. tmpreg = os.getenv("GRASS_REGION")
  239. os.unsetenv("GRASS_REGION")
  240. if len(cmdlist) == 1:
  241. import menuform
  242. # process GRASS command without argument
  243. menuform.GUI().ParseCommand(cmdlist, parentframe=self)
  244. else:
  245. # process GRASS command with argument
  246. cmdPID = self.cmdThread.requestId + 1
  247. self.WriteCmdLog('%s' % ' '.join(cmdlist), pid=cmdPID)
  248. self.cmdThread.RunCmd(GrassCmd,
  249. cmdlist, self.cmd_stdout, self.cmd_stderr)
  250. self.cmd_output_timer.Start(50)
  251. return None
  252. # deactivate computational region and return to display settings
  253. if tmpreg:
  254. os.environ["GRASS_REGION"] = tmpreg
  255. else:
  256. # Send any other command to the shell. Send output to
  257. # console output window
  258. if hasattr(self.parent, "curr_page"):
  259. # change notebook page only for Layer Manager
  260. if self.parent.notebook.GetSelection() != 1:
  261. self.parent.notebook.SetSelection(1)
  262. # if command is not a GRASS command, treat it like a shell command
  263. try:
  264. generalCmd = gcmd.Command(cmdlist,
  265. stdout=self.cmd_stdout,
  266. stderr=self.cmd_stderr)
  267. except gcmd.CmdError, e:
  268. print >> sys.stderr, e
  269. return None
  270. def ClearHistory(self, event):
  271. """Clear history of commands"""
  272. self.cmd_output.ClearAll()
  273. self.console_progressbar.SetValue(0)
  274. def SaveHistory(self, event):
  275. """Save history of commands"""
  276. self.history = self.cmd_output.GetSelectedText()
  277. if self.history == '':
  278. self.history = self.cmd_output.GetText()
  279. # add newline if needed
  280. if len(self.history) > 0 and self.history[-1] != os.linesep:
  281. self.history += os.linesep
  282. wildcard = "Text file (*.txt)|*.txt"
  283. dlg = wx.FileDialog(
  284. self, message=_("Save file as..."), defaultDir=os.getcwd(),
  285. defaultFile="grass_cmd_history.txt", wildcard=wildcard,
  286. style=wx.SAVE|wx.FD_OVERWRITE_PROMPT)
  287. # Show the dialog and retrieve the user response. If it is the OK response,
  288. # process the data.
  289. if dlg.ShowModal() == wx.ID_OK:
  290. path = dlg.GetPath()
  291. output = open(path, "w")
  292. output.write(self.history)
  293. output.close()
  294. dlg.Destroy()
  295. def GetCmd(self):
  296. """Get running command or None"""
  297. return self.requestQ.get()
  298. def OnCmdOutput(self, event):
  299. """Print command output"""
  300. message = event.text
  301. type = event.type
  302. # message prefix
  303. if type == 'warning':
  304. messege = 'WARNING: ' + message
  305. elif type == 'error':
  306. message = 'ERROR: ' + message
  307. p1 = self.cmd_output.GetCurrentPos()
  308. message = message.replace('\r', '')
  309. pc = -1
  310. if '\b' in message:
  311. pc = p1
  312. last_c = ''
  313. for c in message:
  314. if c == '\b':
  315. pc -= 1
  316. else:
  317. self.cmd_output.SetCurrentPos(pc)
  318. self.cmd_output.ReplaceSelection(c)
  319. pc = self.cmd_output.GetCurrentPos()
  320. if c != ' ':
  321. last_c = c
  322. if last_c not in ('0123456789'):
  323. self.cmd_output.AddText('\n')
  324. pc = -1
  325. else:
  326. if os.linesep not in message:
  327. self.cmd_output.AddTextWrapped(message, wrap=60)
  328. else:
  329. self.cmd_output.AddText(message)
  330. p2 = self.cmd_output.GetCurrentPos()
  331. self.cmd_output.StartStyling(p1, 0xff)
  332. if type == 'error':
  333. self.cmd_output.SetStyling(p2 - p1 + 1, self.cmd_output.StyleError)
  334. elif type == 'warning':
  335. self.cmd_output.SetStyling(p2 - p1 + 1, self.cmd_output.StyleWarning)
  336. elif type == 'message':
  337. self.cmd_output.SetStyling(p2 - p1 + 1, self.cmd_output.StyleMessage)
  338. else: # unknown
  339. self.cmd_output.SetStyling(p2 - p1 + 1, self.cmd_output.StyleUnknown)
  340. if pc > 0:
  341. self.cmd_output.SetCurrentPos(pc)
  342. self.cmd_output.EnsureCaretVisible()
  343. def OnCmdProgress(self, event):
  344. """Update progress message info"""
  345. self.console_progressbar.SetValue(event.value)
  346. def OnCmdAbort(self, event):
  347. """Abort running command"""
  348. self.cmdThread.abort()
  349. def OnCmdDone(self, event):
  350. """Command done (or aborted)"""
  351. if event.aborted:
  352. # Thread aborted (using our convention of None return)
  353. self.WriteLog(_('Please note that the data are left in incosistent stage '
  354. 'and can be corrupted'), self.cmd_output.StyleWarning)
  355. self.WriteCmdLog(_('Command aborted'),
  356. pid=self.cmdThread.requestId)
  357. else:
  358. try:
  359. # Process results here
  360. self.WriteCmdLog(_('Command finished (%d sec)') % (time.time() - event.time),
  361. pid=event.pid)
  362. except KeyError:
  363. # stopped deamon
  364. pass
  365. self.console_progressbar.SetValue(0) # reset progress bar on '0%'
  366. self.cmd_output_timer.Stop()
  367. # updated command dialog
  368. if hasattr(self.parent.parent, "btn_run"):
  369. dialog = self.parent.parent
  370. if hasattr(self.parent.parent, "btn_abort"):
  371. dialog.btn_abort.Enable(False)
  372. if hasattr(self.parent.parent, "btn_cancel"):
  373. dialog.btn_cancel.Enable(True)
  374. if hasattr(self.parent.parent, "btn_clipboard"):
  375. dialog.btn_clipboard.Enable(True)
  376. if hasattr(self.parent.parent, "btn_help"):
  377. dialog.btn_help.Enable(True)
  378. dialog.btn_run.Enable(True)
  379. if dialog.get_dcmd is None and \
  380. dialog.closebox.IsChecked():
  381. time.sleep(1)
  382. dialog.Close()
  383. event.Skip()
  384. def OnProcessPendingOutputWindowEvents(self, event):
  385. self.ProcessPendingEvents()
  386. class GMStdout:
  387. """GMConsole standard output
  388. Based on FrameOutErr.py
  389. Name: FrameOutErr.py
  390. Purpose: Redirecting stdout / stderr
  391. Author: Jean-Michel Fauth, Switzerland
  392. Copyright: (c) 2005-2007 Jean-Michel Fauth
  393. Licence: GPL
  394. """
  395. def __init__(self, parent):
  396. self.parent = parent # GMConsole
  397. def write(self, s):
  398. if len(s) == 0 or s == '\n':
  399. return
  400. s = s.replace('\n', os.linesep)
  401. for line in s.split(os.linesep):
  402. if len(line) == 0:
  403. continue
  404. evt = wxCmdOutput(text=line + os.linesep,
  405. type='')
  406. wx.PostEvent(self.parent.cmd_output, evt)
  407. class GMStderr:
  408. """GMConsole standard error output
  409. Based on FrameOutErr.py
  410. Name: FrameOutErr.py
  411. Purpose: Redirecting stdout / stderr
  412. Author: Jean-Michel Fauth, Switzerland
  413. Copyright: (c) 2005-2007 Jean-Michel Fauth
  414. Licence: GPL
  415. """
  416. def __init__(self, parent):
  417. self.parent = parent # GMConsole
  418. self.type = ''
  419. self.message = ''
  420. self.printMessage = False
  421. def write(self, s):
  422. s = s.replace('\n', os.linesep)
  423. # remove/replace escape sequences '\b' or '\r' from stream
  424. progressValue = -1
  425. for line in s.split(os.linesep):
  426. if len(line) == 0:
  427. continue
  428. if 'GRASS_INFO_PERCENT' in line:
  429. value = int(line.rsplit(':', 1)[1].strip())
  430. if value >= 0 and value < 100:
  431. progressValue = value
  432. else:
  433. progressValue = 0
  434. elif 'GRASS_INFO_MESSAGE' in line:
  435. self.type = 'message'
  436. self.message = line.split(':', 1)[1].strip()
  437. elif 'GRASS_INFO_WARNING' in line:
  438. self.type = 'warning'
  439. self.message = line.split(':', 1)[1].strip()
  440. elif 'GRASS_INFO_ERROR' in line:
  441. self.type = 'error'
  442. self.message = line.split(':', 1)[1].strip()
  443. elif 'GRASS_INFO_END' in line:
  444. self.printMessage = True
  445. elif self.type == '':
  446. if len(line) == 0:
  447. continue
  448. evt = wxCmdOutput(text=line,
  449. type='')
  450. wx.PostEvent(self.parent.cmd_output, evt)
  451. elif len(line) > 0:
  452. self.message += line.strip() + os.linesep
  453. if self.printMessage and len(self.message) > 0:
  454. evt = wxCmdOutput(text=self.message,
  455. type=self.type)
  456. wx.PostEvent(self.parent.cmd_output, evt)
  457. self.type = ''
  458. self.message = ''
  459. self.printMessage = False
  460. # update progress message
  461. if progressValue > -1:
  462. # self.gmgauge.SetValue(progressValue)
  463. evt = wxCmdProgress(value=progressValue)
  464. wx.PostEvent(self.parent.console_progressbar, evt)
  465. class GMStc(wx.stc.StyledTextCtrl):
  466. """Styled GMConsole
  467. Based on FrameOutErr.py
  468. Name: FrameOutErr.py
  469. Purpose: Redirecting stdout / stderr
  470. Author: Jean-Michel Fauth, Switzerland
  471. Copyright: (c) 2005-2007 Jean-Michel Fauth
  472. Licence: GPL
  473. """
  474. def __init__(self, parent, id, margin=False, wrap=None):
  475. wx.stc.StyledTextCtrl.__init__(self, parent, id)
  476. self.parent = parent
  477. self.wrap = wrap
  478. #
  479. # styles
  480. #
  481. self.StyleDefault = 0
  482. self.StyleDefaultSpec = "face:Courier New,size:10,fore:#000000,back:#FFFFFF"
  483. self.StyleCommand = 1
  484. self.StyleCommandSpec = "face:Courier New,size:10,fore:#000000,back:#bcbcbc"
  485. self.StyleOutput = 2
  486. self.StyleOutputSpec = "face:Courier New,size:10,fore:#000000,back:#FFFFFF"
  487. # fatal error
  488. self.StyleError = 3
  489. self.StyleErrorSpec = "face:Courier New,size:10,fore:#7F0000,back:#FFFFFF"
  490. # warning
  491. self.StyleWarning = 4
  492. self.StyleWarningSpec = "face:Courier New,size:10,fore:#0000FF,back:#FFFFFF"
  493. # message
  494. self.StyleMessage = 5
  495. self.StyleMessageSpec = "face:Courier New,size:10,fore:#000000,back:#FFFFFF"
  496. # unknown
  497. self.StyleUnknown = 6
  498. self.StyleUnknownSpec = "face:Courier New,size:10,fore:#7F0000,back:#FFFFFF"
  499. # default and clear => init
  500. self.StyleSetSpec(wx.stc.STC_STYLE_DEFAULT, self.StyleDefaultSpec)
  501. self.StyleClearAll()
  502. self.StyleSetSpec(self.StyleCommand, self.StyleCommandSpec)
  503. self.StyleSetSpec(self.StyleOutput, self.StyleOutputSpec)
  504. self.StyleSetSpec(self.StyleError, self.StyleErrorSpec)
  505. self.StyleSetSpec(self.StyleWarning, self.StyleWarningSpec)
  506. self.StyleSetSpec(self.StyleMessage, self.StyleMessageSpec)
  507. self.StyleSetSpec(self.StyleUnknown, self.StyleUnknownSpec)
  508. #
  509. # line margins
  510. #
  511. # TODO print number only from cmdlog
  512. self.SetMarginWidth(1, 0)
  513. self.SetMarginWidth(2, 0)
  514. if margin:
  515. self.SetMarginType(0, wx.stc.STC_MARGIN_NUMBER)
  516. self.SetMarginWidth(0, 30)
  517. else:
  518. self.SetMarginWidth(0, 0)
  519. #
  520. # miscellaneous
  521. #
  522. self.SetViewWhiteSpace(False)
  523. self.SetTabWidth(4)
  524. self.SetUseTabs(False)
  525. self.UsePopUp(True)
  526. self.SetSelBackground(True, "#FFFF00")
  527. self.SetUseHorizontalScrollBar(True)
  528. #
  529. # bindins
  530. #
  531. self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
  532. def OnDestroy(self, evt):
  533. """The clipboard contents can be preserved after
  534. the app has exited"""
  535. wx.TheClipboard.Flush()
  536. evt.Skip()
  537. def AddTextWrapped(self, txt, wrap=None):
  538. """Add string to text area.
  539. String is wrapped and linesep is also added to the end
  540. of the string"""
  541. if wrap is None and self.wrap:
  542. wrap = self.wrap
  543. if wrap is not None:
  544. txt = textwrap.fill(txt, wrap) + os.linesep
  545. else:
  546. txt += os.linesep
  547. self.AddText(txt)
  548. def SetWrap(self, wrap):
  549. """Set wrapping value
  550. @param wrap wrapping value
  551. @return current wrapping value
  552. """
  553. if wrap > 0:
  554. self.wrap = wrap
  555. return self.wrap