exceptions.py 2.42 KB
Newer Older
1 2 3 4 5 6 7 8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import pykat.external.six as six
if six.PY2:
	import exceptions
9 10
import os, sys

Daniel Brown's avatar
Daniel Brown committed
11
def PrintError(message, exception=None):
12
    size = 62
13 14 15 16 17 18
    
    print("\033[91m")
    
    try:
        from textwrap import wrap, fill
        print ("-" * size)
19 20 21 22
        
        for a in wrap(message, size):
            print(a)
            
Daniel Brown's avatar
Daniel Brown committed
23 24 25 26 27
        if exception is not None:
            for a in wrap(str(exception.msg), size):
                a = a.replace("*** ", "\n")
                a = a.replace("** ", "\n")
                print(a)
28
            
29 30 31 32
        print ("-" * size)
    finally:
        print ("\033[0m")
        sys.exit(1)
Daniel Brown's avatar
Daniel Brown committed
33 34 35 36 37 38 39 40 41 42 43 44 45

def printWarning(message, exception=None):
    size = 62
    
    print("\033[93m")
    
    try:
        from textwrap import wrap, fill
        
        for a in wrap(message, size):
            print(a)

        if exception is not None:
46 47 48 49 50 51
            if hasattr(exception, "msg"):
                msg = str(exception.msg)
            else:
                msg = str(exception)
                
            for a in wrap(msg, size):
Daniel Brown's avatar
Daniel Brown committed
52 53 54 55 56 57
                a = a.replace("*** ", "\n")
                a = a.replace("** ", "\n")
                print(a)
            
    finally:
        print ("\033[0m")
58
    
Daniel Brown's avatar
Daniel Brown committed
59 60 61

class BasePyKatException(Exception):
    def __init__(self, msg):
62
        self.msg = msg
Daniel Brown's avatar
Daniel Brown committed
63 64
        
    def __str__(self):
65
        return self.msg
Daniel Brown's avatar
Daniel Brown committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

class FinesseParse(BasePyKatException) :    
    def __init__(self, msg):
        BasePyKatException.__init__(self, "Error parsing Finesse input\n{0}".format(msg))
    
class MissingFinesseEnvVar(BasePyKatException) :    
    def __init__(self):
        BasePyKatException.__init__(self, "The environment variable FINESSE_DIR was not defined")

class MissingFinesse(BasePyKatException) :    
    def __init__(self):
        BasePyKatException.__init__(self, "Could not find the finesse executable 'kat' in '{0}'," \
                                     "or you do not have the permissions to run it." \
                                      .format(os.environ.get('FINESSE_DIR')))
    
class FinesseRunError(BasePyKatException) :
    def __init__(self, err, kat):
        self.__err = err
        self.__kat = kat
        
Daniel Brown's avatar
Daniel Brown committed
86
        BasePyKatException.__init__(self, self.__err)
87
        #BasePyKatException.__init__(self, "{0}".format(self.__err))
Daniel Brown's avatar
Daniel Brown committed
88