#!/usr/bin/python """Platform-independent script for removing or replacing quoted strings. python removeQuoted.py [-D=] first argument in input file path (default is std in) second argument is ouput file path (default is std out). any arguments of the form -D= are treated as variable definitions Currently: - uses @ as the quote character - if text between quote characters matches a variable defined on the command line it will be replaced with the value, otherwise its replaced with nothing. - demands that the quoted string is on a single line - does not have any mechanism for escaping the @ - does not replace consecutive @'s or strings that are started with multiple @ symbols (this is a concession to the @@@ replacement syntax of the CIPRES registry). """ import re import sys definitions = {} for arg in sys.argv[1:] : if arg.startswith("-D") and len(arg) > 2 : nv = arg[2:].split("=", 1) if len(nv) == 2: definitions[nv[0]] = nv[1] sys.argv.remove(arg) #set inStream if len(sys.argv) > 1: inFN = sys.argv[1] if inFN.upper() in ['-H', '--HELP', '-V', '--VERSION', '--VERBOSE']: sys.exit(__doc__) try: inStream = open(inFN, 'rU') except: sys.exit('Could not open %s' % inFN) else: inStream = sys.stdin #set outStream if len(sys.argv) > 2: outFN = sys.argv[2] if outFN == inFN: sys.exit('Input and output files cannot be the same') try: outStream = open(outFN, 'w') except: sys.exit('Could not open %s' % infN) else: outFN = None outStream = sys.stdout try: #pattern = re.compile(r'@[^@]*@') pattern = re.compile(r'(?<=[^@])@([^@]+)@') # this pattern is used to avoid replacing the @@@ strings that used to be replaced by the @ for line in inStream: value = "" m = pattern.search(line) if m and m.group(1) in definitions: value = definitions[m.group(1)] outStream.write(pattern.sub(value, line)) finally: if outFN is not None: outStream.close()