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

Adding parsing exception handling

parent dfb15166
No related branches found
No related tags found
No related merge requests found
...@@ -6,14 +6,30 @@ from __future__ import unicode_literals ...@@ -6,14 +6,30 @@ from __future__ import unicode_literals
import pykat.external.six as six import pykat.external.six as six
if six.PY2: if six.PY2:
import exceptions import exceptions
import os import os, sys
def PrintError(message, exception):
size = 60
print("\033[91m")
try:
from textwrap import wrap, fill
print ("-" * size)
for a in wrap(message, size): print(a)
for a in wrap(str(exception.msg), size): print(a)
print ("-" * size)
finally:
print ("\033[0m")
sys.exit(1)
class BasePyKatException(Exception): class BasePyKatException(Exception):
def __init__(self, msg): def __init__(self, msg):
self.__msg = msg self.msg = msg
def __str__(self): def __str__(self):
return self.__msg return self.msg
class FinesseParse(BasePyKatException) : class FinesseParse(BasePyKatException) :
def __init__(self, msg): def __init__(self, msg):
......
...@@ -603,6 +603,7 @@ class kat(object): ...@@ -603,6 +603,7 @@ class kat(object):
return commands_new return commands_new
def parseCommands(self, commands, blocks=None): def parseCommands(self, commands, blocks=None):
try:
blockComment = False blockComment = False
commands=self.remove_comments(commands) commands=self.remove_comments(commands)
...@@ -613,7 +614,6 @@ class kat(object): ...@@ -613,7 +614,6 @@ class kat(object):
# objects have been set and created # objects have been set and created
for line in commands: for line in commands:
try:
if len(line.strip()) >= 2: if len(line.strip()) >= 2:
line = line.strip() line = line.strip()
...@@ -767,11 +767,6 @@ class kat(object): ...@@ -767,11 +767,6 @@ class kat(object):
print ("Removed existing object '{0}' of type {1} to add line '{2}'".format(obj.name, obj.__class__, line)) print ("Removed existing object '{0}' of type {1} to add line '{2}'".format(obj.name, obj.__class__, line))
self.add(obj) self.add(obj)
except:
print ("--------------------------------------------------------")
print ("Error parsing line: " + line)
print ("--------------------------------------------------------")
raise
# now process all the varous gauss/attr etc. commands which require # now process all the varous gauss/attr etc. commands which require
...@@ -884,6 +879,11 @@ class kat(object): ...@@ -884,6 +879,11 @@ class kat(object):
self.__currentTag = NO_BLOCK self.__currentTag = NO_BLOCK
except pkex.BasePyKatException as ex:
pkex.PrintError("Error parsing line: '%s':"% line, ex)
sys.exit(1)
def saveScript(self, filename=None): def saveScript(self, filename=None):
""" """
Saves the current kat object to a Finesse input file Saves the current kat object to a Finesse input file
...@@ -1031,23 +1031,17 @@ class kat(object): ...@@ -1031,23 +1031,17 @@ class kat(object):
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(line) # todo fix this if needed
elif line.rstrip().endswith(b'%'): elif line.rstrip().endswith(b'%'):
vals = line.split("-") vals = line.split("-")
action = vals[0].strip() action = vals[0].strip()
prc = vals[1].strip()[:] prc = vals[1].strip()[:]
if printerr == 1: if printerr == 1:
if six.PY2: sys.stdout.write("\r{0} {1}".format(action, str(prc)))
sys.stdout.write("\r{0} {1}".format(action, prc))
else:
sys.stdout.write("\r{0} {1}".format(str(action, 'utf-8'), str(prc, 'utf-8')))
else: else:
if six.PY2: err += str(line)
err="".join((err,line))
else:
err="".join((err,str(line, 'utf-8')))
[out,errpipe] = p.communicate() [out,errpipe] = p.communicate()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment