-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathutils.py
More file actions
175 lines (145 loc) · 5.6 KB
/
utils.py
File metadata and controls
175 lines (145 loc) · 5.6 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import json
import re
import warnings
from enum import Enum
from pathlib import Path
from typing import Iterator, Optional
from pydantic import BaseModel
EXAMPLES_ROOT = Path(__file__).parent.parent
with warnings.catch_warnings():
# This triggers some dumb warning in jupyter_core
warnings.simplefilter("ignore")
import jupytext
import jupytext.config
class ExampleType(int, Enum):
MODULE = 1
ASSET = 2
class Example(BaseModel):
type: ExampleType
filename: str # absolute filepath to example file
module: Optional[str] = (
None # python import path, or none if file is not a py module.
)
# TODO(erikbern): don't think the module is used (by docs or monitors)?
metadata: Optional[dict] = None
repo_filename: str # git repo relative filepath
cli_args: Optional[list] = None # Full command line args to run it
stem: Optional[str] = None # stem of path
tags: Optional[list[str]] = None # metadata tags for the example
env: Optional[dict[str, str]] = (
None # environment variables for the example
)
_RE_NEWLINE = re.compile(r"\r?\n")
_RE_FRONTMATTER = re.compile(r"^---$", re.MULTILINE)
_RE_CODEBLOCK = re.compile(r"\s*```[^`]+```\s*", re.MULTILINE)
def render_example_md(example: Example) -> str:
"""Render a Python code example to Markdown documentation format."""
with open(example.filename) as f:
content = f.read()
lines = _RE_NEWLINE.split(content)
markdown: list[str] = []
code: list[str] = []
for line in lines:
if line == "#" or line.startswith("# "):
if code:
markdown.extend(["```python", *code, "```", ""])
code = []
markdown.append(line[2:])
else:
if markdown and markdown[-1]:
markdown.append("")
if code or line:
code.append(line)
if code:
markdown.extend(["```python", *code, "```", ""])
text = "\n".join(markdown)
if _RE_FRONTMATTER.match(text):
# Strip out frontmatter from text.
if match := _RE_FRONTMATTER.search(text, 4):
text = text[match.end() + 1 :]
if match := _RE_CODEBLOCK.match(text):
filename = Path(example.filename).name
if match.end() == len(text):
# Special case: The entire page is a single big code block.
text = f"""# Example ({filename})
This is the source code for **{example.module}**.
{text}"""
return text
def gather_example_files(
parents: list[str], subdir: Path, ignored: list[str], recurse: bool
) -> Iterator[Example]:
config = jupytext.config.JupytextConfiguration(
root_level_metadata_as_raw_cell=False
)
for filename in sorted(list(subdir.iterdir())):
if filename.is_dir() and recurse:
# Gather two-subdirectories deep, but no further.
yield from gather_example_files(
parents + [str(subdir.stem)], filename, ignored, recurse=False
)
else:
filename_abs: str = str(filename.resolve())
ext: str = filename.suffix
if parents:
repo_filename: str = (
f"{'/'.join(parents)}/{subdir.name}/{filename.name}"
)
else:
repo_filename: str = f"{subdir.name}/{filename.name}"
if ext == ".py" and filename.stem != "__init__":
if parents:
parent_mods = ".".join(parents)
module = f"{parent_mods}.{subdir.stem}.{filename.stem}"
else:
module = f"{subdir.stem}.{filename.stem}"
data = jupytext.read(open(filename_abs), config=config)
metadata = data["metadata"]["jupytext"].get(
"root_level_metadata", {}
)
cmd = metadata.get("cmd", ["modal", "run", repo_filename])
args = metadata.get("args", [])
tags = metadata.get("tags", [])
env = metadata.get("env", dict())
yield Example(
type=ExampleType.MODULE,
filename=filename_abs,
metadata=metadata,
module=module,
repo_filename=repo_filename,
cli_args=(cmd + args),
stem=Path(filename_abs).stem,
tags=tags,
env=env,
)
elif ext in [".png", ".jpeg", ".jpg", ".gif", ".mp4"]:
yield Example(
type=ExampleType.ASSET,
filename=filename_abs,
repo_filename=repo_filename,
)
else:
ignored.append(str(filename))
def get_examples() -> Iterator[Example]:
"""Yield all Python module files and asset files relevant to building modal.com/docs."""
if not EXAMPLES_ROOT.exists():
raise Exception(
f"Can't find directory {EXAMPLES_ROOT}. You might need to clone the modal-examples repo there."
)
ignored = []
for subdir in sorted(
p
for p in EXAMPLES_ROOT.iterdir()
if p.is_dir()
and not p.name.startswith(".")
and not p.name.startswith("internal")
and not p.name.startswith("misc")
):
yield from gather_example_files(
parents=[], subdir=subdir, ignored=ignored, recurse=True
)
def get_examples_json():
examples = list(ex.dict() for ex in get_examples())
return json.dumps(examples)
if __name__ == "__main__":
for example in get_examples():
print(example.json())