From db43051adae231ccee186bf8b1b49f894421b48f Mon Sep 17 00:00:00 2001 From: Andrea Zoppi Date: Sat, 2 Mar 2024 15:34:44 +0100 Subject: [PATCH] Renamed `hexdump` and `xxd` core functions as `*_core` Signed-off-by: Andrea Zoppi --- docs/_autosummary/hexrec.hexdump.hexdump.rst | 6 -- .../hexrec.hexdump.hexdump_core.rst | 6 ++ docs/_autosummary/hexrec.hexdump.rst | 2 +- docs/_autosummary/hexrec.xxd.HUMAN_ASCII.rst | 6 ++ docs/_autosummary/hexrec.xxd.HUMAN_EBCDIC.rst | 6 ++ docs/_autosummary/hexrec.xxd.humanize.rst | 6 ++ docs/_autosummary/hexrec.xxd.parse_seek.rst | 6 ++ docs/_autosummary/hexrec.xxd.rst | 21 ++++++ docs/_autosummary/hexrec.xxd.xxd_core.rst | 6 ++ src/hexrec/__init__.py | 2 +- src/hexrec/cli.py | 4 +- src/hexrec/hexdump.py | 2 +- src/hexrec/xxd.py | 2 +- tests/test_hexdump.py | 26 +++---- tests/test_xxd.py | 68 ++++++++++--------- 15 files changed, 114 insertions(+), 55 deletions(-) delete mode 100644 docs/_autosummary/hexrec.hexdump.hexdump.rst create mode 100644 docs/_autosummary/hexrec.hexdump.hexdump_core.rst create mode 100644 docs/_autosummary/hexrec.xxd.HUMAN_ASCII.rst create mode 100644 docs/_autosummary/hexrec.xxd.HUMAN_EBCDIC.rst create mode 100644 docs/_autosummary/hexrec.xxd.humanize.rst create mode 100644 docs/_autosummary/hexrec.xxd.parse_seek.rst create mode 100644 docs/_autosummary/hexrec.xxd.xxd_core.rst diff --git a/docs/_autosummary/hexrec.hexdump.hexdump.rst b/docs/_autosummary/hexrec.hexdump.hexdump.rst deleted file mode 100644 index 34107b3..0000000 --- a/docs/_autosummary/hexrec.hexdump.hexdump.rst +++ /dev/null @@ -1,6 +0,0 @@ -hexdump -======= - -.. currentmodule:: hexrec.hexdump - -.. autofunction:: hexdump \ No newline at end of file diff --git a/docs/_autosummary/hexrec.hexdump.hexdump_core.rst b/docs/_autosummary/hexrec.hexdump.hexdump_core.rst new file mode 100644 index 0000000..af383d8 --- /dev/null +++ b/docs/_autosummary/hexrec.hexdump.hexdump_core.rst @@ -0,0 +1,6 @@ +hexdump\_core +============= + +.. currentmodule:: hexrec.hexdump + +.. autofunction:: hexdump_core \ No newline at end of file diff --git a/docs/_autosummary/hexrec.hexdump.rst b/docs/_autosummary/hexrec.hexdump.rst index 5568a50..587958b 100644 --- a/docs/_autosummary/hexrec.hexdump.rst +++ b/docs/_autosummary/hexrec.hexdump.rst @@ -29,7 +29,7 @@ :template: custom-base-template.rst :nosignatures: - ~hexdump + ~hexdump_core diff --git a/docs/_autosummary/hexrec.xxd.HUMAN_ASCII.rst b/docs/_autosummary/hexrec.xxd.HUMAN_ASCII.rst new file mode 100644 index 0000000..d088d3b --- /dev/null +++ b/docs/_autosummary/hexrec.xxd.HUMAN_ASCII.rst @@ -0,0 +1,6 @@ +HUMAN\_ASCII +============ + +.. currentmodule:: hexrec.xxd + +.. autodata:: HUMAN_ASCII \ No newline at end of file diff --git a/docs/_autosummary/hexrec.xxd.HUMAN_EBCDIC.rst b/docs/_autosummary/hexrec.xxd.HUMAN_EBCDIC.rst new file mode 100644 index 0000000..893e295 --- /dev/null +++ b/docs/_autosummary/hexrec.xxd.HUMAN_EBCDIC.rst @@ -0,0 +1,6 @@ +HUMAN\_EBCDIC +============= + +.. currentmodule:: hexrec.xxd + +.. autodata:: HUMAN_EBCDIC \ No newline at end of file diff --git a/docs/_autosummary/hexrec.xxd.humanize.rst b/docs/_autosummary/hexrec.xxd.humanize.rst new file mode 100644 index 0000000..c16904d --- /dev/null +++ b/docs/_autosummary/hexrec.xxd.humanize.rst @@ -0,0 +1,6 @@ +humanize +======== + +.. currentmodule:: hexrec.xxd + +.. autofunction:: humanize \ No newline at end of file diff --git a/docs/_autosummary/hexrec.xxd.parse_seek.rst b/docs/_autosummary/hexrec.xxd.parse_seek.rst new file mode 100644 index 0000000..52b899a --- /dev/null +++ b/docs/_autosummary/hexrec.xxd.parse_seek.rst @@ -0,0 +1,6 @@ +parse\_seek +=========== + +.. currentmodule:: hexrec.xxd + +.. autofunction:: parse_seek \ No newline at end of file diff --git a/docs/_autosummary/hexrec.xxd.rst b/docs/_autosummary/hexrec.xxd.rst index 0331c26..6486861 100644 --- a/docs/_autosummary/hexrec.xxd.rst +++ b/docs/_autosummary/hexrec.xxd.rst @@ -6,14 +6,35 @@ + .. rubric:: Attributes + .. autosummary:: + :toctree: + :template: custom-base-template.rst + :nosignatures: + ~HUMAN_ASCII + ~HUMAN_EBCDIC + .. rubric:: Functions + + .. autosummary:: + :toctree: + :template: custom-base-template.rst + :nosignatures: + + ~humanize + ~parse_seek + ~xxd_core + + + + diff --git a/docs/_autosummary/hexrec.xxd.xxd_core.rst b/docs/_autosummary/hexrec.xxd.xxd_core.rst new file mode 100644 index 0000000..d1ca17d --- /dev/null +++ b/docs/_autosummary/hexrec.xxd.xxd_core.rst @@ -0,0 +1,6 @@ +xxd\_core +========= + +.. currentmodule:: hexrec.xxd + +.. autofunction:: xxd_core \ No newline at end of file diff --git a/src/hexrec/__init__.py b/src/hexrec/__init__.py index 35b1415..0a6583b 100644 --- a/src/hexrec/__init__.py +++ b/src/hexrec/__init__.py @@ -39,7 +39,7 @@ from .formats.srec import SrecFile from .formats.titxt import TiTxtFile from .formats.xtek import XtekFile -from .xxd import xxd +from .xxd import xxd_core def _register_default_file_types(): diff --git a/src/hexrec/cli.py b/src/hexrec/cli.py index e3fab08..9b7261a 100644 --- a/src/hexrec/cli.py +++ b/src/hexrec/cli.py @@ -56,11 +56,11 @@ from .base import guess_format_name from .formats.srec import SrecFile from .formats.srec import SrecRecord -from .hexdump import hexdump as hexdump_core +from .hexdump import hexdump_core from .utils import hexlify from .utils import parse_int from .utils import unhexlify -from .xxd import xxd as xxd_core +from .xxd import xxd_core class BasedIntParamType(click.ParamType): diff --git a/src/hexrec/hexdump.py b/src/hexrec/hexdump.py index 15e77eb..818ea56 100644 --- a/src/hexrec/hexdump.py +++ b/src/hexrec/hexdump.py @@ -326,7 +326,7 @@ def _format_two_bytes_hex( # noinspection PyShadowingBuiltins -def hexdump( +def hexdump_core( infile: Optional[Union[str, AnyBytes, IO]] = None, outfile: Optional[Union[str, AnyBytes, IO]] = None, one_byte_octal: bool = False, diff --git a/src/hexrec/xxd.py b/src/hexrec/xxd.py index 830d065..c117f62 100644 --- a/src/hexrec/xxd.py +++ b/src/hexrec/xxd.py @@ -131,7 +131,7 @@ def parse_seek(value: Optional[str]) -> Tuple[str, int]: return ss, sv -def xxd( +def xxd_core( infile: Optional[Union[str, AnyBytes, IO]] = None, outfile: Optional[Union[str, AnyBytes, IO]] = None, autoskip: bool = False, diff --git a/tests/test_hexdump.py b/tests/test_hexdump.py index 20621c8..9e30a14 100644 --- a/tests/test_hexdump.py +++ b/tests/test_hexdump.py @@ -1,4 +1,6 @@ import glob +import io +import os from pathlib import Path from typing import Any from typing import cast as _cast @@ -9,7 +11,7 @@ from test_base import replace_stdout from hexrec.cli import main as cli_main -from hexrec.hexdump import * +from hexrec.hexdump import hexdump_core @pytest.fixture @@ -93,7 +95,7 @@ def test_hexdump_io(datapath): data_in = stream.read() text_out = io.BytesIO() - hexdump(data_in, text_out) + hexdump_core(data_in, text_out) text_out = text_out.getvalue().replace(b'\r\n', b'\n').decode() text_ref = read_text(str(datapath / 'test_hexdump_bytes.bin.hexdump')) @@ -105,25 +107,25 @@ def test_hexdump_raises(datapath, tmppath): data_in = memoryview(stream.read()) with pytest.raises(NotImplementedError, match='"color" option is not supported'): - hexdump(data_in, color='') + hexdump_core(data_in, color='') with pytest.raises(NotImplementedError, match='"format" option is not supported'): - hexdump(data_in, format='') + hexdump_core(data_in, format='') with pytest.raises(NotImplementedError, match='"format_file" option is not supported'): - hexdump(data_in, format_file='') + hexdump_core(data_in, format_file='') with pytest.raises(ValueError, match='negative skip'): - hexdump(data_in, skip=-1) + hexdump_core(data_in, skip=-1) with pytest.raises(ValueError, match='negative length'): - hexdump(data_in, length=-1) + hexdump_core(data_in, length=-1) with pytest.raises(ValueError, match='invalid width'): - hexdump(data_in, width=0) + hexdump_core(data_in, width=0) with pytest.raises(ValueError, match='unknown format option'): - hexdump(data_in, format_order=['(this is a non-existing format)']) + hexdump_core(data_in, format_order=['(this is a non-existing format)']) def test_hexdump_stdin_stdout(datapath): @@ -133,7 +135,7 @@ def test_hexdump_stdin_stdout(datapath): fake_stdout = io.BytesIO() with replace_stdin(fake_stdin), replace_stdout(fake_stdout): - hexdump(linesep=b'\n') + hexdump_core(linesep=b'\n') text_out = fake_stdout.getvalue().decode() text_ref = read_text(str(datapath / 'test_hexdump_bytes.bin.hexdump')) @@ -145,7 +147,7 @@ def test_hexdump_str(datapath, tmppath): data_in = stream.read() path_out = str(tmppath / 'test_hexdump_str.hexdump') - hexdump(data_in, path_out) + hexdump_core(data_in, path_out) text_out = read_text(path_out) text_ref = read_text(str(datapath / 'test_hexdump_bytes.bin.hexdump')) @@ -156,7 +158,7 @@ def test_hexdump_stream(datapath): fake_stdout = io.BytesIO() with replace_stdout(fake_stdout): with open(str(datapath / 'bytes.bin'), 'rb') as stream_in: - hexdump(stream_in, linesep=b'\n') + hexdump_core(stream_in, linesep=b'\n') text_out = fake_stdout.getvalue().decode() text_ref = read_text(str(datapath / 'test_hexdump_bytes.bin.hexdump')) diff --git a/tests/test_xxd.py b/tests/test_xxd.py index a4bdae6..f379f8d 100644 --- a/tests/test_xxd.py +++ b/tests/test_xxd.py @@ -1,12 +1,18 @@ import argparse import glob +import io +import os +import sys from pathlib import Path import pytest from test_base import replace_stdin from test_base import replace_stdout -from hexrec.xxd import * +from hexrec.utils import parse_int +from hexrec.xxd import ZERO_BLOCK_SIZE +from hexrec.xxd import parse_seek +from hexrec.xxd import xxd_core @pytest.fixture @@ -76,7 +82,7 @@ def run_cli(args=None, namespace=None): args = parser.parse_args(args, namespace) kwargs = vars(args) - xxd(**kwargs) + xxd_core(**kwargs) def test_parse_seek(): @@ -154,61 +160,61 @@ def test_xxd(datapath): data_in = memoryview(stream.read()) with pytest.raises(ValueError, match='invalid column count'): - xxd(cols=-1) + xxd_core(cols=-1) with pytest.raises(ValueError, match='invalid column count'): - xxd(cols=257) + xxd_core(cols=257) with pytest.raises(ValueError, match='incompatible options'): - xxd(bits=True, postscript=True) + xxd_core(bits=True, postscript=True) with pytest.raises(ValueError, match='incompatible options'): - xxd(bits=True, include=True) + xxd_core(bits=True, include=True) with pytest.raises(ValueError, match='incompatible options'): - xxd(bits=True, revert=True) + xxd_core(bits=True, revert=True) with pytest.raises(ValueError, match='incompatible options'): - xxd(endian=True, postscript=True) + xxd_core(endian=True, postscript=True) with pytest.raises(ValueError, match='incompatible options'): - xxd(endian=True, include=True) + xxd_core(endian=True, include=True) with pytest.raises(ValueError, match='incompatible options'): - xxd(endian=True, revert=True) + xxd_core(endian=True, revert=True) with pytest.raises(ValueError, match='incompatible options'): - xxd(postscript=True, include=True) + xxd_core(postscript=True, include=True) with pytest.raises(ValueError, match='incompatible options'): - xxd(postscript=True, bits=True) + xxd_core(postscript=True, bits=True) with pytest.raises(ValueError, match='incompatible options'): - xxd(include=True, bits=True) + xxd_core(include=True, bits=True) with pytest.raises(ValueError, match='incompatible options'): - xxd(revert=False, oseek=0) + xxd_core(revert=False, oseek=0) with pytest.raises(ValueError, match='invalid seeking'): - xxd(revert=True, oseek=-1) + xxd_core(revert=True, oseek=-1) with pytest.raises(ValueError, match='invalid syntax'): - xxd(data_in, iseek='invalid') + xxd_core(data_in, iseek='invalid') with pytest.raises(ValueError, match='invalid seeking'): - xxd(data_in, iseek='+') + xxd_core(data_in, iseek='+') with pytest.raises(ValueError, match='invalid grouping'): - xxd(data_in, groupsize=-1) + xxd_core(data_in, groupsize=-1) with pytest.raises(ValueError, match='invalid grouping'): - xxd(data_in, groupsize=257) + xxd_core(data_in, groupsize=257) with pytest.raises(ValueError, match='offset overflow'): - xxd(data_in, offset=-1) + xxd_core(data_in, offset=-1) with pytest.raises(ValueError, match='offset overflow'): - xxd(data_in, offset=(1 << 32)) + xxd_core(data_in, offset=(1 << 32)) def test_xxd_stdinout(datapath): @@ -218,7 +224,7 @@ def test_xxd_stdinout(datapath): stream_out = io.BytesIO() with replace_stdin(stream_in), replace_stdout(stream_out): - xxd() + xxd_core() with open(str(datapath / 'test_xxd_bytes.bin.xxd'), 'rb') as stream: text_ref = stream.read().replace(b'\r\n', b'\n') @@ -232,7 +238,7 @@ def test_xxd_bytes(datapath): data_in = stream.read() text_out = io.BytesIO() - xxd(data_in, text_out) + xxd_core(data_in, text_out) with open(str(datapath / 'test_xxd_bytes.bin.xxd'), 'rb') as stream: text_ref = stream.read().replace(b'\r\n', b'\n') @@ -249,7 +255,7 @@ def test_xxd_bytes_seek(datapath): with replace_stdin(stream_in), replace_stdout(stream_out): sys.stdin.buffer.seek(96, io.SEEK_CUR) - xxd(iseek='+-64') + xxd_core(iseek='+-64') filename = 'test_xxd_-s_32_bytes.bin.xxd' with open(str(datapath / filename), 'rb') as stream: @@ -264,7 +270,7 @@ def test_xxd_include_stdin(datapath): data_in = stream.read() text_out = io.BytesIO() - xxd(data_in, text_out, include=True) + xxd_core(data_in, text_out, include=True) with open(str(datapath / 'bytes-stdin.c'), 'rb') as stream: text_ref = stream.read().replace(b'\r\n', b'\n') @@ -280,7 +286,7 @@ def test_xxd_none(datapath): fake_stdout = io.BytesIO() with replace_stdout(fake_stdout): with open(str(datapath / 'bytes.bin'), 'rb') as stream_in: - xxd(stream_in, linesep=b'\n') + xxd_core(stream_in, linesep=b'\n') text_out = fake_stdout.getvalue() assert text_out == text_ref @@ -293,7 +299,7 @@ def test_xxd_none_revert(datapath): fake_stdout = io.BytesIO() with replace_stdout(fake_stdout): with open(str(datapath / 'bytes.xxd'), 'rb') as stream_in: - xxd(stream_in, revert=True) + xxd_core(stream_in, revert=True) data_out = fake_stdout.getvalue() assert data_out == data_ref @@ -305,12 +311,12 @@ def test_xxd_none_revert_oseek(datapath): stream_in = io.BytesIO(data_in) stream_out = io.BytesIO() - xxd(stream_in, stream_out) + xxd_core(stream_in, stream_out) xxd_in = stream_out.getvalue() stream_in = io.BytesIO(xxd_in) stream_out = io.BytesIO() - xxd(stream_in, stream_out, oseek=skip, revert=True) + xxd_core(stream_in, stream_out, oseek=skip, revert=True) data_out = stream_out.getvalue() data_ref = (b'\0' * skip) + data_in @@ -323,12 +329,12 @@ def test_xxd_none_revert_offset(datapath): stream_in = io.BytesIO(data_in) stream_out = io.BytesIO() - xxd(stream_in, stream_out, offset=skip) + xxd_core(stream_in, stream_out, offset=skip) xxd_in = stream_out.getvalue() stream_in = io.BytesIO(xxd_in) stream_out = io.BytesIO() - xxd(stream_in, stream_out, revert=True) + xxd_core(stream_in, stream_out, revert=True) data_out = stream_out.getvalue() data_ref = (b'\0' * skip) + data_in