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

more error handling

parent c01cd0ad
...@@ -50,6 +50,6 @@ class FinesseRunError(BasePyKatException) : ...@@ -50,6 +50,6 @@ class FinesseRunError(BasePyKatException) :
self.__err = err self.__err = err
self.__kat = kat self.__kat = kat
BasePyKatException.__init__(self, "Finesse error (file {1}):\n{0}".format(self.__err, self.__kat)) BasePyKatException.__init__(self, self.__err)
#BasePyKatException.__init__(self, "{0}".format(self.__err)) #BasePyKatException.__init__(self, "{0}".format(self.__err))
...@@ -554,53 +554,59 @@ class kat(object): ...@@ -554,53 +554,59 @@ class kat(object):
Before fully parsing a bunch of commands firstly any constants or variables Before fully parsing a bunch of commands firstly any constants or variables
to be recorded and replaced. to be recorded and replaced.
""" """
constants = self.constants
for line in commands: try:
values = line.split() constants = self.constants
for line in commands:
values = line.split()
if len(values)>0 and values[0] == 'const': if len(values)>0 and values[0] == 'const':
if len(values) >= 3: if len(values) >= 3:
if values[1] in constants: if values[1] in constants:
raise pkex.BasePyKatException('const command with the name "{0}" already used'.format(values[1])) raise pkex.BasePyKatException('const command with the name "{0}" already used'.format(values[1]))
else:
constants[str(values[1])] = Constant(values[1], values[2], [])
else: else:
constants[str(values[1])] = Constant(values[1], values[2], []) raise pkex.BasePyKatException('const command "{0}" was not the correct format'.format(line))
else:
raise pkex.BasePyKatException('const command "{0}" was not the correct format'.format(line))
commands_new = [] commands_new = []
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':
# 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():
# TODO: need to fix this for checking mulitple instances of const in a single line # TODO: need to fix this for checking mulitple instances of const in a single line
chars = [' ', '+', '-', '*', '/', ')'] chars = [' ', '+', '-', '*', '/', ')']
for c in chars: for c in chars:
none_found = False none_found = False
while not none_found: while not none_found:
if line.find('$'+key+c) > -1: if line.find('$'+key+c) > -1:
constants[key].usedBy.append(line) constants[key].usedBy.append(line)
line = line.replace('$'+key+c, str(constants[key].value)+ c) line = line.replace('$'+key+c, str(constants[key].value)+ c)
else: else:
none_found = True none_found = True
if line.endswith('$'+key): if line.endswith('$'+key):
constants[key].usedBy.append(line) constants[key].usedBy.append(line)
line = line.replace('$'+key, str(constants[key].value)) line = line.replace('$'+key, str(constants[key].value))
commands_new.append(line) commands_new.append(line)
self.constants = constants self.constants = constants
return commands_new return commands_new
except pkex.BasePyKatException as ex:
pkex.PrintError("Error processing constants:", ex)
sys.exit(1)
def parseCommands(self, commands, blocks=None): def parseCommands(self, commands, blocks=None):
try: try:
...@@ -1007,6 +1013,7 @@ class kat(object): ...@@ -1007,6 +1013,7 @@ class kat(object):
line = unicode(aline, "utf-8") line = unicode(aline, "utf-8")
else: else:
line = aline line = aline
if len(line) > 0: if len(line) > 0:
# remove any ANSI commands # remove any ANSI commands
#ansi = re.compile(r'\x1b[^m]*m') #ansi = re.compile(r'\x1b[^m]*m')
...@@ -1026,12 +1033,14 @@ class kat(object): ...@@ -1026,12 +1033,14 @@ class kat(object):
sys.stdout.write("\r{0} {1}".format(action, prc)) sys.stdout.write("\r{0} {1}".format(action, prc))
else: else:
sys.stdout.write("\r{0} {1}".format(str(action, 'utf-8'), str(prc, 'utf-8'))) sys.stdout.write("\r{0} {1}".format(str(action, 'utf-8'), str(prc, 'utf-8')))
elif line.lstrip().startswith(b'*'): elif line.lstrip().startswith(b'*'):
if self.verbose: if self.verbose:
if six.PY2: if six.PY2:
sys.stdout.write(line) sys.stdout.write(line)
else: else:
sys.stdout.write(str(line,'utf-8')) sys.stdout.write(str(line,'utf-8'))
elif line.rstrip().endswith(b'%'): elif line.rstrip().endswith(b'%'):
vals = line.split("-") vals = line.split("-")
action = vals[0].strip() action = vals[0].strip()
...@@ -1066,8 +1075,7 @@ class kat(object): ...@@ -1066,8 +1075,7 @@ class kat(object):
# If Finesse returned an error, just print that and exit! # If Finesse returned an error, just print that and exit!
if p.returncode != 0: if p.returncode != 0:
print (err) raise pkex.FinesseRunError(err, katfile.name)
sys.exit(1)
self.__prevrunfilename = katfile.name self.__prevrunfilename = katfile.name
...@@ -1139,9 +1147,11 @@ class kat(object): ...@@ -1139,9 +1147,11 @@ class kat(object):
return [r, perfData] return [r, perfData]
else: else:
return r return r
except pkex.FinesseRunError as fe: except pkex.FinesseRunError as ex:
print (fe) pkex.PrintError("Error from Finesse:", ex)
except pkex.BasePyKatException as ex:
pkex.PrintError("Error from pykat:", ex)
finally: finally:
if self.verbose: print ("") if self.verbose: print ("")
if self.verbose: print ("Finished in " + str(datetime.datetime.now()-start)) if self.verbose: print ("Finished in " + str(datetime.datetime.now()-start))
......
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