From d7735fbf9f848a80888cceac1a1bfed141d44c46 Mon Sep 17 00:00:00 2001
From: Daniel Brown <ddb@star.sr.bham.ac.uk>
Date: Thu, 28 Nov 2013 16:41:18 +0000
Subject: [PATCH] updating examples and fixing some exception errors

---
 bin/test_aperture.py |  7 ++++---
 pykat/exceptions.py  | 12 ++++++------
 pykat/finesse.py     | 22 +++++++++++-----------
 3 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/bin/test_aperture.py b/bin/test_aperture.py
index c6bab5f..19f738d 100644
--- a/bin/test_aperture.py
+++ b/bin/test_aperture.py
@@ -15,9 +15,10 @@ pd refl n2
 xaxis m1 r_ap lin 0.1e-3 2e-3 10
 """
 
-kat = finesse.kat(kat_code = code)
+kat = finesse.kat()
+kat.parseCommands(code)
 
-maxtem = np.arange(0, 3, 2)
+maxtem = np.arange(0, 5, 2)
 
 for tem in maxtem:
     print "Calculating maxtem ", tem, "..."
@@ -30,4 +31,4 @@ pl.ylabel("Reflected Power [W]")
 pl.xlabel("Mirror aperture [mm]")
 pl.legend()
 pl.show()
-    
\ No newline at end of file
+    
diff --git a/pykat/exceptions.py b/pykat/exceptions.py
index 20c2a6e..5df2844 100644
--- a/pykat/exceptions.py
+++ b/pykat/exceptions.py
@@ -1,19 +1,19 @@
 import exceptions
 
-class BasePyKatException:
+class BasePyKatException(Exception):
     def __init__(self, msg):
         self.__msg = msg
         
     def __str__(self):
-        return "PyKat Exception message: ", self.__msg
+        return self.__msg
 
 class MissingFinesseEnvVar(BasePyKatException) :    
     def __init__(self):
-        BasePyKatExeception.__init__("The environment variable FINESSE_DIR was not defined")
+        BasePyKatException.__init__(self, "The environment variable FINESSE_DIR was not defined")
 
 class MissingFinesse(BasePyKatException) :    
     def __init__(self):
-        BasePyKatExeception.__init__("Could not find the finesse executable 'kat' in '{0}'," \
+        BasePyKatException.__init__(self, "Could not find the finesse executable 'kat' in '{0}'," \
                                      "or you do not have the permissions to run it." \
                                       .format(os.environ.get('FINESSE_DIR')))
     
@@ -22,5 +22,5 @@ class FinesseRunError(BasePyKatException) :
         self.__err = err
         self.__kat = kat
         
-        BasePyKatExeception.__init__("Finesse returned an error running {1}: {0}".format(self.__err, self.__kat))
-        
\ No newline at end of file
+        BasePyKatException.__init__(self, "Finesse returned an error running {1}: {0}".format(self.__err, self.__kat))
+        
diff --git a/pykat/finesse.py b/pykat/finesse.py
index aad4e2f..ac99281 100644
--- a/pykat/finesse.py
+++ b/pykat/finesse.py
@@ -32,7 +32,7 @@ import datetime
 import pickle
 import pykat
 
-import pykat.exceptions as pkex
+from pykat.exceptions import *
 
 from pykat.node_network import NodeNetwork
 from pykat.detectors import Detector
@@ -54,7 +54,7 @@ class katRun(object):
         
     def saveKatRun(self, run, filename):
         if not isinstance(run, katRun):
-            raise pkex.BasePyKatException("run object must be a katRun type")
+            raise BasePyKatException("run object must be a katRun type")
         
         with open(filename,'w') as outfile:
             pickle.dump(run, outfile, pickle.HIGHEST_PROTOCOL)
@@ -86,7 +86,7 @@ class kat(object):
         self.__time_code = None
         
         if kat_code != None and kat_file != None:
-            raise pkex.BasePyKatException("Specify either a Kat file or some Kat code, not both.")
+            raise BasePyKatException("Specify either a Kat file or some Kat code, not both.")
         
         if kat_code != None:
             self.parseCommands(kat_code)
@@ -165,7 +165,7 @@ class kat(object):
                 self.__finesse_dir = os.environ.get('FINESSE_DIR')
                 
                 if self.__finesse_dir == None :
-                    raise pkex.MissingFinesseEnvVar()
+                    raise MissingFinesseEnvVar()
             else:
                 self.__finesse_dir = self.__katdir
                 
@@ -181,7 +181,7 @@ class kat(object):
             
             # check if kat file exists and it is executable by user        
             if not (os.path.isfile(kat_exec) and os.access(kat_exec, os.X_OK)):
-                raise pkex.MissingFinesse()
+                raise MissingFinesse()
             
             # create a kat file which we will write the script into
             katfile = tempfile.NamedTemporaryFile(suffix=".kat")
@@ -219,7 +219,7 @@ class kat(object):
             r.runDateTime = datetime.datetime.now()
             
             if p.returncode != 0:
-                raise pkex.FinesseRunError(err, katfile.name)
+                raise FinesseRunError(err, katfile.name)
             
             if printout == 1: print out
             if printerr == 1: print err
@@ -275,7 +275,7 @@ class kat(object):
             else:
                 return r
                 
-        except FinesseError as fe:
+        except FinesseRunError as fe:
             print fe
             
         
@@ -284,7 +284,7 @@ class kat(object):
             if isinstance(obj, Component):
                 
                 if obj.name in self.__components :
-                    raise pkex.BasePyKatException("A component with name '{0}' has already been added".format([obj.name]))            
+                    raise BasePyKatException("A component with name '{0}' has already been added".format([obj.name]))            
                             
                 self.__components[obj.name] = obj
                 self.__add_component(obj)
@@ -292,7 +292,7 @@ class kat(object):
             elif isinstance(obj, Detector):
                 
                 if obj.name in self.__detectors :
-                        raise pkex.BasePyKatException("A detector '{0}' has already been added".format(obj.name))
+                        raise BasePyKatException("A detector '{0}' has already been added".format(obj.name))
                         
                 self.__detectors[obj.name] = obj
                 self.__add_detector(obj)
@@ -303,11 +303,11 @@ class kat(object):
                 self.__add_command(obj)
                 
             else :
-                raise pkex.BasePyKatException("Object {0} could not be added".format(obj))
+                raise BasePyKatException("Object {0} could not be added".format(obj))
                 
             obj._on_kat_add(self)
             
-        except pkex.BasePyKatException as ex:
+        except BasePyKatException as ex:
             print ex
 
     def readOutFile(self, filename):
-- 
GitLab