GME  13
createerrtab.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright (c) 2006-2009 ISIS, Vanderbilt Univeristy 
00004 # Author: Peter Volgyesi (peter.volgyesi@vanderbilt.edu)
00005 #
00006 
00007 """Generates cpp file for supporting IErrorInfo based on the idl file
00008    Replaces (deprecates) the original bash/sed script"""
00009 
00010 import os
00011 import re
00012 import sys
00013 import time
00014 import getpass
00015 
00016 rbegin = re.compile('MGAERRORTABLE\s+BEGIN')
00017 rhelpstring = re.compile('\s*\[\s*helpstring\s*\(\s*("[^"]*")\s*\)\s*\]')
00018 rhresult = re.compile('\s*(E_\S+)\s*=\s*0x')
00019 rend = re.compile('MGAERRORTABLE\s+END')
00020 intable = False
00021 
00022 if not os.environ.has_key('GME_ROOT'):
00023         print "Please, set the GME_ROOT environment variable correctly."
00024         sys.exit(1)
00025 
00026 root_path = os.environ['GME_ROOT']
00027 i_path = os.path.join(root_path, 'GME', 'Interfaces', 'Mga.idl');
00028 o_path = os.path.join(root_path, 'GME', 'Mga', 'MgaErr.c');
00029 
00030 print 'Generating error info from', i_path, 'to', o_path, "...", 
00031 
00032 i = open(i_path, 'r')
00033 o = open(o_path, 'w')
00034 
00035 header = '''// MgaErr.c
00036 // Error table file automatically generated from Interfaces/Mga.idl
00037 // Genarated on %s 
00038 //           by %s 
00039 //           from %s
00040 //           to %s
00041 //           using script %s
00042 
00043 const struct errtab MgaErrTab[]= {
00044 ''' % (time.ctime(), getpass.getuser(), i_path, o_path, sys.argv[0]) 
00045 
00046 o.write(header)
00047 
00048 l = i.readline()
00049 while l:
00050         if intable:
00051                 m = rhelpstring.match(l)
00052                 if m:
00053                         hs = m.group(1)
00054                   
00055                 m = rhresult.match(l)
00056                 if m:
00057                         ecode = m.group(1)
00058                         if not hs:
00059                                 print "WARNING: could not find helpstring for error", ecode
00060                                 hs = "Unkown error"
00061                         o.write('{%s, L%s},\n' % (ecode, hs))
00062                         hs = None
00063 
00064         if rbegin.search(l):
00065                 intable = True
00066 
00067         if rend.search(l):
00068                 intable = False
00069         
00070         l = i.readline()
00071         
00072 footer = '''    {0}};\n'''
00073 
00074 o.write(footer)
00075 
00076         
00077 o.close()
00078 i.close()
00079 
00080 print 'Done.'