#! /usr/bin/python """ crap.py - Comment Removing Ascii Parser Copyright (C) 2001 Thomas Andrews This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ import sys, getopt, string, os, signal #---------------------------------------------------------- infile = '' outfile = '' #---------------------------------------------------------- def help_message(): print '''\ Usage: crap [Options] [Infile] [Outfile] Options: -h or --help -- displays this help message -v or --version -- displays the version''' sys.exit(0) #---------------------------------------------------------- def cleanup(): if infile != '': infile.close() if outfile != '': outfile.close() sys.exit(0) #---------------------------------------------------------- def signal_handler(signal, frame): cleanup() #---------------------------------------------------------- # ---- Get all the command-line args ---- try: options, xarguments = getopt.getopt(sys.argv[1:], 'hv', ['help','version']) except getopt.error: print 'Error: Unknown option' help_message() sys.exit(0) # ---- Check if too many files were specified ---- if len(xarguments) > 2: print 'Error: You haven\'t supplied the correct number of arguments' help_message() # ---- Check if any of the Options flags were specified ---- for a in options[:]: if a[0] == '-h' or a[0] == '--help': help_message() for a in options[:]: if a[0] == '-v' or a[0] == '--version': print 'crap version 1.0.002' sys.exit(0) # ---- Open the specified "input" file ---- if len(xarguments) > 0: try: infile = open(xarguments[0], 'r') except IOError: print 'Can\'t open "' + xarguments[0] + '" for reading.' cleanup() # ---- Open the specified "output" file ---- if len(xarguments) > 1: try: outfile = open(xarguments[1],'w') except IOError: print 'Can\'t open "' + xarguments[1] + '" for writing.' cleanup() # ---- Parse the data, deleting lines that start with # or ; --- signal.signal(signal.SIGINT, signal_handler) while 1: if infile: data = infile.readline() else: data = sys.stdin.readline() if data: list = string.split(data) if len(list) and list[0][0] != '#' and list[0][0] != ';': if not outfile: sys.stdout.write(data) else: outfile.write(data) else: break cleanup()