Ejemplo n.º 1
0
def urls(packets, editor, pages, subpages):
    """Paginate a t42 stream and print edit.tf URLs."""

    packets = (p for p in packets if not p.is_padding())
    subpages = (
        Subpage.from_packets(pl)
        for pl in pipeline.paginate(packets, pages=pages, subpages=subpages))

    for s in subpages:
        print(f'{editor}{s.url}')
Ejemplo n.º 2
0
def squash(packets, min_duplicates, pages, subpages, ignore_empty):
    """Reduce errors in t42 stream by using frequency analysis."""

    packets = (p for p in packets if not p.is_padding())
    for sp in pipeline.subpage_squash(pipeline.paginate(packets,
                                                        pages=pages,
                                                        subpages=subpages),
                                      min_duplicates=min_duplicates,
                                      ignore_empty=ignore_empty):
        yield from sp.packets
Ejemplo n.º 3
0
def deconvolve(chunker, mags, rows, pages, subpages, paginate, config, mode,
               force_cpu, threads, keep_empty, progress, mag_hist, row_hist,
               err_hist, rejects, tape_format):
    """Deconvolve raw VBI samples into Teletext packets."""

    if keep_empty and paginate:
        raise click.UsageError("Can't keep empty packets when paginating.")

    from teletext.vbi.line import process_lines

    if force_cpu:
        sys.stderr.write('CUDA disabled by user request.\n')

    chunks = chunker(config.line_length * np.dtype(config.dtype).itemsize,
                     config.field_lines, config.field_range)

    if progress:
        chunks = tqdm(chunks, unit='L', dynamic_ncols=True)
        if any((mag_hist, row_hist, rejects)):
            chunks.postfix = StatsList()

    packets = itermap(process_lines,
                      chunks,
                      threads,
                      mode=mode,
                      config=config,
                      force_cpu=force_cpu,
                      mags=mags,
                      rows=rows,
                      tape_format=tape_format)

    if progress and rejects:
        packets = Rejects(packets)
        chunks.postfix.append(packets)

    if keep_empty:
        packets = (p if isinstance(p, Packet) else Packet() for p in packets)
    else:
        packets = (p for p in packets if isinstance(p, Packet))

    if progress and mag_hist:
        packets = MagHistogram(packets)
        chunks.postfix.append(packets)
    if progress and row_hist:
        packets = RowHistogram(packets)
        chunks.postfix.append(packets)
    if progress and err_hist:
        packets = ErrorHistogram(packets)
        chunks.postfix.append(packets)

    if paginate:
        for p in pipeline.paginate(packets, pages=pages, subpages=subpages):
            yield from p
    else:
        yield from packets
Ejemplo n.º 4
0
def split(packets, pattern, pages, subpages):
    """Split a t42 stream in to multiple files."""

    packets = (p for p in packets if not p.is_padding())
    counts = defaultdict(int)

    for pl in pipeline.paginate(packets, pages=pages, subpages=subpages):
        subpage = Subpage.from_packets(pl)
        m = subpage.mrag.magazine
        p = subpage.header.page
        s = subpage.header.subpage
        c = counts[(m, p, s)]
        counts[(m, p, s)] += 1
        f = pathlib.Path(
            pattern.format(m=m, p=f'{p:02x}', s=f'{s:04x}', c=f'{c:04d}'))
        f.parent.mkdir(parents=True, exist_ok=True)
        with f.open('ab') as ff:
            ff.write(b''.join(p.bytes for p in pl))
Ejemplo n.º 5
0
def _list(packets, subpages):
    """List pages present in a t42 stream."""

    import textwrap

    packets = (p for p in packets if not p.is_padding())

    seen = set()
    try:
        for pl in pipeline.paginate(packets):
            s = Subpage.from_packets(pl)
            identifier = f'{s.mrag.magazine}{s.header.page:02x}'
            if subpages:
                identifier += f':{s.header.subpage:04x}'
            seen.add(identifier)
    except KeyboardInterrupt:
        print('\n')
    finally:
        print('\n'.join(textwrap.wrap(' '.join(sorted(seen)))))
Ejemplo n.º 6
0
def filter(packets, pages, subpages, paginate, n, keep_empty):
    """Demultiplex and display t42 packet streams."""

    if n:
        paginate = True

    if not keep_empty:
        packets = (p for p in packets if not p.is_padding())

    if paginate:
        for pn, pl in enumerate(pipeline.paginate(packets,
                                                  pages=pages,
                                                  subpages=subpages),
                                start=1):
            yield from pl
            if pn == n:
                return
    else:
        yield from packets