-
Notifications
You must be signed in to change notification settings - Fork 0
/
halp.py
95 lines (73 loc) · 2.37 KB
/
halp.py
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
92
93
94
95
import codecs
import io
import pydoc
import tokenize
# thanks to Python-wizard Akuli for showing me how to abuse codecs :)
# see his self-harming tutorial https://github.com/Akuli/import-that
# if you like this
class HelpEncodingIterator:
def __init__(self, tokens):
self.iterator = (
token
for token in tokens
if token.type not in (tokenize.NEWLINE, tokenize.COMMENT)
)
self._coming_up = None
@property
def coming_up(self):
if self._coming_up is None:
try:
self._coming_up = next(self.iterator)
except StopIteration:
return None
return self._coming_up
def __iter__(self):
return self
def __next__(self):
if self._coming_up is None:
return next(self.iterator)
tmp = self._coming_up
self._coming_up = None
return tmp
def __bool__(self):
return self.coming_up is not None
class HelpEncoding:
def __init__(self, tokens):
self.tokens = HelpEncodingIterator(tokens)
self.output = []
def parse(self):
while self.tokens:
self._parse_token()
def _parse_token(self):
token = next(self.tokens)
self.output.append(token)
if not (token.type == tokenize.NAME and token.string == "help"):
return
paren = next(self.tokens)
self.output.append(paren)
if paren.string != "(":
return
inner = next(self.tokens)
if inner.string in pydoc.Helper.keywords:
self.output.append(
tokenize.TokenInfo(
type=tokenize.STRING,
string=repr(inner.string),
start=inner.start,
end=inner.end,
line=inner.line,
)
)
else:
self.output.append(inner)
def encode(string, errors="strict"):
raise NotImplemented("this is nonsense, right?")
def decode(byteslike, errors="strict"):
code_bytes = bytes(byteslike)
decoder = HelpEncoding(
tokenize.tokenize(io.BytesIO(b"(" + code_bytes + b")").readline)
)
decoder.parse()
return (tokenize.untokenize(decoder.output[2:-2]), len(code_bytes))
codec_info = codecs.CodecInfo(encode, decode, name="halp")
codecs.register({"halp": codec_info}.get)