mitmproxy/docs/scripts/examples.py

72 lines
1.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import re
from pathlib import Path
here = Path(__file__).absolute().parent
example_dir = here / ".." / "src" / "examples" / "addons"
2022-04-26 11:53:35 +00:00
examples = example_dir.glob("*.py")
overview = []
listings = []
for example in examples:
code = example.read_text()
slug = str(example.with_suffix("").relative_to(example_dir))
slug = re.sub(r"[^a-zA-Z]", "-", slug)
2022-04-26 11:53:35 +00:00
match = re.search(
r'''
^
(?:[#][^\n]*\n)? # there might be a shebang
"""
\s*
(.+?)
\s*
(?:\n\n|""") # stop on empty line or end of comment
2022-04-26 11:53:35 +00:00
''',
code,
re.VERBOSE,
)
if match:
comment = "" + match.group(1)
else:
comment = ""
2022-04-26 11:53:35 +00:00
overview.append(f" * [{example.name}](#{slug}){comment}\n")
listings.append(
f"""
2021-02-04 21:52:28 +00:00
<h3 id="{slug}">Example: {example.name}</h3>
```python
2021-02-09 00:05:02 +00:00
{code.strip()}
```
2022-04-26 11:53:35 +00:00
"""
)
2021-02-09 00:05:02 +00:00
2022-04-26 11:53:35 +00:00
print(
f"""
2021-02-09 00:05:02 +00:00
# Addon Examples
### Dedicated Example Addons
{"".join(overview)}
### Built-In Addons
Much of mitmproxys own functionality is defined in
2021-03-16 15:17:27 +00:00
[a suite of built-in addons](https://github.com/mitmproxy/mitmproxy/tree/main/mitmproxy/addons),
2021-02-09 00:05:02 +00:00
implementing everything from functionality like anticaching and sticky cookies to our onboarding webapp.
The built-in addons make for instructive reading, and you will quickly see that quite complex functionality
can often boil down to a very small, completely self-contained modules.
### Additional Community Examples
Additional examples contributed by the mitmproxy community can be found
2021-03-16 15:17:27 +00:00
[on GitHub](https://github.com/mitmproxy/mitmproxy/tree/main/examples/contrib).
2021-02-09 00:05:02 +00:00
-------------------------
{"".join(listings)}
2022-04-26 11:53:35 +00:00
"""
)