Skip to content

docstrings: only render async/sync dialect snippets #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 5 additions & 35 deletions playwright/async_api/_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,18 +664,12 @@ def expect_event(
`predicate` function and waits for `predicate(event)` to return a truthy value. Will throw an error if the socket is
closed before the `event` is fired.

```python async
```py
async with ws.expect_event(event_name) as event_info:
await ws.click("button")
value = await event_info.value
```

```python sync
with ws.expect_event(event_name) as event_info:
ws.click("button")
value = event_info.value
```

Parameters
----------
event : str
Expand Down Expand Up @@ -4597,18 +4591,12 @@ def expect_navigation(
cause the page to navigate. e.g. The click target has an `onclick` handler that triggers navigation from a `setTimeout`.
Consider this example:

```python async
```py
async with frame.expect_navigation():
await frame.click("a.delayed-navigation") # Clicking the link will indirectly cause a navigation
# Context manager waited for the navigation to happen.
```

```python sync
with frame.expect_navigation():
frame.click("a.delayed-navigation") # Clicking the link will indirectly cause a navigation
# Context manager waited for the navigation to happen.
```

> NOTE: Usage of the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) to change the URL is
considered a navigation.

Expand Down Expand Up @@ -7890,18 +7878,12 @@ def expect_event(
`predicate` function and waits for `predicate(event)` to return a truthy value. Will throw an error if the page is
closed before the `event` is fired.

```python async
```py
async with page.expect_event(event_name) as event_info:
await page.click("button")
value = await event_info.value
```

```python sync
with page.expect_event(event_name) as event_info:
page.click("button")
value = event_info.value
```

Parameters
----------
event : str
Expand Down Expand Up @@ -8024,18 +8006,12 @@ def expect_navigation(
cause the page to navigate. e.g. The click target has an `onclick` handler that triggers navigation from a `setTimeout`.
Consider this example:

```python async
```py
async with page.expect_navigation():
await page.click("a.delayed-navigation") # Clicking the link will indirectly cause a navigation
# Context manager waited for the navigation to happen.
```

```python sync
with page.expect_navigation():
page.click("a.delayed-navigation") # Clicking the link will indirectly cause a navigation
# Context manager waited for the navigation to happen.
```

> NOTE: Usage of the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) to change the URL is
considered a navigation.

Expand Down Expand Up @@ -8770,18 +8746,12 @@ def expect_event(
`predicate` function and waits for `predicate(event)` to return a truthy value. Will throw an error if browser context
is closed before the `event` is fired.

```python async
```py
async with context.expect_event("page") as event_info:
await context.click("button")
page = await event_info.value
```

```python sync
with context.expect_event("page") as event_info:
context.click("button")
page = event_info.value
```

Parameters
----------
event : str
Expand Down
40 changes: 5 additions & 35 deletions playwright/sync_api/_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,13 +670,7 @@ def expect_event(
`predicate` function and waits for `predicate(event)` to return a truthy value. Will throw an error if the socket is
closed before the `event` is fired.

```python async
async with ws.expect_event(event_name) as event_info:
await ws.click("button")
value = await event_info.value
```

```python sync
```py
with ws.expect_event(event_name) as event_info:
ws.click("button")
value = event_info.value
Expand Down Expand Up @@ -4720,13 +4714,7 @@ def expect_navigation(
cause the page to navigate. e.g. The click target has an `onclick` handler that triggers navigation from a `setTimeout`.
Consider this example:

```python async
async with frame.expect_navigation():
await frame.click("a.delayed-navigation") # Clicking the link will indirectly cause a navigation
# Context manager waited for the navigation to happen.
```

```python sync
```py
with frame.expect_navigation():
frame.click("a.delayed-navigation") # Clicking the link will indirectly cause a navigation
# Context manager waited for the navigation to happen.
Expand Down Expand Up @@ -8117,13 +8105,7 @@ def expect_event(
`predicate` function and waits for `predicate(event)` to return a truthy value. Will throw an error if the page is
closed before the `event` is fired.

```python async
async with page.expect_event(event_name) as event_info:
await page.click("button")
value = await event_info.value
```

```python sync
```py
with page.expect_event(event_name) as event_info:
page.click("button")
value = event_info.value
Expand Down Expand Up @@ -8247,13 +8229,7 @@ def expect_navigation(
cause the page to navigate. e.g. The click target has an `onclick` handler that triggers navigation from a `setTimeout`.
Consider this example:

```python async
async with page.expect_navigation():
await page.click("a.delayed-navigation") # Clicking the link will indirectly cause a navigation
# Context manager waited for the navigation to happen.
```

```python sync
```py
with page.expect_navigation():
page.click("a.delayed-navigation") # Clicking the link will indirectly cause a navigation
# Context manager waited for the navigation to happen.
Expand Down Expand Up @@ -9008,13 +8984,7 @@ def expect_event(
`predicate` function and waits for `predicate(event)` to return a truthy value. Will throw an error if browser context
is closed before the `event` is fired.

```python async
async with context.expect_event("page") as event_info:
await context.click("button")
page = await event_info.value
```

```python sync
```py
with context.expect_event("page") as event_info:
context.click("button")
page = event_info.value
Expand Down
23 changes: 16 additions & 7 deletions scripts/documentation_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@


class DocumentationProvider:
def __init__(self) -> None:
def __init__(self, is_async: bool) -> None:
self.is_async = is_async
self.api: Any = {}
self.printed_entries: List[str] = []
process_output = subprocess.run(
Expand Down Expand Up @@ -190,21 +191,29 @@ def indent_paragraph(self, p: str, indent: str) -> str:
def beautify_method_comment(self, comment: str, indent: str) -> str:
lines = comment.split("\n")
result = []
in_example = False
skip_example = False
last_was_blank = True
for line in lines:
if not line.strip():
last_was_blank = True
continue
if line.strip() == "```js":
in_example = True
if not in_example:
match = re.match(r"\s*```(.+)", line)
if match:
lang = match[1]
if lang in ["html", "yml", "sh", "py", "python"]:
Copy link
Member

@mxschmitt mxschmitt Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the long-term it would be better to move that upstream I think, so each method has language specific examples attached. We loose then the location and have to put them at the end of a method description but should be fine.
Better than each language has to implement this in my opinion.

skip_example = False
elif lang == "python " + ("async" if self.is_async else "sync"):
skip_example = False
line = "```py"
else:
skip_example = True
if not skip_example:
if last_was_blank:
last_was_blank = False
result.append("")
result.append(self.render_links(line))
if line.strip() == "```":
in_example = False
if skip_example and line.strip() == "```":
skip_example = False
return self.indent_paragraph("\n".join(result), indent)

def render_links(self, comment: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate_async_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
signature,
)

documentation_provider = DocumentationProvider()
documentation_provider = DocumentationProvider(True)


def generate(t: Any) -> None:
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate_sync_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
signature,
)

documentation_provider = DocumentationProvider()
documentation_provider = DocumentationProvider(False)


def generate(t: Any) -> None:
Expand Down