1
|
|
|
"""Command line program for EIP 1577 content hash.""" |
2
|
|
|
|
3
|
|
|
# pylint: disable=W0703 |
4
|
|
|
|
5
|
|
|
import argparse |
6
|
|
|
import sys |
7
|
|
|
|
8
|
|
|
from . import get_codec, decode, encode |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def handle_get_codec(args): |
12
|
|
|
"""Handle get codec.""" |
13
|
|
|
|
14
|
|
|
try: |
15
|
|
|
chash = args.chash |
16
|
|
|
codec = get_codec(chash) |
17
|
|
|
|
18
|
|
|
except BaseException as err: # pragma: no cover |
19
|
|
|
print(str(err), file=sys.stderr) |
20
|
|
|
sys.exit(1) |
21
|
|
|
|
22
|
|
|
print(codec) |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def handle_decode(args): |
26
|
|
|
"""Handle decode.""" |
27
|
|
|
|
28
|
|
|
try: |
29
|
|
|
chash = args.chash |
30
|
|
|
value = decode(chash) |
31
|
|
|
|
32
|
|
|
except BaseException as err: # pragma: no cover |
33
|
|
|
print(str(err), file=sys.stderr) |
34
|
|
|
sys.exit(1) |
35
|
|
|
|
36
|
|
|
print(value) |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
def handle_encode(args): |
40
|
|
|
"""Handle encode.""" |
41
|
|
|
|
42
|
|
|
try: |
43
|
|
|
codec = args.codec |
44
|
|
|
value = args.value |
45
|
|
|
chash = encode(codec, value) |
46
|
|
|
|
47
|
|
|
except BaseException as err: # pragma: no cover |
48
|
|
|
print(str(err), file=sys.stderr) |
49
|
|
|
sys.exit(1) |
50
|
|
|
|
51
|
|
|
print(chash) |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def main(): |
55
|
|
|
"""Handle command line program.""" |
56
|
|
|
|
57
|
|
|
parser = argparse.ArgumentParser( |
58
|
|
|
prog=__package__, |
59
|
|
|
description='Python implementation of EIP 1577 content hash' |
60
|
|
|
) |
61
|
|
|
subparsers = parser.add_subparsers() |
62
|
|
|
|
63
|
|
|
get_codec_parser = subparsers.add_parser( |
64
|
|
|
name='get-codec', |
65
|
|
|
help='extract the codec of a content hash' |
66
|
|
|
) |
67
|
|
|
get_codec_parser.set_defaults(which='get-codec') |
68
|
|
|
get_codec_parser.add_argument('chash', help='an hex string containing a content hash') |
69
|
|
|
|
70
|
|
|
get_codec_parser = subparsers.add_parser( |
71
|
|
|
name='decode', |
72
|
|
|
help='decode a content hash' |
73
|
|
|
) |
74
|
|
|
get_codec_parser.set_defaults(which='decode') |
75
|
|
|
get_codec_parser.add_argument('chash', help='an hex string containing a content hash') |
76
|
|
|
|
77
|
|
|
get_codec_parser = subparsers.add_parser( |
78
|
|
|
name='encode', |
79
|
|
|
help='encode a content hash' |
80
|
|
|
) |
81
|
|
|
get_codec_parser.set_defaults(which='encode') |
82
|
|
|
get_codec_parser.add_argument('codec', help='a codec of a content hash') |
83
|
|
|
get_codec_parser.add_argument('value', help='a value of a content hash') |
84
|
|
|
|
85
|
|
|
if len(sys.argv) == 1: # pragma: no cover |
86
|
|
|
parser.print_help(sys.stderr) |
87
|
|
|
sys.exit(1) |
88
|
|
|
|
89
|
|
|
args = parser.parse_args() |
90
|
|
|
|
91
|
|
|
if args.which == 'get-codec': |
92
|
|
|
handle_get_codec(args) |
93
|
|
|
elif args.which == 'decode': |
94
|
|
|
handle_decode(args) |
95
|
|
|
elif args.which == 'encode': |
96
|
|
|
handle_encode(args) |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
if __name__ == '__main__': # pragma: no cover |
100
|
|
|
main() |
101
|
|
|
|