- refactored some of the code in the App class and created a new Tcl Command named Help

This commit is contained in:
Marius Stanciu
2020-04-22 23:00:54 +03:00
committed by Marius
parent 66d9ddd402
commit a1499158c2
10 changed files with 551 additions and 459 deletions

View File

@@ -14,6 +14,8 @@ from flatcamGUI.GUIElements import _BrowserTextEdit, _ExpandableTextEdit
import html
import sys
import tkinter as tk
import gettext
import FlatCAMTranslation as fcTranslate
import builtins
@@ -227,6 +229,12 @@ class TermWidget(QWidget):
class FCShell(TermWidget):
def __init__(self, sysShell, version, *args):
"""
:param sysShell: When instantiated the sysShell will be actually the FlatCAMApp.App() class
:param version: FlatCAM version string
:param args: Parameters passed to the TermWidget parent class
"""
TermWidget.__init__(self, version, *args)
self._sysShell = sysShell
@@ -246,4 +254,94 @@ class FCShell(TermWidget):
return True
def child_exec_command(self, text):
self._sysShell.exec_command(text)
self.exec_command(text)
def exec_command(self, text, no_echo=False):
"""
Handles input from the shell. See FlatCAMApp.setup_shell for shell commands.
Also handles execution in separated threads
:param text: FlatCAM TclCommand with parameters
:param no_echo: If True it will not try to print to the Shell because most likely the shell is hidden and it
will create crashes of the _Expandable_Edit widget
:return: output if there was any
"""
self._sysShell.report_usage('exec_command')
return self.exec_command_test(text, False, no_echo=no_echo)
def exec_command_test(self, text, reraise=True, no_echo=False):
"""
Same as exec_command(...) with additional control over exceptions.
Handles input from the shell. See FlatCAMApp.setup_shell for shell commands.
:param text: Input command
:param reraise: Re-raise TclError exceptions in Python (mostly for unittests).
:param no_echo: If True it will not try to print to the Shell because most likely the shell is hidden and it
will create crashes of the _Expandable_Edit widget
:return: Output from the command
"""
tcl_command_string = str(text)
try:
if no_echo is False:
self.open_processing() # Disables input box.
result = self._sysShell.tcl.eval(str(tcl_command_string))
if result != 'None' and no_echo is False:
self.append_output(result + '\n')
except tk.TclError as e:
# This will display more precise answer if something in TCL shell fails
result = self._sysShell.tcl.eval("set errorInfo")
self._sysShell.log.error("Exec command Exception: %s" % (result + '\n'))
if no_echo is False:
self.append_error('ERROR: ' + result + '\n')
# Show error in console and just return or in test raise exception
if reraise:
raise e
finally:
if no_echo is False:
self.close_processing()
pass
return result
# """
# Code below is unsused. Saved for later.
# """
# parts = re.findall(r'([\w\\:\.]+|".*?")+', text)
# parts = [p.replace('\n', '').replace('"', '') for p in parts]
# self.log.debug(parts)
# try:
# if parts[0] not in commands:
# self.shell.append_error("Unknown command\n")
# return
#
# #import inspect
# #inspect.getargspec(someMethod)
# if (type(commands[parts[0]]["params"]) is not list and len(parts)-1 != commands[parts[0]]["params"]) or \
# (type(commands[parts[0]]["params"]) is list and len(parts)-1 not in commands[parts[0]]["params"]):
# self.shell.append_error(
# "Command %s takes %d arguments. %d given.\n" %
# (parts[0], commands[parts[0]]["params"], len(parts)-1)
# )
# return
#
# cmdfcn = commands[parts[0]]["fcn"]
# cmdconv = commands[parts[0]]["converters"]
# if len(parts) - 1 > 0:
# retval = cmdfcn(*[cmdconv[i](parts[i + 1]) for i in range(len(parts)-1)])
# else:
# retval = cmdfcn()
# retfcn = commands[parts[0]]["retfcn"]
# if retval and retfcn(retval):
# self.shell.append_output(retfcn(retval) + "\n")
#
# except Exception as e:
# #self.shell.append_error(''.join(traceback.format_exc()))
# #self.shell.append_error("?\n")
# self.shell.append_error(str(e) + "\n")