Skip to content

Commit f3872b7

Browse files
committed
show-bencodex
1 parent d29fdc6 commit f3872b7

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ To be released.
88

99
- Python 3.5 and 3.6 are no more supported. Instead, Python 3.8, 3.9, and 3.10
1010
are now officially supported.
11+
- Added ``show-bencodex``, a CLI program.
1112

1213

1314
Version 1.0.1

README.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ b'du3:agei30eu4:nameu8:Jane Doeu11:nationalitylu2:BRu2:USee'
3737
{'age': 30, 'name': 'Jane Doe', 'nationality': ['BR', 'US']}
3838

3939

40+
Debug-friendly formatter
41+
------------------------
42+
43+
The package also provides a CLI program named ``show-bencodex``, which shows
44+
the given Bencodex data file (or data from the stdin) in the debug-friendly
45+
format:
46+
47+
.. code:: console
48+
49+
$ show-bencodex bencodex.dat
50+
$ cat bencodex.dat | show-bencodex
51+
52+
4053
License
4154
-------
4255

bencodex/cli.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import argparse
2+
import io
3+
import pprint
4+
import re
5+
import shutil
6+
import sys
7+
from typing import Optional
8+
9+
from bencodex import load
10+
11+
__all__ = 'main',
12+
13+
parser = argparse.ArgumentParser(
14+
description='Debug-friendly Bencodex formatter',
15+
)
16+
parser.add_argument(
17+
'file',
18+
metavar='FILE',
19+
nargs='?',
20+
help='the path of the Bencodex file to show. if omitted (default) it '
21+
'expects the Bencodex data from the standard input'
22+
)
23+
parser.add_argument(
24+
'-x', '--hex-input',
25+
action='store_true',
26+
default=False,
27+
help='expects the input data is not binary, but hexadecimal text'
28+
)
29+
30+
31+
def read_input(path: Optional[str], hex: bool):
32+
if path is None:
33+
# Pipes are not seekable
34+
if hex:
35+
data = hex_to_bin(sys.stdin.read())
36+
else:
37+
data = sys.stdin.buffer.read()
38+
return io.BytesIO(data)
39+
elif hex:
40+
with open(path, 'r') as f:
41+
hex_data = f.read()
42+
data = hex_to_bin(hex_data)
43+
return io.BytesIO(data)
44+
return open(path, 'rb')
45+
46+
47+
def hex_to_bin(hex: str) -> bytes:
48+
return bytes.fromhex(re.sub(r'[\s.,:;_-]+', '', hex))
49+
50+
51+
def main() -> None:
52+
args = parser.parse_args()
53+
term_size = shutil.get_terminal_size()
54+
with read_input(args.file, args.hex_input) as f:
55+
tree = load(f)
56+
try:
57+
pprint.pprint(tree, width=term_size.columns, compact=True)
58+
except KeyboardInterrupt:
59+
raise SystemExit(130)
60+
61+
62+
if __name__ == '__main__':
63+
main()

setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ bencodex =
3434
spec/testsuite/*.dat
3535
spec/testsuite/*.yaml
3636
spec/testsuite/*.yml
37+
38+
[options.entry_points]
39+
console_scripts =
40+
show-bencodex = bencodex.cli:main

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ deps =
77
flake8 >= 3.6.0
88
flake8-import-order-spoqa >= 1.5.0
99
PyYAML ~= 3.13
10+
allowlist_externals =
11+
echo
1012
commands =
1113
python setup.py test
14+
echo 6475383a434c4920746573747465 | show-bencodex -x
1215
flake8
1316

1417
[testenv:mypy]

0 commit comments

Comments
 (0)