Matrix Science header

common_error.py

Generic error handling example.

#!/usr/bin/python
##############################################################################
# file: common_error.py                                                      #
# 'msparser' toolkit example code                                            #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1998-2010 Matrix Science Limited  All Rights Reserved.           #
#                                                                            #
##############################################################################
#     $Source: /vol/cvsroot/parser/examples/test_python/common_error.py,v $ #
#     $Author: neilr $                                                      #
#       $Date: 2016/03/22 10:13:16 $                                         #
#   $Revision: 1.4 $                                                         #
# $NoKeywords::                                                            $ #
##############################################################################

import msparser
import sys

def main() :
    file = msparser.ms_datfile()

    if len(sys.argv) > 1 :  
        file.setFileName(sys.argv[1])
    else :
        file.setFileName("wrong_name.txt")

    file.read_file()

    if file.isValid() : 
        print("The file has been read and parsed successfully. Congratulations!")
    else :
        checkErrorHandler(file)


def checkErrorHandler(obj) :
    print("Last error description                   ")
    print("=========================================")
    print("Error: %s" % obj.getLastErrorString())
    print("=========================================")
    print("Testing the error handling...            ")
    print("=========================================")

    err = obj.getErrorHandler()
    for i in range(1 + err.getNumberOfErrors()) : 
        print("Error number: %d (%d times): %s" % (
                err.getErrorNumber(i),
                err.getErrorRepeats(i) + 1,
                err.getErrorString(i)
                ))

    obj.clearAllErrors()


if __name__ == "__main__":
    sys.exit(main())


""" Running the program with no arguments, for example with

python common_error.py

will give the output:

 
Last error description                   
=========================================
Error: Cannot find Mascot configuration file 'wrong_name.txt'.
=========================================
Testing the error handling...            
=========================================
Error number: 0 (1 times): 
Error number: 1537 (1 times): Cannot find Mascot configuration file 'wrong_name.txt'.


"""

Copyright © 2016 Matrix Science Ltd.  All Rights Reserved. Generated on Fri Jun 2 2017 01:44:50