Commit 03399649 authored by Daniel Brown's avatar Daniel Brown
Browse files

adding in cav command parsing. Adding in helper method to return all objects...

adding in cav command parsing. Adding in helper method to return all objects of a particular type that are present in a particular kat object
parent 3416fca0
......@@ -25,7 +25,8 @@ from pykat.optics.gaussian_beams import beam_param
class Command(object):
__metaclass__ = abc.ABCMeta
def __init__(self, name):
def __init__(self, name, unique):
self.__unique = unique
self.tag = None
self.__removed = False
self.__name = name.strip("*")
......@@ -56,16 +57,55 @@ class Command(object):
class cavity(Command):
def __init__(self, name, c1, n1, c2, n2):
Command.__init__(self, name)
Command.__init__(self, name, False)
self.__c1 = c1
self.__c2 = c2
self.__n1 = n1
self.__n2 = n2
self.enabled = True
def getFinesseText(self):
return 'cav {0} {1} {2} {3} {4}'.format(self.name, self.__c1, self.__n1, self.__c2, self.__n2);
if self.enabled:
return 'cav {0} {1} {2} {3} {4}'.format(self.name, self.__c1.name, self.__n1.name, self.__c2.name, self.__n2.name);
else:
return None
@staticmethod
def parseFinesseText(line, kat):
v = line.split()
if len(v) != 6:
raise pkex.BasePyKatException("cav command format `{0}` is incorrect".format(line))
if v[2] not in kat.components:
raise pkex.BasePyKatException("cav command `{0}` refers to component `{1}` which does not exist".format(line, v[2]))
if v[4] not in kat.components:
raise pkex.BasePyKatException("cav command `{0}` refers to component `{1}` which does not exist".format(line, v[4]))
if v[3] not in kat.nodes.getNodes():
raise pkex.BasePyKatException("cav command `{0}` refers to node `{1}` which does not exist".format(line, v[3]))
if v[5] not in kat.nodes.getNodes():
raise pkex.BasePyKatException("cav command `{0}` refers to node `{1}` which does not exist".format(line, v[5]))
c1 = getattr(kat, v[2])
c2 = getattr(kat, v[4])
n1 = getattr(kat.nodes, v[3])
n2 = getattr(kat.nodes, v[5])
if not hasattr(c1, n1.name):
raise pkex.BasePyKatException("cav command `{0}`: node `{1}` is not attached to `{2}`".format(line, n1.name, c1.name))
if not hasattr(c2, n2.name):
raise pkex.BasePyKatException("cav command `{0}`: node `{1}` is not attached to `{2}`".format(line, n2.name, c2.name))
return pykat.commands.cavity(v[1], c1, n1, c2, n2)
class gauss(object):
@staticmethod
def parseFinesseText(text, kat):
......@@ -123,7 +163,7 @@ class tf(Command):
fQ = namedtuple('fQ', ['f', 'Q'])
def __init__(self, name, poles, zeros):
Command.__init__(self, name)
Command.__init__(self, name, False)
pass
class xaxis(Command):
......@@ -142,7 +182,7 @@ class xaxis(Command):
steps is the number of points to compute between upper and lower limits.
"""
Command.__init__(self, axis_type)
Command.__init__(self, axis_type, True)
self._axis_type = axis_type
......
......@@ -481,6 +481,25 @@ class kat(object):
if kat_file != None:
self.loadKatFile(kat_file)
def getAll(self, type):
"""
Returns a collection of all objects of the type argument that are
part of this kat object.
Example:
# returns all cav commands that are present in this kat object
cavs = kat.getAll(pykat.commands.cavity)
"""
items = []
for a in (item for item in self.__class__.__dict__):
b = getattr(self, a)
if isinstance(b, type):
items.append(b)
return tuple(items)
def __deepcopy__(self, memo):
"""
When deep copying a kat object we need to take into account
......@@ -572,6 +591,10 @@ class kat(object):
def detectors(self):
return self.__detectors.copy()
@property
def commands(self):
return self.__commands.copy()
@property
def noxaxis(self): return self.__noxaxis
@noxaxis.setter
......@@ -755,6 +778,8 @@ class kat(object):
after_process.append(line)
elif(first == "pdtype"):
after_process.append(line)
elif(first == "cav"):
after_process.append(line)
elif(first == "attr"):
after_process.append(line)
elif(first == "noxaxis"):
......@@ -826,9 +851,15 @@ class kat(object):
# now process all the varous gauss/attr etc. commands which require
# components to exist first before they can be processed
for line in after_process:
first = line.split(" ",1)[0]
first = line.split(" ",1)[0]
if first == "gauss" or first == "gauss*" or first == "gauss**":
pykat.commands.gauss.parseFinesseText(line, self)
elif (first == "cav"):
self.add(pykat.commands.cavity.parseFinesseText(line, self))
elif (first == "scale"):
v = line.split()
accepted = ["psd","psd_hf","asd","asd_hf","meter", "ampere", "degs"]
......@@ -1293,8 +1324,12 @@ class kat(object):
del self.__components[obj.name]
self.__del_component(obj)
self.nodes._removeComponent(obj)
elif isinstance(obj, Command):
del self.__commands[obj.name]
elif isinstance(obj, Command):
if obj._Command__unique:
del self.__commands[obj.__class__.__name__]
else:
del self.__commands[obj.name]
self.__del_command(obj)
elif isinstance(obj, Detector):
del self.__detectors[obj.name]
......@@ -1391,7 +1426,11 @@ class kat(object):
elif isinstance(obj, Command):
self.__commands[obj.__class__.__name__] = obj
if obj._Command__unique:
self.__commands[obj.__class__.__name__] = obj
else:
self.__commands[obj.name] = obj
self.__add_command(obj)
else:
......@@ -1891,7 +1930,11 @@ class kat(object):
if not isinstance(com, Command):
raise pkex.BasePyKatException("Argument is not of type Command")
name = com.__class__.__name__
if com._Command__unique:
name = com.__class__.__name__
else:
name = com.name
fget = lambda self: self.__get_command(name)
setattr(self.__class__, name, property(fget))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment