Procházet zdrojové kódy

implement raiseTclError and as example use it in drillcncjob

Kamil Sopko před 10 roky
rodič
revize
3fd9b361b8
1 změnil soubory, kde provedl 22 přidání a 7 odebrání
  1. 22 7
      FlatCAMApp.py

+ 22 - 7
FlatCAMApp.py

@@ -644,6 +644,16 @@ class App(QtCore.QObject):
         else:
         else:
             self.defaults['stats'][resource] = 1
             self.defaults['stats'][resource] = 1
 
 
+    def raiseTclError(self, text):
+        """
+        this method  pass exception from python into TCL as error, so we get stacktrace and reason
+        :param text: text of error
+        :return: raise exception
+        """
+        self.tcl.eval('error "%s"' % text)
+        raise Exception(text)
+
+
     def exec_command(self, text):
     def exec_command(self, text):
         """
         """
         Handles input from the shell. See FlatCAMApp.setup_shell for shell commands.
         Handles input from the shell. See FlatCAMApp.setup_shell for shell commands.
@@ -661,7 +671,7 @@ class App(QtCore.QObject):
         except Tkinter.TclError, e:
         except Tkinter.TclError, e:
             #this will display more precise answer if something in  TCL shell fail
             #this will display more precise answer if something in  TCL shell fail
             result = self.tcl.eval("set errorInfo")
             result = self.tcl.eval("set errorInfo")
-            self.log.error("Exec command Exception: %s" % (str(e)+ '\n' + result + '\n'))
+            self.log.error("Exec command Exception: %s" % (result + '\n'))
             self.shell.append_error('ERROR: ' + result + '\n')
             self.shell.append_error('ERROR: ' + result + '\n')
             #show error in console and just return
             #show error in console and just return
         return
         return
@@ -2446,7 +2456,9 @@ class App(QtCore.QObject):
             return 'Ok'
             return 'Ok'
 
 
 
 
-        def drillcncjob(name, *args):
+        def drillcncjob(name=None, *args):
+            #name should not be none, but we set it to default, because TCL return error without reason if argument is missing
+            #we should  check it inside shell commamnd instead
             a, kwa = h(*args)
             a, kwa = h(*args)
             types = {'tools': str,
             types = {'tools': str,
                      'outname': str,
                      'outname': str,
@@ -2457,21 +2469,24 @@ class App(QtCore.QObject):
                      'toolchange': int
                      'toolchange': int
                      }
                      }
 
 
+            if name is None:
+                self.raiseTclError('Argument name is missing.')
+
             for key in kwa:
             for key in kwa:
                 if key not in types:
                 if key not in types:
-                    return 'Unknown parameter: %s' % key
+                    self.raiseTclError('Unknown parameter: %s' % key)
                 kwa[key] = types[key](kwa[key])
                 kwa[key] = types[key](kwa[key])
 
 
             try:
             try:
                 obj = self.collection.get_by_name(str(name))
                 obj = self.collection.get_by_name(str(name))
             except:
             except:
-                return "Could not retrieve object: %s" % name
+                self.raiseTclError("Could not retrieve object: %s" % name)
 
 
             if obj is None:
             if obj is None:
-                return "Object not found: %s" % name
+                self.raiseTclError('Object not found: %s' % name)
 
 
             if not isinstance(obj, FlatCAMExcellon):
             if not isinstance(obj, FlatCAMExcellon):
-                return "ERROR: Only Excellon objects can be drilled."
+                self.raiseTclError('Only Excellon objects can be drilled: %s' % name)
 
 
             try:
             try:
 
 
@@ -2497,7 +2512,7 @@ class App(QtCore.QObject):
                 obj.app.new_object("cncjob", job_name, job_init)
                 obj.app.new_object("cncjob", job_name, job_init)
 
 
             except Exception, e:
             except Exception, e:
-                return "Operation failed: %s" % str(e)
+                self.raiseTclError("Operation failed: %s" % str(e))
 
 
             return 'Ok'
             return 'Ok'