Skip to content

Commit e95e482

Browse files
committed
Making the b64 binary a lot more flexible. It now allows only parts of the consumed line to be b64 encoded/decoded.
1 parent 4587a70 commit e95e482

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ services:
6969

7070
* **b64**
7171

72-
Base64 encodes (`--encode`) or decodes (`--decode`) data from STDIN to STDOUT.
72+
Base64 encodes (`--encode`) or decodes (`--decode`) data from STDIN to STDOUT. Optinally takes two arguments, the `input_format_specification` and the `output_format_specification` to flexibly allow only parts of the input to be encoded/decoded.
7373

7474
* **jsonify**
7575

bin/b64

+68-8
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,83 @@ Command line utility tool for processing input from stdin. Each line on the
55
input stream is base64 encoded with no wrapping and ended with a newline.
66
"""
77

8+
# pylint: disable=duplicate-code
9+
810
import sys
11+
import logging
12+
import warnings
913
import argparse
1014
from base64 import b64encode, b64decode
1115

16+
import parse
17+
1218
parser = argparse.ArgumentParser()
1319
group = parser.add_mutually_exclusive_group(required=True)
1420
group.add_argument("--encode", action="store_true", default=False)
1521
group.add_argument("--decode", action="store_true", default=False)
22+
parser.add_argument(
23+
"--log-level", type=lambda level: getattr(logging, level), default=logging.WARNING
24+
)
25+
parser.add_argument(
26+
"input_specification",
27+
type=str,
28+
nargs="?",
29+
default="{input}",
30+
help="Example: '{timestamp} {data}',"
31+
"See https://github.com/r1chardj0n3s/parse#format-specification",
32+
)
33+
parser.add_argument(
34+
"output_specification",
35+
type=str,
36+
nargs="?",
37+
default="{output}",
38+
help="Example: '{data}',"
39+
"See https://github.com/r1chardj0n3s/parse#format-specification",
40+
)
1641

1742
args = parser.parse_args()
1843

19-
if args.encode:
20-
for line in sys.stdin:
21-
sys.stdout.write(b64encode(line.encode()).decode() + "\n")
22-
sys.stdout.flush()
44+
# Setup logger
45+
logging.basicConfig(
46+
format="%(asctime)s %(levelname)s %(name)s %(message)s", level=args.log_level
47+
)
48+
logging.captureWarnings(True)
49+
warnings.filterwarnings("once")
50+
51+
logger = logging.getLogger("b64")
52+
53+
# Compile pattern
54+
input_pattern = parse.compile(args.input_specification)
55+
56+
for line in sys.stdin:
57+
logger.debug(line)
58+
res = input_pattern.parse(line.rstrip())
59+
60+
if not res:
61+
logger.error(
62+
"Could not parse line: %s according to the input_specification: %s",
63+
line,
64+
args.input_specification,
65+
)
66+
continue
67+
68+
if not "input" in res.named:
69+
logger.error(
70+
"Could not find the expected named argument 'input' in the input specification: %s",
71+
args.input_specification,
72+
)
73+
continue
74+
75+
parts = res.named
76+
77+
_input = parts.pop("input")
78+
output = (
79+
b64encode(_input.encode()).decode()
80+
if args.encode
81+
else b64decode(_input.encode()).decode()
82+
)
83+
84+
parts["output"] = output
2385

24-
else:
25-
for line in sys.stdin:
26-
sys.stdout.write(b64decode(line.encode()).decode())
27-
sys.stdout.flush()
86+
sys.stdout.write(args.output_specification.format(**parts) + "\n")
87+
sys.stdout.flush()

bin/jsonify

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ input stream is parsed according to the specification provided by the user
66
and assembled into a json object.
77
"""
88

9+
# pylint: disable=duplicate-code
10+
911
import sys
1012
import json
1113
import logging

0 commit comments

Comments
 (0)