Ejemplo n.º 1
0
def main():
    progname = get_progname()

    # Get the command line options.

    syntax = cmdsyntax.Syntax(syntax_description)
    try:
        matches = syntax.get_args(sys.argv[1:])
        args = matches[0]
    except IndexError:
        print >>sys.stderr, "Syntax:"
        print >>sys.stderr, syntax_description
        sys.exit(1)
    else:
        if args.has_key("help"):
            print >>sys.stderr, __doc__
            print >>sys.stderr, "Syntax:"
            print >>sys.stderr, syntax_description
            sys.exit(1)

    try:
        fields = int(args["fields"])
    except ValueError:
        print >>sys.stderr, "%s: Need a number of fields/columns." % progname
        sys.exit(1)

    padding = args["padding"]

    if args.has_key("filename"):
        filename = filename_or_stream = args["filename"]
    else:
        filename_or_stream = reread(sys.stdin)
        filename = None

    reader = RawImportFileReader(filename_or_stream)
    writer = RawImportFile(rewrite(sys.stdout))

    try:
        try:
            for details in reader:

                # Pad the line to have at least as many fields as indicated.

                if len(details) < fields:
                    details += (fields - len(details)) * [padding]

                writer.append(details)

        except IOError, exc:
            print >>sys.stderr, "%s: %s" % (progname, exc)

    finally:
        reader.close()
Ejemplo n.º 2
0
def main():
    progname = get_progname()

    # Get the command line options.

    syntax = cmdsyntax.Syntax(syntax_description)
    try:
        matches = syntax.get_args(sys.argv[1:])
        args = matches[0]
    except IndexError:
        print >> sys.stderr, "Syntax:"
        print >> sys.stderr, syntax_description
        sys.exit(1)
    else:
        if args.has_key("help"):
            print >> sys.stderr, __doc__
            print >> sys.stderr, "Syntax:"
            print >> sys.stderr, syntax_description
            sys.exit(1)

    try:
        fields = int(args["fields"])
    except ValueError:
        print >> sys.stderr, "%s: Need a number of fields/columns." % progname
        sys.exit(1)

    padding = args["padding"]

    if args.has_key("filename"):
        filename = filename_or_stream = args["filename"]
    else:
        filename_or_stream = reread(sys.stdin)
        filename = None

    reader = RawImportFileReader(filename_or_stream)
    writer = RawImportFile(rewrite(sys.stdout))

    try:
        try:
            for details in reader:

                # Pad the line to have at least as many fields as indicated.

                if len(details) < fields:
                    details += (fields - len(details)) * [padding]

                writer.append(details)

        except IOError, exc:
            print >> sys.stderr, "%s: %s" % (progname, exc)

    finally:
        reader.close()
Ejemplo n.º 3
0
def main():

    # Get the command line options.

    syntax = cmdsyntax.Syntax(syntax_description)
    try:
        matches = syntax.get_args(sys.argv[1:])
        args = matches[0]
    except IndexError:
        print >> sys.stderr, "Syntax:"
        print >> sys.stderr, syntax_description
        sys.exit(1)
    else:
        if args.has_key("help"):
            print >> sys.stderr, __doc__
            print >> sys.stderr, "Syntax:"
            print >> sys.stderr, syntax_description
            sys.exit(1)

    fields = map(index_for_int, args["fields"].split(","))

    if args.has_key("filename"):
        filename = filename_or_stream = args["filename"]
    else:
        filename_or_stream = reread(sys.stdin)
        filename = None

    reader = RawImportFileReader(filename_or_stream)
    writer = RawImportFile(rewrite(sys.stdout))

    try:
        try:
            for details in reader:

                # Filter the fields.

                details = [
                    details[field] for field in fields if field is not None
                ]

                # Cut the columns according to the field details.

                writer.append(details)

        except IOError, exc:
            print >> sys.stderr, "%s: %s" % (get_progname(), exc)

    finally:
        reader.close()
