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

move removing of comments and extra whitespace

parent eca0a8ba
No related branches found
No related tags found
No related merge requests found
...@@ -76,7 +76,7 @@ class katRun(object): ...@@ -76,7 +76,7 @@ class katRun(object):
def get(self, value): return self[value] def get(self, value): return self[value]
def __getitem__(self, value): def __getitem__(self, value):
idx = [i for i in range(len(self.ylabels)) if self.ylabels[i].split(" ")[0] == str(value)] idx = [i for i in range(len(self.ylabels)) if self.ylabels[i].split()[0] == str(value)]
if len(idx) > 0 and self.y.shape == (): if len(idx) > 0 and self.y.shape == ():
# In the case we have a noxaxis and just one output... # In the case we have a noxaxis and just one output...
...@@ -123,7 +123,7 @@ class katRun2D(object): ...@@ -123,7 +123,7 @@ class katRun2D(object):
def get(self, value): return self[value] def get(self, value): return self[value]
def __getitem__(self, value): def __getitem__(self, value):
idx = [i for i in range(len(self.zlabels)) if self.zlabels[i].split(" ")[0] == str(value)] idx = [i for i in range(len(self.zlabels)) if self.zlabels[i].split()[0] == str(value)]
if len(idx) > 0: if len(idx) > 0:
return self.z[idx].squeeze() return self.z[idx].squeeze()
...@@ -340,7 +340,7 @@ class kat(object): ...@@ -340,7 +340,7 @@ class kat(object):
constants = self.constants constants = self.constants
for line in commands: for line in commands:
values = line.split(' ') values = line.split()
if len(values)>0 and values[0] == 'const': if len(values)>0 and values[0] == 'const':
...@@ -354,11 +354,10 @@ class kat(object): ...@@ -354,11 +354,10 @@ class kat(object):
commands_new = [] commands_new = []
print constants
for line in commands: for line in commands:
values = line.split(' ') values = line.split()
if values[0] != 'const': if len(values) > 0 and values[0] != 'const':
# check if we have a var/constant in this line # check if we have a var/constant in this line
if line.find('$') >= 0: if line.find('$') >= 0:
for key in constants.keys(): for key in constants.keys():
...@@ -389,7 +388,7 @@ class kat(object): ...@@ -389,7 +388,7 @@ class kat(object):
line = line.strip() line = line.strip()
# Looking for block start or end # Looking for block start or end
values = line.split(" ") values = line.split()
if values[0] == "%%%": if values[0] == "%%%":
if values[1] == "FTblock": if values[1] == "FTblock":
newTag = values[2] newTag = values[2]
...@@ -454,13 +453,13 @@ class kat(object): ...@@ -454,13 +453,13 @@ class kat(object):
elif(first == "noxaxis"): elif(first == "noxaxis"):
self.noxaxis = True self.noxaxis = True
elif(first == "phase"): elif(first == "phase"):
v = line.split(" ") v = line.split()
if len(v) != 2: if len(v) != 2:
raise pkex.BasePyKatException("phase command `{0}` is incorrect.".format(line)) raise pkex.BasePyKatException("phase command `{0}` is incorrect.".format(line))
else: else:
self.phase = int(v[1]) self.phase = int(v[1])
elif(first == "maxtem"): elif(first == "maxtem"):
v = line.split(" ") v = line.split()
if len(v) != 2: if len(v) != 2:
raise pkex.BasePyKatException("maxtem command `{0}` is incorrect.".format(line)) raise pkex.BasePyKatException("maxtem command `{0}` is incorrect.".format(line))
else: else:
...@@ -469,13 +468,13 @@ class kat(object): ...@@ -469,13 +468,13 @@ class kat(object):
else: else:
self.maxtem = int(v[1]) self.maxtem = int(v[1])
elif(first == "retrace"): elif(first == "retrace"):
v = line.split(" ") v = line.split()
if len(v) > 2: if len(v) > 2:
raise pkex.BasePyKatException("Retrace command `{0}` is incorrect.".format(line)) raise pkex.BasePyKatException("Retrace command `{0}` is incorrect.".format(line))
elif len(v) == 2: elif len(v) == 2:
self.retrace = v[1] self.retrace = v[1]
elif(first == "deriv_h"): elif(first == "deriv_h"):
v = line.split(" ") v = line.split()
if len(v) != 2: if len(v) != 2:
raise pkex.BasePyKatException("deriv_h command `{0}` is incorrect.".format(line)) raise pkex.BasePyKatException("deriv_h command `{0}` is incorrect.".format(line))
else: else:
...@@ -500,7 +499,7 @@ class kat(object): ...@@ -500,7 +499,7 @@ class kat(object):
if first == "gauss" or first == "gauss*" or first == "gauss**": if first == "gauss" or first == "gauss*" or first == "gauss**":
pykat.commands.gauss.parseFinesseText(line) pykat.commands.gauss.parseFinesseText(line)
elif (first == "scale"): elif (first == "scale"):
v = line.split(" ") v = line.split()
if len(v) == 3: if len(v) == 3:
component_name = v[2] component_name = v[2]
if component_name in self.__detectors : if component_name in self.__detectors :
...@@ -512,7 +511,7 @@ class kat(object): ...@@ -512,7 +511,7 @@ class kat(object):
else: else:
raise pkex.BasePyKatException("scale command `{0}` is incorrect.".format(text)) raise pkex.BasePyKatException("scale command `{0}` is incorrect.".format(text))
elif (first == "pdtype"): elif (first == "pdtype"):
v = line.split(" ") v = line.split()
if len(v) == 3: if len(v) == 3:
component_name = v[1] component_name = v[1]
if component_name in self.__detectors : if component_name in self.__detectors :
...@@ -709,7 +708,7 @@ class kat(object): ...@@ -709,7 +708,7 @@ class kat(object):
perffile = open(root[0] + ".perf",'r') perffile = open(root[0] + ".perf",'r')
for l in perffile.readlines(): for l in perffile.readlines():
vals = l.strip().split(' ') vals = l.strip().split()
perfData.append((vals[0], float(vals[1]), float(vals[2]), float(vals[3]))) perfData.append((vals[0], float(vals[1]), float(vals[2]), float(vals[3])))
return [r, perfData] return [r, perfData]
...@@ -991,6 +990,7 @@ class kat(object): ...@@ -991,6 +990,7 @@ class kat(object):
""" """
This takes a raw Finesse code string and removes any comments This takes a raw Finesse code string and removes any comments
It returns a list of lines however, not a multiline string. It returns a list of lines however, not a multiline string.
Also removes any extrawhite space in command lines.
""" """
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)
...@@ -1010,6 +1010,10 @@ class kat(object): ...@@ -1010,6 +1010,10 @@ class kat(object):
commands = [] commands = []
for line in string.split('\n'): for line in string.split('\n'):
line = line.replace('\r','')
if len(line) > 0:
# remove any mutliple whitespace
line = " ".join(line.split())
# add to a list all the positions of any inline comment markers # add to a list all the positions of any inline comment markers
i = [line.find('#'), line.find('\\')] i = [line.find('#'), line.find('\\')]
i = filter(lambda a: a != -1, i) i = filter(lambda a: a != -1, i)
...@@ -1017,7 +1021,10 @@ class kat(object): ...@@ -1017,7 +1021,10 @@ class kat(object):
if len(i) == 0: if len(i) == 0:
commands.append(line) commands.append(line)
else: else:
commands.append(line[0:min(i)]) line = line[0:min(i)]
if len(line):
commands.append(line)
return commands return commands
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment