Skip to content

Commit

Permalink
Added sparse files to xxd
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Zoppi <[email protected]>
  • Loading branch information
TexZK committed Mar 5, 2024
1 parent a3abce9 commit 4c24ef7
Show file tree
Hide file tree
Showing 18 changed files with 259 additions and 141 deletions.
6 changes: 6 additions & 0 deletions docs/_autosummary/hexrec.xxd.CHAR_ASCII.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CHAR\_ASCII
===========

.. currentmodule:: hexrec.xxd

.. autodata:: CHAR_ASCII
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
HUMAN\_ASCII
CHAR\_EBCDIC
============

.. currentmodule:: hexrec.xxd

.. autodata:: HUMAN_ASCII
.. autodata:: CHAR_EBCDIC
6 changes: 0 additions & 6 deletions docs/_autosummary/hexrec.xxd.HUMAN_EBCDIC.rst

This file was deleted.

6 changes: 0 additions & 6 deletions docs/_autosummary/hexrec.xxd.humanize.rst

This file was deleted.

5 changes: 2 additions & 3 deletions docs/_autosummary/hexrec.xxd.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
:template: custom-base-template.rst
:nosignatures:

~HUMAN_ASCII
~HUMAN_EBCDIC
~CHAR_ASCII
~CHAR_EBCDIC



Expand All @@ -28,7 +28,6 @@
:template: custom-base-template.rst
:nosignatures:

~humanize
~parse_seek
~xxd_core

Expand Down
32 changes: 30 additions & 2 deletions src/hexrec/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,6 @@ def flood(
""")
@click.option('-I', '--input-format', type=FORMAT_CHOICE, help="""
Forces the input file format.
Required for the standard input.
""")
@click.option('-V', '--version', is_flag=True, is_eager=True,
expose_value=False, callback=print_hexdump_version, help="""
Expand Down Expand Up @@ -808,7 +807,6 @@ def hexdump(
""")
@click.option('-I', '--input-format', type=FORMAT_CHOICE, help="""
Forces the input file format.
Required for the standard input.
""")
@click.option('-V', '--version', is_flag=True, is_eager=True,
expose_value=False, callback=print_hexdump_version, help="""
Expand Down Expand Up @@ -1207,6 +1205,16 @@ def del_header(
@click.option('-u', '--upper', 'upper', is_flag=True, help="""
Uses upper case hex letters on data only.
""")
@click.option('-I', '--input-format', type=FORMAT_CHOICE, help="""
Forces the input file format.
""")
@click.option('-O', '--output-format', type=FORMAT_CHOICE, help="""
Forces the output file format.
""")
@click.option('--seek-zeroes/--no-seek-zeroes', 'oseek_zeroes', is_flag=True,
default=True, help="""
Output seeking writes zeroes while seeking.
""")
@click.option('-v', '--version', is_flag=True, is_eager=True,
expose_value=False, callback=print_version, help="""
Prints the package version number.
Expand All @@ -1230,6 +1238,9 @@ def xxd(
iseek: int,
upper_all: bool,
upper: bool,
input_format: str,
output_format: str,
oseek_zeroes: bool,
infile: str,
outfile: str,
) -> None:
Expand All @@ -1242,6 +1253,19 @@ def xxd(

infile = None if infile == '-' else infile
outfile = None if outfile == '-' else outfile
output_path = outfile
output_file = None
input_type = None

if input_format:
input_type = guess_input_type(infile, input_format)
input_file = input_type.load(infile)
infile = input_file.memory

if output_format:
output_type = guess_output_type(outfile, input_format, input_type)
output_file = output_type()
outfile = output_file.memory

xxd_core(
infile=infile,
Expand All @@ -1262,4 +1286,8 @@ def xxd(
iseek=iseek,
upper_all=upper_all,
upper=upper,
oseek_zeroes=oseek_zeroes,
)

if output_format:
output_file.save(output_path)
4 changes: 2 additions & 2 deletions src/hexrec/hexdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,8 @@ def hexdump_core(
format_handlers = [_format_default]

do_squeezing = not no_squeezing
instream = None
outstream = None
instream: Optional[IO, SparseMemoryIO] = None
outstream: Optional[IO] = None
try:
# Input stream binding
if infile is None:
Expand Down
7 changes: 0 additions & 7 deletions src/hexrec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import sys
from typing import Any
from typing import Iterator
from typing import List
from typing import Mapping
from typing import Optional
from typing import Sequence
Expand All @@ -43,12 +42,6 @@
from .base import AnyBytes
from .base import EllipsisType

BIN8_TO_STR: List[str] = [bin(i)[2:].zfill(8) for i in range(256)]
STR_TO_BIN8: Mapping[str, int] = {s: i for i, s in enumerate(BIN8_TO_STR)}

BIN8_TO_BYTES: List[bytes] = [bin(i)[2:].zfill(8).encode() for i in range(256)]
BYTES_TO_BIN8: Mapping[bytes, int] = {s: i for i, s in enumerate(BIN8_TO_BYTES)}

SUFFIX_SCALE: Mapping[str, int] = {
'k': 2**10,
'm': 2**20,
Expand Down
Loading

0 comments on commit 4c24ef7

Please sign in to comment.