# $Id: N3SyntaxCheck.py,v 1.4 2002/06/06 13:08:30 graham Exp $ # # N3 syntax checker: runs a named file through the N3 parser, and reports # any errors. # #--------+---------+---------+---------+---------+---------+---------+---------+ # # Copyright (c) 2002, G. KLYNE # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # #--------+---------+---------+---------+---------+---------+---------+---------+ # $Source: /file/cvsdev/PythonN3/N3SyntaxCheck.py,v $ # $Author: graham $ # $Revision: 1.4 $ $Date: 2002/06/06 13:08:30 $ #--------+---------+---------+---------+---------+---------+---------+---------+ # 1 2 3 4 5 6 7 8 import os import sys import string import StringIO from N3Exception import N3Exception from N3Node import N3Node from N3Model import N3Model, N3PrefixError #--------+---------+---------+---------+---------+---------+---------+---------+ # Define N3SyntaxCheck package exception conditions # # Base class for exceptions # class N3SyntaxCheckError( N3Exception ): """ Base class for syntax checking errors. Attributes: message -- explanation of the error """ def __init__(self, message): N3Exception.__init__( self, "N3SyntaxCheckError: "+message ) pass class N3CommandError( N3SyntaxCheckError ): """ Invalid command line argument supplied Attributes: message -- explanation of the error """ def __init__(self, message): N3SyntaxCheckError.__init__( self, "N3CommandError: "+message ) pass class N3ReadError( N3SyntaxCheckError ): """ Error reading data files Attributes: message -- explanation of the error """ def __init__(self, message, file): N3SyntaxCheckError.__init__( self, "N3ReadError: "+message ) self.File = file pass #--------+---------+---------+---------+---------+---------+---------+---------+ # N3SyntaxCheck - check syntax of N3 file(s) # class N3SyntaxCheck: Usage = ''' python N3SyntaxCheck.py options Options are: -r file,file,... read, check and merge files -d file,file,... read, check and display files -c file,file,... read and check files only ''' #--------------------------------------------------------------------------- # Setup methods #--------------------------------------------------------------------------- def __init__( self, argv ): """ Initialize and process command line options Parameters: argv list of command line arguments. """ self.Model = N3Model() self.Model.setPrefixTable( None ) self.Files = [] i = 1 while i < len(argv): if ( ( argv[i] == "-r" ) or ( argv[i] == "-d" ) or ( argv[i] == "-c" ) ): mode = argv[i] i += 1 if i >= len(argv): raise N3CommandError( "Input file(s) expected" ) for fnam in string.split(argv[i],","): if not os.access( fnam, os.R_OK ): raise N3CommandError( "Cannot read file "+fnam ) self.Files.append( (mode,fnam) ) i += 1 # End while return def readData( self ): """ Read data from files specified on command line. """ for mf in self.Files: mode = mf[0] fnam = mf[1] if mode == '-d': sys.stdout.write( "Displaying file "+fnam+"...\n" ) elif mode == '-r': sys.stdout.write( "Reading file "+fnam+"...\n" ) else: sys.stdout.write( "Checking file "+fnam+"...\n" ) s = file( fnam, "r" ) m = N3Model( s ) s.close() if not m.parseOK(): raise N3ReadError( "Invalid data file", fnam ) if mode == '-d': m.dump( sys.stdout ) if mode == '-r': self.Model.merge( m ) return def debug( self, msg ): sys.stdout.write( "N3SyntaxCheck: "+msg+"\n" ) return # End of N3SyntaxCheck #--------+---------+---------+---------+---------+---------+---------+---------+ # Stand-alone test code follows: # if __name__ == '__main__': # Process command line arguments sys.stdout.write( "Initializing\n" ) try: argv = sys.argv sys.stdout.write( "argv "+`argv`+", len="+`len(argv)`+"\n" ) if not argv or len(argv) <= 1: argv = [ "N3SyntaxCheck", "-d", "QueryUseCase.n3" ] # "-d", "n3play.n3", # "-c", "sbp-data.n3" ] GenMsgRegistry = N3SyntaxCheck( argv ) except N3CommandError, e: sys.stdout.write( str(e)+"\n" ) sys.stdout.write( N3SyntaxCheck.Usage ) sys.exit( 1 ) # Read model data sys.stdout.write( "Read data files\n" ) try: GenMsgRegistry.readData() except N3ReadError, e: sys.stdout.write( "Error reading file "+e.File+"\n" ) sys.stdout.write( str(e)+"\n" ) sys.exit( 2 ) except N3PrefixError, e: sys.stdout.write( str(e)+"\n" ) sys.exit( 2 ) sys.stdout.write( "Exiting\n" ) sys.exit( 0 ) #--------+---------+---------+---------+---------+---------+---------+---------+ # # $Log: N3SyntaxCheck.py,v $ # Revision 1.4 2002/06/06 13:08:30 graham # Added query example use-case in N3, # based on Andy Seaborne's schema. # # Revision 1.3 2002/05/01 15:43:48 graham # Cleanup small details # # Revision 1.2 2002/04/28 11:12:19 graham # Further improvements to N3 parser, and extend options for N3SyntaxCheck. # # Revision 1.1 2002/04/27 18:07:12 graham # Fix bugs in N3 parser, add N3 syntax checker program #