示例#1
0
def main():
    parser = argparse.ArgumentParser(
        description='Remove all noise, idealize the data')
    parser.add_argument('file',
                        nargs='?',
                        default='/dev/stdin',
                        help='TZX file, stdin if omitted')
    parser.add_argument('-o',
                        '--to',
                        dest='to',
                        metavar='TARGET',
                        default='/dev/stdout',
                        help='target TZX file, stdout if omitted')
    parser.add_argument('-c',
                        '--stripcrc',
                        dest='stripcrc',
                        action='store_true',
                        help='also remove blocks with bad CRC')
    args = parser.parse_args()

    fout = TzxFile()
    crcCnt = 0
    noiseCnt = 0

    file = TzxFile()
    file.read(args.file)
    for b in file.blocks:
        # Convert Turbo blocks to standard timed blocks if possible
        if b.id in [0x11, 0x14]:
            o = b.asData()

        # Use all data blocks for the output
        if b.id in [0x10, 0x11, 0x14]:
            if not b.valid():
                crcCnt += 1
            if b.valid() or not args.stripcrc:
                fout.blocks.append(b)
            continue

        # Use all pause blocks if they mean "stop the tape"
        if b.id in [0x20, 0x2A]:
            if b.id != 0x20 or b.length() == 0:
                fout.blocks.append(b)
            continue

        # Use all meta blocks
        if b.id not in [0x12, 0x13, 0x15, 0x18, 0x19]:
            fout.blocks.append(b)
            continue

        noiseCnt += 1

    fout.write(args.to)

    print('Blocks found:           %3d' % (len(file.blocks)), file=sys.stderr)
    print('Noise blocks removed:   %3d' % (noiseCnt), file=sys.stderr)
    print('Blocks with CRC errors: %3d' % (crcCnt), file=sys.stderr)
    print('Blocks written:         %3d' % (len(fout.blocks)), file=sys.stderr)
示例#2
0
def main():
    parser = argparse.ArgumentParser(description='Merges TZX files')
    parser.add_argument('files', nargs='+', help='TZX files to merge')
    parser.add_argument('-o',
                        '--to',
                        metavar='TARGET',
                        default='/dev/stdout',
                        help='target TZX file, stdout if omitted')
    args = parser.parse_args()

    file = TzxFile()

    for f in args.files:
        mergeFile = TzxFile()
        mergeFile.read(f)
        file.blocks.extend(mergeFile.blocks)

    file.write(args.to)
示例#3
0
def main():
    parser = argparse.ArgumentParser(
        description='Split into separate programs')
    parser.add_argument('blocks',
                        nargs='*',
                        help='block numbers and ranges to keep')
    parser.add_argument('-i',
                        '--from',
                        dest='file',
                        metavar='SOURCE',
                        default='/dev/stdin',
                        help='source TZX file, stdin if omitted')
    parser.add_argument('-o',
                        '--to',
                        dest='to',
                        metavar='TARGET',
                        default='/dev/stdout',
                        help='target TZX file, stdout if omitted')
    parser.add_argument('-v',
                        '--invert',
                        dest='invert',
                        action='store_true',
                        help='do not keep, but remove the blocks')
    args = parser.parse_args()

    file = TzxFile()
    file.read(args.file)
    lastBlock = len(file.blocks)

    ranges = []
    for rng in args.blocks:
        m = re.match(r'^(-?\d+)$', rng)
        if m:
            v1 = int(m.group(1))
            appendRange(ranges, v1, v1, lastBlock)
            continue
        m = re.match(r'^(-?\d+):(-?\d+)$', rng)
        if m:
            v1 = int(m.group(1))
            v2 = int(m.group(2))
            appendRange(ranges, v1, v2, lastBlock)
            continue
        m = re.match(r'^(-?\d+):$', rng)
        if m:
            v1 = int(m.group(1))
            appendRange(ranges, v1, lastBlock, lastBlock)
            continue
        m = re.match(r'^:(-?\d+)$', rng)
        if m:
            v2 = int(m.group(1))
            appendRange(ranges, 0, v2, lastBlock)
            continue
        print('Illegal range: %s' % (rng), file=sys.stderr)
        exit(1)

    fout = TzxFile()

    count = 0
    for b in file.blocks:
        if isInRange(ranges, count) != args.invert:
            fout.blocks.append(b)
        count += 1

    fout.write(args.to)