Ejemplo n.º 4
0
def main():

    # Get the command line options.

    syntax = cmdsyntax.Syntax(syntax_description)
    try:
        matches = syntax.get_args(sys.argv[1:])
        args = matches[0]
    except IndexError:
        print >>sys.stderr, "Syntax:"
        print >>sys.stderr, syntax_description
        sys.exit(1)
    else:
        if args.has_key("help"):
            print >>sys.stderr, __doc__
            print >>sys.stderr, "Syntax:"
            print >>sys.stderr, syntax_description
            sys.exit(1)

    fields = map(index_for_int, args["fields"].split(","))

    if args.has_key("filename"):
        filename = filename_or_stream = args["filename"]
    else:
        filename_or_stream = reread(sys.stdin)
        filename = None

    reader = RawImportFileReader(filename_or_stream)
    writer = RawImportFile(rewrite(sys.stdout))

    try:
        try:
            for details in reader:

                # Filter the fields.

                details = [details[field] for field in fields if field is not None]

                # Cut the columns according to the field details.

                writer.append(details)

        except IOError, exc:
            print >>sys.stderr, "%s: %s" % (get_progname(), exc)

    finally:
        reader.close()
Ejemplo n.º 5
0
def main():
    progname = get_progname()

    # Get the command line options.

    syntax = cmdsyntax.Syntax(syntax_description)
    try:
        matches = syntax.get_args(sys.argv[1:])
        args = matches[0]
    except IndexError:
        print >>sys.stderr, "Syntax:"
        print >>sys.stderr, syntax_description
        sys.exit(1)
    else:
        if args.has_key("help"):
            print >>sys.stderr, __doc__
            print >>sys.stderr, "Syntax:"
            print >>sys.stderr, syntax_description
            sys.exit(1)

    start_field = index_for_int(args.get("start-field", 1))
    end_field = int_or_none(args.get("end-field", None)) # treat 1-index as 0-index plus one

    delimiter = args.get("delimiter", "\t")
    delimiter_within_fields = args.get("delimiter-within-fields", "\t")

    sequence_start = int_or_none(args.get("sequence-start"))

    if args.has_key("filename"):
        filename_or_stream = args["filename"]
    else:
        filename_or_stream = reread(sys.stdin)

    reader = RawImportFileReader(filename_or_stream, delimiter=delimiter)
    writer = RawImportFile(rewrite(sys.stdout))

    try:
        try:
            for details in reader:

                # Process the data.

                preceding, fields, following = partition(details, start_field, end_field)

                if delimiter_within_fields != delimiter:
                    split_fields = []
                    for field in fields:
                        split_fields += field.split(delimiter_within_fields)
                    fields = split_fields

                for i, field in enumerate(fields):
                    if sequence_start is not None:
                        values = [i + sequence_start, field]
                    else:
                        values = [field]
                    writer.append(preceding + values + following)

        except IOError, exc:
            print >>sys.stderr, "%s: %s" % (progname, exc)

    finally:
        writer.close()
        reader.close()
def main():
    progname = get_progname()

    # Get the command line options.

    syntax = cmdsyntax.Syntax(syntax_description)
    try:
        matches = syntax.get_args(sys.argv[1:])
        args = matches[0]
    except IndexError:
        print >>sys.stderr, "Syntax:"
        print >>sys.stderr, syntax_description
        sys.exit(1)
    else:
        if args.has_key("help"):
            print >>sys.stderr, __doc__
            print >>sys.stderr, "Syntax:"
            print >>sys.stderr, syntax_description
            sys.exit(1)

    start_field = index_for_int(args.get("start-field", 1))
    end_field = int_or_none(args.get("end-field", None)) # treat 1-index as 0-index plus one

    delimiter = args.get("delimiter", "\t")
    delimiter_within_fields = args.get("delimiter-within-fields", "\t")

    sequence_start = int_or_none(args.get("sequence-start"))

    if args.has_key("filename"):
        filename_or_stream = args["filename"]
    else:
        filename_or_stream = reread(sys.stdin)

    reader = RawImportFileReader(filename_or_stream, delimiter=delimiter)
    writer = RawImportFile(rewrite(sys.stdout))

    try:
        try:
            for details in reader:

                # Process the data.

                preceding, fields, following = partition(details, start_field, end_field)

                if delimiter_within_fields != delimiter:
                    split_fields = []
                    for field in fields:
                        split_fields += field.split(delimiter_within_fields)
                    fields = split_fields

                for i, field in enumerate(fields):
                    if sequence_start is not None:
                        values = [i + sequence_start, field]
                    else:
                        values = [field]
                    writer.append(preceding + values + following)

        except IOError, exc:
            print >>sys.stderr, "%s: %s" % (progname, exc)

    finally:
        writer.close()
        reader.close()