def main(argv): global verbose parser = optparse.OptionParser(usage='Usage: %prog [options] word...') parser.add_option('-s', '--special-chars', dest='special_chars', default=':', help='Special characters to quote (default is ":")') parser.add_option('-q', '--quote', dest='quote', default='\\', help='Quote or escape character (default is "\")') parser.add_option('-t', '--run-tests', dest='tests', action='store_true', help='Run built-in tests\n') parser.add_option('-u', '--unquote-input', dest='unquote_input', action='store_true', help='Unquote command line argument') parser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Verbose test output') options, args = parser.parse_args(argv) if options.verbose: verbose = True num_errors = 0 if options.tests: sys.argv = [sys.argv[0]] + args unittest.main() else: for word in args: # NB: there are inputs x for which quote(unquote(x) != x, but # there should be no input x for which unquote(quote(x)) != x. if options.unquote_input: qq = quote.unquote(word, options.special_chars, options.quote) sys.stdout.write('unquote(%s) = %s\n' % (word, ''.join(qq))) # There is no expected output for unquote -- this is just for # manual testing, so it is okay that we do not (and cannot) # update num_errors here. else: q = quote.quote(word, options.special_chars, options.quote) qq = quote.unquote(q, options.special_chars, options.quote) sys.stdout.write('quote(%s) = %s, unquote(%s) = %s\n' % (word, q, q, ''.join(qq))) if word != ''.join(qq): num_errors += 1 if num_errors > 0: sys.stderr.write('[ FAILED ] %d test failures\n' % num_errors) return num_errors
def ParseExtraFiles(encoded_list, err): """Parse the extra-files list and return a canonicalized list of [key, arch, url] triples. The |encoded_list| should be a list of strings of the form 'key:url' or 'key:arch:url', where an omitted 'arch' is taken to mean 'portable'. All entries in |encoded_list| are checked for syntax errors before returning. Error messages are written to |err| (typically sys.stderr) so that the user has actionable feedback for fixing all errors, rather than one at a time. If there are any errors, None is returned instead of a list, since an empty list is a valid return value. """ seen_error = False canonicalized = [] for ix in range(len(encoded_list)): kv = encoded_list[ix] unquoted = quote.unquote(kv, ":") if len(unquoted) == 3: if unquoted[1] != ":": err.write("Syntax error for key:value tuple " + "for --extra-files argument: " + kv + "\n") seen_error = True else: canonicalized.append([unquoted[0], "portable", unquoted[2]]) elif len(unquoted) == 5: if unquoted[1] != ":" or unquoted[3] != ":": err.write("Syntax error for key:arch:url tuple " + "for --extra-files argument: " + kv + "\n") seen_error = True else: canonicalized.append([unquoted[0], unquoted[2], unquoted[4]]) else: err.write("Bad key:arch:url tuple for --extra-files: " + kv + "\n") if seen_error: return None return canonicalized
def VerboseUnquote(in_string, specials, *args, **kwargs): if verbose: sys.stdout.write('Invoking unquote(%s, %s, %s)\n' % (repr(in_string), repr(specials), ', '.join([repr(a) for a in args] + [repr(k) + ':' + repr(v) for k, v in kwargs]))) return quote.unquote(in_string, specials, *args, **kwargs)
def main(argv): global verbose parser = optparse.OptionParser( usage='Usage: %prog [options] word...') parser.add_option('-s', '--special-chars', dest='special_chars', default=':', help='Special characters to quote (default is ":")') parser.add_option('-q', '--quote', dest='quote', default='\\', help='Quote or escape character (default is "\")') parser.add_option('-t', '--run-tests', dest='tests', action='store_true', help='Run built-in tests\n') parser.add_option('-u', '--unquote-input', dest='unquote_input', action='store_true', help='Unquote command line argument') parser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Verbose test output') options, args = parser.parse_args(argv) if options.verbose: verbose = True num_errors = 0 if options.tests: sys.argv = [sys.argv[0]] + args unittest.main() else: for word in args: # NB: there are inputs x for which quote(unquote(x) != x, but # there should be no input x for which unquote(quote(x)) != x. if options.unquote_input: qq = quote.unquote(word, options.special_chars, options.quote) sys.stdout.write('unquote(%s) = %s\n' % (word, ''.join(qq))) # There is no expected output for unquote -- this is just for # manual testing, so it is okay that we do not (and cannot) # update num_errors here. else: q = quote.quote(word, options.special_chars, options.quote) qq = quote.unquote(q, options.special_chars, options.quote) sys.stdout.write('quote(%s) = %s, unquote(%s) = %s\n' % (word, q, q, ''.join(qq))) if word != ''.join(qq): num_errors += 1 if num_errors > 0: sys.stderr.write('[ FAILED ] %d test failures\n' % num_errors) return num_errors
def main(args): global verbose parser = argparse.ArgumentParser() parser.add_argument('-s', '--special-chars', dest='special_chars', default=':', help='Special characters to quote (default is ":")') parser.add_argument('-q', '--quote', dest='quote', default='\\', help='Quote or escape character (default is "\")') parser.add_argument('-u', '--unquote-input', dest='unquote_input', action='store_true', help='Unquote command line argument') parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help='Verbose test output') parser.add_argument('words', nargs='*') options = parser.parse_args(args) if options.verbose: verbose = True if not options.words: unittest.main() num_errors = 0 for word in options.words: # NB: there are inputs x for which quote(unquote(x) != x, but # there should be no input x for which unquote(quote(x)) != x. if options.unquote_input: qq = quote.unquote(word, options.special_chars, options.quote) sys.stdout.write('unquote(%s) = %s\n' % (word, ''.join(qq))) # There is no expected output for unquote -- this is just for # manual testing, so it is okay that we do not (and cannot) # update num_errors here. else: q = quote.quote(word, options.special_chars, options.quote) qq = quote.unquote(q, options.special_chars, options.quote) sys.stdout.write('quote(%s) = %s, unquote(%s) = %s\n' % (word, q, q, ''.join(qq))) if word != ''.join(qq): num_errors += 1 if num_errors > 0: sys.stderr.write('[ FAILED ] %d test failures\n' % num_errors) return num_errors
def VerboseUnquote(in_string, specials, *args, **kwargs): if verbose: sys.stdout.write( "Invoking unquote(%s, %s, %s)\n" % ( repr(in_string), repr(specials), ", ".join([repr(a) for a in args] + [repr(k) + ":" + repr(v) for k, v in kwargs]), ) ) return quote.unquote(in_string, specials, *args, **kwargs)
def main(argv): global verbose parser = optparse.OptionParser(usage="Usage: %prog [options] word...") parser.add_option( "-s", "--special-chars", dest="special_chars", default=":", help='Special characters to quote (default is ":")' ) parser.add_option("-q", "--quote", dest="quote", default="\\", help='Quote or escape character (default is "")') parser.add_option("-t", "--run-tests", dest="tests", action="store_true", help="Run built-in tests\n") parser.add_option( "-u", "--unquote-input", dest="unquote_input", action="store_true", help="Unquote command line argument" ) parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="Verbose test output") options, args = parser.parse_args(argv) if options.verbose: verbose = True num_errors = 0 if options.tests: sys.argv = [sys.argv[0]] + args unittest.main() else: for word in args: # NB: there are inputs x for which quote(unquote(x) != x, but # there should be no input x for which unquote(quote(x)) != x. if options.unquote_input: qq = quote.unquote(word, options.special_chars, options.quote) sys.stdout.write("unquote(%s) = %s\n" % (word, "".join(qq))) # There is no expected output for unquote -- this is just for # manual testing, so it is okay that we do not (and cannot) # update num_errors here. else: q = quote.quote(word, options.special_chars, options.quote) qq = quote.unquote(q, options.special_chars, options.quote) sys.stdout.write("quote(%s) = %s, unquote(%s) = %s\n" % (word, q, q, "".join(qq))) if word != "".join(qq): num_errors += 1 if num_errors > 0: sys.stderr.write("[ FAILED ] %d test failures\n" % num_errors) return num_errors
def ParseExtraFiles(encoded_list, err): """Parse the extra-files list and return a canonicalized list of [key, arch, url] triples. The |encoded_list| should be a list of strings of the form 'key:url' or 'key:arch:url', where an omitted 'arch' is taken to mean 'portable'. All entries in |encoded_list| are checked for syntax errors before returning. Error messages are written to |err| (typically sys.stderr) so that the user has actionable feedback for fixing all errors, rather than one at a time. If there are any errors, None is returned instead of a list, since an empty list is a valid return value. """ seen_error = False canonicalized = [] for ix in range(len(encoded_list)): kv = encoded_list[ix] unquoted = quote.unquote(kv, ':') if len(unquoted) == 3: if unquoted[1] != ':': err.write('Syntax error for key:value tuple ' + 'for --extra-files argument: ' + kv + '\n') seen_error = True else: canonicalized.append([unquoted[0], 'portable', unquoted[2]]) elif len(unquoted) == 5: if unquoted[1] != ':' or unquoted[3] != ':': err.write('Syntax error for key:arch:url tuple ' + 'for --extra-files argument: ' + kv + '\n') seen_error = True else: canonicalized.append([unquoted[0], unquoted[2], unquoted[4]]) else: err.write('Bad key:arch:url tuple for --extra-files: ' + kv + '\n') if seen_error: return None return canonicalized