Skip to content
Snippets Groups Projects
Commit eca0a8ba authored by Daniel Brown's avatar Daniel Brown
Browse files

changing remove comments to get rid of inline comments too

parent 2aeae8e3
No related branches found
No related tags found
No related merge requests found
...@@ -8,7 +8,7 @@ import numpy as np ...@@ -8,7 +8,7 @@ import numpy as np
import pylab as pl import pylab as pl
code = """ code = """
l l1 1 0 0 n1 l l1 1 0 0 n1 ### test
s s1 10 1 n1 n2 s s1 10 1 n1 n2
m m1 0.5 0.5 0 n2 n3 m m1 0.5 0.5 0 n2 n3
s s2 10 1 n3 n4 s s2 10 1 n3 n4
...@@ -29,7 +29,7 @@ kat.add(photodiode('pd_trs','n5')) ...@@ -29,7 +29,7 @@ kat.add(photodiode('pd_trs','n5'))
kat.add(photodiode('pd_cav','n4', num_demods=1, demods=[1])) kat.add(photodiode('pd_cav','n4', num_demods=1, demods=[1]))
kat.add(xaxis("lin", [0, 360], kat.m2, kat.m2.phi, 100)) kat.add(xaxis("lin", [0, 360], kat.m2.phi, 100))
kat.m1.Rcx = -1000.0 kat.m1.Rcx = -1000.0
kat.m1.Rcy = -1000.0 kat.m1.Rcy = -1000.0
......
...@@ -17,6 +17,8 @@ from pykat.gui.graphics import * ...@@ -17,6 +17,8 @@ from pykat.gui.graphics import *
from pykat.SIfloat import * from pykat.SIfloat import *
from pykat.param import Param, AttrParam from pykat.param import Param, AttrParam
import pykat.exceptions as pkex
next_component_id = 1 next_component_id = 1
class NodeGaussSetter(object): class NodeGaussSetter(object):
...@@ -235,10 +237,10 @@ class mirror(AbstractMirrorComponent): ...@@ -235,10 +237,10 @@ class mirror(AbstractMirrorComponent):
values = text.split(" ") values = text.split(" ")
if values[0] != "m" and values[0] != "m1" and values[0] != "m2": if values[0] != "m" and values[0] != "m1" and values[0] != "m2":
raise exceptions.RuntimeError("'{0}' not a valid Finesse mirror command".format(text)) raise pkex.BasePyKatException("'{0}' not a valid Finesse mirror command".format(text))
if len(values) != 7: if len(values) != 7:
raise exceptions.RuntimeError("Mirror Finesse code format incorrect '{0}'".format(text)) raise pkex.BasePyKatException("Mirror Finesse code format incorrect '{0}'".format(text))
if len(values[0])==1: if len(values[0])==1:
values.pop(0) # remove initial value values.pop(0) # remove initial value
...@@ -290,10 +292,10 @@ class beamSplitter(AbstractMirrorComponent): ...@@ -290,10 +292,10 @@ class beamSplitter(AbstractMirrorComponent):
values = text.split(" ") values = text.split(" ")
if values[0] != "bs" and values[0] != "bs1" and values[0] != "bs2": if values[0] != "bs" and values[0] != "bs1" and values[0] != "bs2":
raise exceptions.RuntimeError("'{0}' not a valid Finesse beam splitter command".format(text)) raise pkex.BasePyKatException("'{0}' not a valid Finesse beam splitter command".format(text))
if len(values) != 10: if len(values) != 10:
raise exceptions.RuntimeError("Beam splitter Finesse code format incorrect '{0}'".format(text)) raise pkex.BasePyKatException("Beam splitter Finesse code format incorrect '{0}'".format(text))
if len(values[0])==2: if len(values[0])==2:
values.pop(0) # remove initial value values.pop(0) # remove initial value
...@@ -356,7 +358,7 @@ class space(Component): ...@@ -356,7 +358,7 @@ class space(Component):
values = text.split(" ") values = text.split(" ")
if values[0] != "s": if values[0] != "s":
raise exceptions.RuntimeError("'{0}' not a valid Finesse space command".format(text)) raise pkex.BasePyKatException("'{0}' not a valid Finesse space command".format(text))
values.pop(0) # remove initial value values.pop(0) # remove initial value
...@@ -365,7 +367,7 @@ class space(Component): ...@@ -365,7 +367,7 @@ class space(Component):
elif len(values) == 4: elif len(values) == 4:
return space(values[0], values[2], values[3], values[1]) return space(values[0], values[2], values[3], values[1])
else: else:
raise exceptions.RuntimeError("Space Finesse code format incorrect '{0}'".format(text)) raise pkex.BasePyKatException("Space Finesse code format incorrect '{0}'".format(text))
def getFinesseText(self): def getFinesseText(self):
rtn = [] rtn = []
...@@ -397,16 +399,16 @@ class grating(Component): ...@@ -397,16 +399,16 @@ class grating(Component):
if node3 != None: if node3 != None:
self._requested_node_names.append(node3) self._requested_node_names.append(node3)
else: else:
raise exceptions.RuntimeError("Grating node 3 not specified") raise pkex.BasePyKatException("Grating node 3 not specified")
if n > 3: if n > 3:
if node4 != None: if node4 != None:
self._requested_node_names.append(node4) self._requested_node_names.append(node4)
else: else:
raise exceptions.RuntimeError("Grating node 4 not specified") raise pkex.BasePyKatException("Grating node 4 not specified")
if n > 4 or n < 2: if n > 4 or n < 2:
raise exceptions.RuntimeError("Grating must have between 2 and 4 ports") raise pkex.BasePyKatException("Grating must have between 2 and 4 ports")
self.__n = n self.__n = n
self.__d = Param("d", self, SIfloat(d)) self.__d = Param("d", self, SIfloat(d))
...@@ -422,7 +424,7 @@ class grating(Component): ...@@ -422,7 +424,7 @@ class grating(Component):
@n.setter @n.setter
def n(self, value): def n(self, value):
if value < 2 or value > 4: if value < 2 or value > 4:
raise exceptions.RuntimeError("Grating must have between 2 and 4 ports") raise pkex.BasePyKatException("Grating must have between 2 and 4 ports")
else: else:
self.__n = value self.__n = value
...@@ -466,11 +468,11 @@ class grating(Component): ...@@ -466,11 +468,11 @@ class grating(Component):
values = text.split(" ") values = text.split(" ")
if values[0][0 : 2] != "gr": if values[0][0 : 2] != "gr":
raise exceptions.RuntimeError("'{0}' not a valid Finesse grating command".format(text)) raise pkex.BasePyKatException("'{0}' not a valid Finesse grating command".format(text))
if len(values[0]) > 2: if len(values[0]) > 2:
if int(values[0][2]) > 4 or int(values[0][2]) < 2: if int(values[0][2]) > 4 or int(values[0][2]) < 2:
raise exceptions.RuntimeError("Grating must have between 2 and 4 ports") raise pkex.BasePyKatException("Grating must have between 2 and 4 ports")
else: else:
n = int(values[0][2]) n = int(values[0][2])
else: else:
...@@ -480,17 +482,17 @@ class grating(Component): ...@@ -480,17 +482,17 @@ class grating(Component):
if n == 2: if n == 2:
if len(values) != 4: if len(values) != 4:
raise exceptions.RuntimeError("Two port grating must have 2 nodes defined") raise pkex.BasePyKatException("Two port grating must have 2 nodes defined")
return grating(values[0], values[2], values[3], None, None, n, values[1]) return grating(values[0], values[2], values[3], None, None, n, values[1])
elif n == 3: elif n == 3:
if len(values) != 5: if len(values) != 5:
raise exceptions.RuntimeError("Three port grating must have 3 nodes defined") raise pkex.BasePyKatException("Three port grating must have 3 nodes defined")
return grating(values[0], values[2], values[3], values[4], None, n, values[1]) return grating(values[0], values[2], values[3], values[4], None, n, values[1])
else: else:
if len(values) != 6: if len(values) != 6:
raise exceptions.RuntimeError("Four port grating must have 4 nodes defined") raise pkex.BasePyKatException("Four port grating must have 4 nodes defined")
return grating(values[0], values[2], values[3], values[4], values[5], n, values[1]) return grating(values[0], values[2], values[3], values[4], values[5], n, values[1])
...@@ -534,14 +536,14 @@ class isolator(Component): ...@@ -534,14 +536,14 @@ class isolator(Component):
values = text.split(" ") values = text.split(" ")
if values[0] != "isol": if values[0] != "isol":
raise exceptions.RuntimeError("'{0}' not a valid Finesse isolator command".format(text)) raise pkex.BasePyKatException("'{0}' not a valid Finesse isolator command".format(text))
values.pop(0) # remove initial value values.pop(0) # remove initial value
if len(values) == 4: if len(values) == 4:
return isolator(values[0], values[2], values[3], values[1]) return isolator(values[0], values[2], values[3], values[1])
else: else:
raise exceptions.RuntimeError("Isolator Finesse code format incorrect '{0}'".format(text)) raise pkex.BasePyKatException("Isolator Finesse code format incorrect '{0}'".format(text))
def getFinesseText(self): def getFinesseText(self):
rtn = ['isol {0} {1} {2} {3}'.format(self.name, self.S.value, self.nodes[0].name, self.nodes[1].name)] rtn = ['isol {0} {1} {2} {3}'.format(self.name, self.S.value, self.nodes[0].name, self.nodes[1].name)]
...@@ -576,14 +578,14 @@ class lens(Component): ...@@ -576,14 +578,14 @@ class lens(Component):
values = text.split(" ") values = text.split(" ")
if values[0] != "lens": if values[0] != "lens":
raise exceptions.RuntimeError("'{0}' not a valid Finesse lens command".format(text)) raise pkex.BasePyKatException("'{0}' not a valid Finesse lens command".format(text))
values.pop(0) # remove initial value values.pop(0) # remove initial value
if len(values) == 4: if len(values) == 4:
return lens(values[0], values[2], values[3], values[1]) return lens(values[0], values[2], values[3], values[1])
else: else:
raise exceptions.RuntimeError("Lens Finesse code format incorrect '{0}'".format(text)) raise pkex.BasePyKatException("Lens Finesse code format incorrect '{0}'".format(text))
def getFinesseText(self): def getFinesseText(self):
rtn = ['lens {0} {1} {2} {3}'.format(self.name, self.f.value, self.nodes[0].name, self.nodes[1].name)] rtn = ['lens {0} {1} {2} {3}'.format(self.name, self.f.value, self.nodes[0].name, self.nodes[1].name)]
...@@ -651,7 +653,7 @@ class modulator(Component): ...@@ -651,7 +653,7 @@ class modulator(Component):
v = text.split(" ") v = text.split(" ")
if v[0] != "mod": if v[0] != "mod":
raise exceptions.RuntimeError("'{0}' not a valid Finesse modulator command".format(text)) raise pkex.BasePyKatException("'{0}' not a valid Finesse modulator command".format(text))
v.pop(0) # remove initial value v.pop(0) # remove initial value
...@@ -707,7 +709,7 @@ class laser(Component): ...@@ -707,7 +709,7 @@ class laser(Component):
values = text.split(" ") values = text.split(" ")
if values[0] != "l": if values[0] != "l":
raise exceptions.RuntimeError("'{0}' not a valid Finesse laser command".format(text)) raise pkex.BasePyKatException("'{0}' not a valid Finesse laser command".format(text))
values.pop(0) # remove initial value values.pop(0) # remove initial value
......
...@@ -377,8 +377,6 @@ class kat(object): ...@@ -377,8 +377,6 @@ class kat(object):
blockComment = False blockComment = False
commands=self.remove_comments(commands) commands=self.remove_comments(commands)
# convert block of strings to list of lines
commands = commands.split('\n')
commands=self.processConstants(commands) commands=self.processConstants(commands)
...@@ -990,6 +988,10 @@ class kat(object): ...@@ -990,6 +988,10 @@ class kat(object):
return getattr(self, '__comp_' + name) return getattr(self, '__comp_' + name)
def remove_comments(self, string): def remove_comments(self, string):
"""
This takes a raw Finesse code string and removes any comments
It returns a list of lines however, not a multiline string.
"""
pattern = r"(\".*?\"|\'.*?\'|%{3}[^\r\n]*$)|(/\*.*?\*/|%[^\r\n]*$|#[^\r\n]*$|//[^\r\n]*$)" pattern = r"(\".*?\"|\'.*?\'|%{3}[^\r\n]*$)|(/\*.*?\*/|%[^\r\n]*$|#[^\r\n]*$|//[^\r\n]*$)"
# first group captures quoted strings (double or single) # first group captures quoted strings (double or single)
# second group captures comments (//single-line or /* multi-line */) # second group captures comments (//single-line or /* multi-line */)
...@@ -1001,7 +1003,23 @@ class kat(object): ...@@ -1001,7 +1003,23 @@ class kat(object):
return "" # so we will return empty to remove the comment return "" # so we will return empty to remove the comment
else: # otherwise, we will return the 1st group else: # otherwise, we will return the 1st group
return match.group(1) # captured quoted-string return match.group(1) # captured quoted-string
return regex.sub(_replacer, string)
# remove any inline comments
string = regex.sub(_replacer, string)
commands = []
for line in string.split('\n'):
# add to a list all the positions of any inline comment markers
i = [line.find('#'), line.find('\\')]
i = filter(lambda a: a != -1, i)
if len(i) == 0:
commands.append(line)
else:
commands.append(line[0:min(i)])
return commands
# printing pykat logo on first input # printing pykat logo on first input
kat.logo() kat.logo()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment