draw tree of HTTPX exceptions
This commit is contained in:
35
20-executors/getflags/httpx-error-tree/drawtree.py
Executable file
35
20-executors/getflags/httpx-error-tree/drawtree.py
Executable file
@@ -0,0 +1,35 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from tree import tree
|
||||||
|
|
||||||
|
|
||||||
|
SP = '\N{SPACE}'
|
||||||
|
HLIN = '\N{BOX DRAWINGS LIGHT HORIZONTAL}' # ─
|
||||||
|
ELBOW = f'\N{BOX DRAWINGS LIGHT UP AND RIGHT}{HLIN*2}{SP}' # └──
|
||||||
|
TEE = f'\N{BOX DRAWINGS LIGHT VERTICAL AND RIGHT}{HLIN*2}{SP}' # ├──
|
||||||
|
PIPE = f'\N{BOX DRAWINGS LIGHT VERTICAL}{SP*3}' # │
|
||||||
|
|
||||||
|
|
||||||
|
def cls_name(cls):
|
||||||
|
module = 'builtins.' if cls.__module__ == 'builtins' else ''
|
||||||
|
return module + cls.__name__
|
||||||
|
|
||||||
|
def render_lines(tree_iter):
|
||||||
|
cls, _, _ = next(tree_iter)
|
||||||
|
yield cls_name(cls)
|
||||||
|
prefix = ''
|
||||||
|
|
||||||
|
for cls, level, last in tree_iter:
|
||||||
|
prefix = prefix[:4 * (level-1)]
|
||||||
|
prefix = prefix.replace(TEE, PIPE).replace(ELBOW, SP*4)
|
||||||
|
prefix += ELBOW if last else TEE
|
||||||
|
yield prefix + cls_name(cls)
|
||||||
|
|
||||||
|
|
||||||
|
def draw(cls):
|
||||||
|
for line in render_lines(tree(cls)):
|
||||||
|
print(line)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
draw(Exception)
|
||||||
24
20-executors/getflags/httpx-error-tree/tree.py
Executable file
24
20-executors/getflags/httpx-error-tree/tree.py
Executable file
@@ -0,0 +1,24 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import httpx # make httpx classes available to .__subclasses__()
|
||||||
|
|
||||||
|
|
||||||
|
def tree(cls, level=0, last_sibling=True):
|
||||||
|
yield cls, level, last_sibling
|
||||||
|
subclasses = [c for c in cls.__subclasses__()
|
||||||
|
if c.__module__ == 'httpx' or c is RuntimeError]
|
||||||
|
if subclasses:
|
||||||
|
last = subclasses[-1]
|
||||||
|
for sub_cls in subclasses:
|
||||||
|
yield from tree(sub_cls, level+1, sub_cls is last)
|
||||||
|
|
||||||
|
|
||||||
|
def display(cls):
|
||||||
|
for cls, level, _ in tree(cls):
|
||||||
|
indent = ' ' * 4 * level
|
||||||
|
module = 'builtins.' if cls.__module__ == 'builtins' else ''
|
||||||
|
print(f'{indent}{module}{cls.__name__}')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
display(Exception)
|
||||||
Reference in New Issue
Block a user