File: examples.py

package info (click to toggle)
mitmproxy 6.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 37,008 kB
  • sloc: python: 46,207; javascript: 6,250; xml: 189; sh: 138; ansic: 68; sql: 19; makefile: 13
file content (48 lines) | stat: -rwxr-xr-x 1,129 bytes parent folder | download
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
#!/usr/bin/env python3

import re
from pathlib import Path

here = Path(__file__).absolute().parent
example_dir = here / ".." / "src" / "examples" / "addons"
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)
    match = re.search(r'''
        ^
        (?:[#][^\n]*\n)?  # there might be a shebang
        """
        \s*
        (.+?)
        \s*
        (?:\n\n|""")     # stop on empty line or end of comment
    ''', code, re.VERBOSE)
    if match:
        comment = " — " + match.group(1)
    else:
        comment = ""
    overview.append(
        f"  * [{example.name}](#{slug}){comment}"
    )
    listings.append(f"""
<h2 id="{slug}">Example: {example.name}</h2>

```python
{code}
```
""")
print("\n".join(overview))
print("""
### Community Examples

Additional examples contributed by the mitmproxy community can be found
[on GitHub](https://github.com/mitmproxy/mitmproxy/tree/master/examples/contrib).

""")
print("\n".join(listings))