示例#4
0
def main():
    parser = argparse.ArgumentParser(
        description='Remove all noise, idealize the data')
    parser.add_argument(
        'file',
        nargs='?',
        type=argparse.FileType('rb'),
        default=(None if sys.stdin.isatty() else sys.stdin.buffer),
        help='TZX file, stdin if omitted')
    parser.add_argument('-o',
                        '--to',
                        dest='to',
                        metavar='TARGET',
                        type=argparse.FileType('wb'),
                        default=sys.stdout.buffer,
                        help='target TZX file, stdout if omitted')
    parser.add_argument('-c',
                        '--stripcrc',
                        dest='stripcrc',
                        action='store_true',
                        help='also remove blocks with bad CRC')
    parser.add_argument(
        '-H',
        '--headermustmatch',
        dest='headermustmatch',
        action='store_true',
        help=
        'Remove blocks not proceeded by matching header (keep only matching header-block pairs)'
    )
    args = parser.parse_args()

    if args.file is None:
        parser.print_help(sys.stderr)
        sys.exit(1)

    fout = TzxFile()
    crcCnt = 0
    noiseCnt = 0
    headerlessCnt = 0

    file = TzxFile()
    file.read(args.file)
    numbytes = 0
    blocklengthfromheader = 0
    for b in file.blocks:
        # Convert Turbo blocks to standard timed blocks if possible
        if b.id == 0x11:  # turbo data
            b = b.asData()

        # when args.headermustmatch keep last header found
        if args.headermustmatch and b.id == 0x10 and b.valid() and isinstance(
                b.tap, TapHeader):
            if blocklengthfromheader != 0:
                # header after header makes the first one a orphan header
                print("Orphan header: {} ({})".format(
                    lastheader.tap.name().strip(), blocklengthfromheader),
                      file=sys.stderr)
                headerlessCnt = headerlessCnt + 1
            lastheader = b
            blocklengthfromheader = lastheader.tap.length()
            continue

        # Use all data blocks for the output
        if b.id in [0x10, 0x11, 0x14]:
            if not b.valid():
                crcCnt += 1
            if b.valid() or not args.stripcrc:
                if args.headermustmatch:
                    if blocklengthfromheader == len(
                            b.tap.data) - 2 and not isinstance(
                                b.tap, TapHeader):
                        # this is a datablock with matching header
                        fout.blocks.append(lastheader)  # write header only now
                        fout.blocks.append(b)
                        numbytes += len(b.tap.data)
                        blocklengthfromheader = 0
                else:
                    # as before.
                    fout.blocks.append(b)
                    numbytes += len(b.tap.data)
            if blocklengthfromheader != 0:
                print("Orphan header: {} ({})".format(
                    lastheader.tap.name().strip(), blocklengthfromheader),
                      file=sys.stderr)
                headerlessCnt = headerlessCnt + 1
                blocklengthfromheader = 0
            continue

        blocklengthfromheader = 0

        # Use all pause blocks if they mean "stop the tape"
        if b.id in [0x20, 0x2A]:
            if b.id != 0x20 or b.length() == 0:
                fout.blocks.append(b)
                numbytes += len(b.tap.data)
            continue

        # Use all meta blocks
        if b.id not in [0x12, 0x13, 0x15, 0x18, 0x19]:
            fout.blocks.append(b)
            numbytes += len(b.tap.data)
            continue

        noiseCnt += 1

    fout.write(args.to)

    print('Blocks found:              %3d' % (len(file.blocks)),
          file=sys.stderr)
    print('Noise blocks removed:      %3d' % (noiseCnt), file=sys.stderr)
    print('Blocks with CRC errors:    %3d' % (crcCnt), file=sys.stderr)
    if args.headermustmatch:
        print('Skipped headerless blocks: %3d' % (headerlessCnt),
              file=sys.stderr)
    print('Blocks written:            %3d' % (len(fout.blocks)),
          file=sys.stderr)
    print('Total bytes written:       %3d' % (numbytes),
          file=sys.stderr)  # DJS