forked from mk-fg/fgtk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repr
executable file
·91 lines (82 loc) · 4.07 KB
/
repr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
import itertools as it, operator as op, functools as ft
import os, sys
def main(args=None):
import argparse, textwrap
dedent = lambda text: (textwrap.dedent(text).strip('\n') + '\n').replace('\t', ' ')
class SmartHelpFormatter(argparse.HelpFormatter):
def __init__(self, *args, **kws):
return super().__init__(*args, **kws, width=100)
def _fill_text(self, text, width, indent):
if '\n' not in text: return super()._fill_text(text, width, indent)
return ''.join(indent + line for line in text.splitlines(keepends=True))
def _split_lines(self, text, width):
return super()._split_lines(text, width)\
if '\n' not in text else dedent(text).splitlines()
parser = argparse.ArgumentParser(
formatter_class=SmartHelpFormatter,
description='Run python repr() to stdout on every (text) line of an input file.')
parser.add_argument('file', nargs='*',
help='Input file path(s). Stdin is used if none specified or in place of "-".')
parser.add_argument('-b', '--binary', action='store_true',
help='Process file lines in binary mode, instead of decoding it with utf-8.'
' Should allow to see BOM and random'
' binary goop escaped as individual bytes, not codepoints.')
parser.add_argument('-n', '--hide-newlines', action='store_true',
help='Enables "universal newlines" translation and strips last \\n from each line.'
' Whether this is sufficient for mismatched line endings'
' depends on how aggressive that mode is in converting newlines.')
parser.add_argument('-x', '--error-replace', action='store_true',
help='When decoding stuff (non-binary mode, convert), replace non-decodable chars.')
parser.add_argument('-c', '--convert',
const='utf-8', nargs='?', metavar='((enc-src:)enc-dst)(+opts)',
help='''
Instead of repr-output, convert encoding
from enc-src (default=utf-8-sig) to enc-dst (utf-8) and output the result.
[[enc-src:]enc-dst] spec can be followed by + and any of the following options:
+r - output windows \\r\\n newlines,
+i - overwrite file in-place, same as sed -i would do
+t - trim whitespace from line endings, and ensure final newline
Examples: -c utf-8-sig+ri, -c +t, -c cp1251:utf-8+it
Allows to use stuff like utf-8-sig or any other codec supported by python.''')
opts = parser.parse_args(sys.argv[1:] if args is None else args)
file_list, file_nl = opts.file, None
if not file_list: file_list = ['-']
if opts.convert:
file_mode = 'rb'
enc_src, enc_dst, enc_opts = 'utf-8-sig', opts.convert, ''
if ':' in enc_dst: enc_src, enc_dst = enc_dst.split(':', 1)
if '+' in enc_dst: enc_dst, enc_opts = enc_dst.split('+', 1)
if not enc_src: enc_src = 'utf-8-sig'
if not enc_dst: enc_dst = 'utf-8'
assert not set(enc_opts).difference('rit'),\
f'Not all encoding options recognized: {enc_opts}'
dst_stdout = open(sys.stdout.fileno(), 'wb')
enc, dec_errors = dict(), 'strict' if not opts.error_replace else 'replace'
else:
file_mode = 'r' if not opts.binary else 'rb'
repr_start = 1 if not opts.binary else 2 # b'' vs ''
file_nl = ('' if not opts.hide_newlines else None) if not opts.binary else None
line_end_hide = ('\n' if not opts.binary else b'\n') if opts.hide_newlines else None
newline='' if not opts.hide_newlines else None
enc = dict(encoding='utf-8-sig', errors='replace') if opts.error_replace else dict()
for p in file_list:
p_file = p != '-'
with open(
p if p_file else sys.stdin.fileno(),
mode=file_mode, newline=file_nl, **enc) as src:
if opts.convert:
text = src.read().decode(enc_src, dec_errors).encode(enc_dst)
while b'\r\n' in text: text = text.replace(b'\r\n', b'\n')
if 't' in enc_opts:
text = b'\n'.join(it.chain(
(line.rstrip() for line in text.rstrip().split(b'\n')), [b''] ))
if 'r' in enc_opts: text = text.replace(b'\n', b'\r\n')
if 'i' in enc_opts and p_file:
with open(p, 'wb') as dst: dst.write(text)
else: dst_stdout.write(text)
else:
for line in src:
if line_end_hide and line.endswith(line_end_hide): line = line[:-1]
print(repr(line)[repr_start:-1])
if __name__ == '__main__': sys.exit(main())