Skip to content

Commit

Permalink
enh: cleanup of path and pathlib usages (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Aug 13, 2020
1 parent af97862 commit fc5485c
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 87 deletions.
84 changes: 51 additions & 33 deletions playwright/async_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ async def screenshot(
self,
timeout: int = None,
type: Literal["png", "jpeg"] = None,
path: str = None,
path: typing.Union[str, pathlib.Path] = None,
quality: int = None,
omitBackground: bool = None,
) -> bytes:
Expand All @@ -1309,7 +1309,7 @@ async def screenshot(
Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the browserContext.setDefaultTimeout(timeout) or page.setDefaultTimeout(timeout) methods.
type : Optional[Literal['png', 'jpeg']]
Specify screenshot type, defaults to `png`.
path : Optional[str]
path : Union[str, pathlib.Path, NoneType]
The file path to save the image to. The screenshot type will be inferred from file extension. If `path` is a relative path, then it is resolved relative to current working directory. If no path is provided, the image won't be saved to the disk.
quality : Optional[int]
The quality of the image, between 0-100. Not applicable to `png` images.
Expand Down Expand Up @@ -2039,7 +2039,11 @@ def isDetached(self) -> bool:
return mapping.from_maybe_impl(self._impl_obj.isDetached())

async def addScriptTag(
self, url: str = None, path: str = None, content: str = None, type: str = None
self,
url: str = None,
path: typing.Union[str, pathlib.Path] = None,
content: str = None,
type: str = None,
) -> "ElementHandle":
"""Frame.addScriptTag
Expand All @@ -2049,7 +2053,7 @@ async def addScriptTag(
----------
url : Optional[str]
URL of a script to be added.
path : Optional[str]
path : Union[str, pathlib.Path, NoneType]
Path to the JavaScript file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory.
content : Optional[str]
Raw JavaScript content to be injected into frame.
Expand All @@ -2068,7 +2072,10 @@ async def addScriptTag(
)

async def addStyleTag(
self, url: str = None, path: str = None, content: str = None
self,
url: str = None,
path: typing.Union[str, pathlib.Path] = None,
content: str = None,
) -> "ElementHandle":
"""Frame.addStyleTag
Expand All @@ -2078,7 +2085,7 @@ async def addStyleTag(
----------
url : Optional[str]
URL of the `<link>` tag.
path : Optional[str]
path : Union[str, pathlib.Path, NoneType]
Path to the CSS file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory.
content : Optional[str]
Raw CSS content to be injected into frame.
Expand Down Expand Up @@ -2748,7 +2755,7 @@ async def register(
self,
name: str,
source: str = None,
path: str = None,
path: typing.Union[str, pathlib.Path] = None,
contentScript: bool = None,
) -> NoneType:
"""Selectors.register
Expand Down Expand Up @@ -2939,14 +2946,14 @@ async def path(self) -> typing.Union[str, NoneType]:
"""
return mapping.from_maybe_impl(await self._impl_obj.path())

async def saveAs(self, path: typing.Union[pathlib.Path, str]) -> NoneType:
async def saveAs(self, path: typing.Union[str, pathlib.Path]) -> NoneType:
"""Download.saveAs
Saves the download to a user-specified path.
Parameters
----------
path : Union[pathlib.Path, str]
path : Union[str, pathlib.Path]
Path where the download should be saved.
"""
return mapping.from_maybe_impl(await self._impl_obj.saveAs(path=path))
Expand Down Expand Up @@ -3397,7 +3404,11 @@ async def evalOnSelectorAll(
)

async def addScriptTag(
self, url: str = None, path: str = None, content: str = None, type: str = None
self,
url: str = None,
path: typing.Union[str, pathlib.Path] = None,
content: str = None,
type: str = None,
) -> "ElementHandle":
"""Page.addScriptTag
Expand All @@ -3408,7 +3419,7 @@ async def addScriptTag(
----------
url : Optional[str]
URL of a script to be added.
path : Optional[str]
path : Union[str, pathlib.Path, NoneType]
Path to the JavaScript file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory.
content : Optional[str]
Raw JavaScript content to be injected into frame.
Expand All @@ -3427,7 +3438,10 @@ async def addScriptTag(
)

async def addStyleTag(
self, url: str = None, path: str = None, content: str = None
self,
url: str = None,
path: typing.Union[str, pathlib.Path] = None,
content: str = None,
) -> "ElementHandle":
"""Page.addStyleTag
Expand All @@ -3438,7 +3452,7 @@ async def addStyleTag(
----------
url : Optional[str]
URL of the `<link>` tag.
path : Optional[str]
path : Union[str, pathlib.Path, NoneType]
Path to the CSS file to be injected into frame. If `path` is a relative path, then it is resolved relative to current working directory.
content : Optional[str]
Raw CSS content to be injected into frame.
Expand Down Expand Up @@ -3890,7 +3904,9 @@ async def bringToFront(self) -> NoneType:
"""
return mapping.from_maybe_impl(await self._impl_obj.bringToFront())

async def addInitScript(self, source: str = None, path: str = None) -> NoneType:
async def addInitScript(
self, source: str = None, path: typing.Union[str, pathlib.Path] = None
) -> NoneType:
"""Page.addInitScript
Adds a script which would be evaluated in one of the following scenarios:
Expand Down Expand Up @@ -3966,7 +3982,7 @@ async def screenshot(
self,
timeout: int = None,
type: Literal["png", "jpeg"] = None,
path: str = None,
path: typing.Union[str, pathlib.Path] = None,
quality: int = None,
omitBackground: bool = None,
fullPage: bool = None,
Expand All @@ -3982,7 +3998,7 @@ async def screenshot(
Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout. The default value can be changed by using the browserContext.setDefaultTimeout(timeout) or page.setDefaultTimeout(timeout) methods.
type : Optional[Literal['png', 'jpeg']]
Specify screenshot type, defaults to `png`.
path : Optional[str]
path : Union[str, pathlib.Path, NoneType]
The file path to save the image to. The screenshot type will be inferred from file extension. If `path` is a relative path, then it is resolved relative to current working directory. If no path is provided, the image won't be saved to the disk.
quality : Optional[int]
The quality of the image, between 0-100. Not applicable to `png` images.
Expand Down Expand Up @@ -4618,7 +4634,7 @@ async def pdf(
height: typing.Union[str, float] = None,
preferCSSPageSize: bool = None,
margin: PdfMargins = None,
path: str = None,
path: typing.Union[str, pathlib.Path] = None,
) -> bytes:
"""Page.pdf
Expand Down Expand Up @@ -4692,7 +4708,7 @@ async def pdf(
Give any CSS `@page` size declared in the page priority over what is declared in `width` and `height` or `format` options. Defaults to `false`, which will scale the content to fit the paper size.
margin : Optional[{"top": Union[str, int, NoneType], "right": Union[str, int, NoneType], "bottom": Union[str, int, NoneType], "left": Union[str, int, NoneType]}]
Paper margins, defaults to none.
path : Optional[str]
path : Union[str, pathlib.Path, NoneType]
The file path to save the PDF to. If `path` is a relative path, then it is resolved relative to current working directory. If no path is provided, the PDF won't be saved to the disk.
Returns
Expand Down Expand Up @@ -5016,7 +5032,9 @@ async def setOffline(self, offline: bool) -> NoneType:
"""
return mapping.from_maybe_impl(await self._impl_obj.setOffline(offline=offline))

async def addInitScript(self, source: str = None, path: str = None) -> NoneType:
async def addInitScript(
self, source: str = None, path: typing.Union[str, pathlib.Path] = None
) -> NoneType:
"""BrowserContext.addInitScript
Adds a script which would be evaluated in one of the following scenarios:
Expand Down Expand Up @@ -5568,7 +5586,7 @@ def executablePath(self) -> str:

async def launch(
self,
executablePath: str = None,
executablePath: typing.Union[str, pathlib.Path] = None,
args: typing.List[str] = None,
ignoreDefaultArgs: typing.Union[bool, typing.List[str]] = None,
handleSIGINT: bool = None,
Expand All @@ -5579,7 +5597,7 @@ async def launch(
headless: bool = None,
devtools: bool = None,
proxy: ProxyServer = None,
downloadsPath: str = None,
downloadsPath: typing.Union[str, pathlib.Path] = None,
slowMo: int = None,
chromiumSandbox: bool = None,
) -> "Browser":
Expand All @@ -5594,7 +5612,7 @@ async def launch(
Parameters
----------
executablePath : Optional[str]
executablePath : Union[str, pathlib.Path, NoneType]
Path to a browser executable to run instead of the bundled one. If `executablePath` is a relative path, then it is resolved relative to current working directory. Note that Playwright only works with the bundled Chromium, Firefox or WebKit, use at your own risk.
args : Optional[List[str]]
Additional arguments to pass to the browser instance. The list of Chromium flags can be found here.
Expand All @@ -5616,7 +5634,7 @@ async def launch(
**Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
proxy : Optional[{"server": str, "bypass": Optional[str], "username": Optional[str], "password": Optional[str]}]
Network proxy settings.
downloadsPath : Optional[str]
downloadsPath : Union[str, pathlib.Path, NoneType]
If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed.
slowMo : Optional[int]
Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
Expand Down Expand Up @@ -5649,7 +5667,7 @@ async def launch(

async def launchServer(
self,
executablePath: str = None,
executablePath: typing.Union[str, pathlib.Path] = None,
args: typing.List[str] = None,
ignoreDefaultArgs: typing.Union[bool, typing.List[str]] = None,
handleSIGINT: bool = None,
Expand All @@ -5660,7 +5678,7 @@ async def launchServer(
headless: bool = None,
devtools: bool = None,
proxy: ProxyServer = None,
downloadsPath: str = None,
downloadsPath: typing.Union[str, pathlib.Path] = None,
port: int = None,
chromiumSandbox: bool = None,
) -> "BrowserServer":
Expand All @@ -5670,7 +5688,7 @@ async def launchServer(
Parameters
----------
executablePath : Optional[str]
executablePath : Union[str, pathlib.Path, NoneType]
Path to a browser executable to run instead of the bundled one. If `executablePath` is a relative path, then it is resolved relative to current working directory. **BEWARE**: Playwright is only guaranteed to work with the bundled Chromium, Firefox or WebKit, use at your own risk.
args : Optional[List[str]]
Additional arguments to pass to the browser instance. The list of Chromium flags can be found here.
Expand All @@ -5692,7 +5710,7 @@ async def launchServer(
**Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
proxy : Optional[{"server": str, "bypass": Optional[str], "username": Optional[str], "password": Optional[str]}]
Network proxy settings.
downloadsPath : Optional[str]
downloadsPath : Union[str, pathlib.Path, NoneType]
If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed.
port : Optional[int]
Port to use for the web socket. Defaults to 0 that picks any available port.
Expand Down Expand Up @@ -5725,8 +5743,8 @@ async def launchServer(

async def launchPersistentContext(
self,
userDataDir: str,
executablePath: str = None,
userDataDir: typing.Union[str, pathlib.Path],
executablePath: typing.Union[str, pathlib.Path] = None,
args: typing.List[str] = None,
ignoreDefaultArgs: typing.Union[bool, typing.List[str]] = None,
handleSIGINT: bool = None,
Expand All @@ -5737,7 +5755,7 @@ async def launchPersistentContext(
headless: bool = None,
devtools: bool = None,
proxy: ProxyServer = None,
downloadsPath: str = None,
downloadsPath: typing.Union[str, pathlib.Path] = None,
slowMo: int = None,
viewport: IntSize = None,
ignoreHTTPSErrors: bool = None,
Expand All @@ -5764,9 +5782,9 @@ async def launchPersistentContext(
Parameters
----------
userDataDir : str
userDataDir : Union[str, pathlib.Path]
Path to a User Data Directory, which stores browser session data like cookies and local storage. More details for Chromium and Firefox.
executablePath : Optional[str]
executablePath : Union[str, pathlib.Path, NoneType]
Path to a browser executable to run instead of the bundled one. If `executablePath` is a relative path, then it is resolved relative to current working directory. **BEWARE**: Playwright is only guaranteed to work with the bundled Chromium, Firefox or WebKit, use at your own risk.
args : Optional[List[str]]
Additional arguments to pass to the browser instance. The list of Chromium flags can be found here.
Expand All @@ -5788,7 +5806,7 @@ async def launchPersistentContext(
**Chromium-only** Whether to auto-open a Developer Tools panel for each tab. If this option is `true`, the `headless` option will be set `false`.
proxy : Optional[{"server": str, "bypass": Optional[str], "username": Optional[str], "password": Optional[str]}]
Network proxy settings.
downloadsPath : Optional[str]
downloadsPath : Union[str, pathlib.Path, NoneType]
If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed.
slowMo : Optional[int]
Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on. Defaults to 0.
Expand Down
5 changes: 4 additions & 1 deletion playwright/browser_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import asyncio
from pathlib import Path
from types import SimpleNamespace
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union

Expand Down Expand Up @@ -139,7 +140,9 @@ async def setExtraHTTPHeaders(self, headers: Dict[str, str]) -> None:
async def setOffline(self, offline: bool) -> None:
await self._channel.send("setOffline", dict(offline=offline))

async def addInitScript(self, source: str = None, path: str = None) -> None:
async def addInitScript(
self, source: str = None, path: Union[str, Path] = None
) -> None:
if path:
with open(path, "r") as file:
source = file.read()
Expand Down
18 changes: 11 additions & 7 deletions playwright/browser_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def executablePath(self) -> str:

async def launch(
self,
executablePath: str = None,
executablePath: Union[str, Path] = None,
args: List[str] = None,
ignoreDefaultArgs: Union[bool, List[str]] = None,
handleSIGINT: bool = None,
Expand All @@ -59,7 +59,7 @@ async def launch(
headless: bool = None,
devtools: bool = None,
proxy: ProxyServer = None,
downloadsPath: str = None,
downloadsPath: Union[str, Path] = None,
slowMo: int = None,
chromiumSandbox: bool = None,
) -> Browser:
Expand All @@ -74,7 +74,7 @@ async def launch(

async def launchServer(
self,
executablePath: str = None,
executablePath: Union[str, Path] = None,
args: List[str] = None,
ignoreDefaultArgs: Union[bool, List[str]] = None,
handleSIGINT: bool = None,
Expand All @@ -85,7 +85,7 @@ async def launchServer(
headless: bool = None,
devtools: bool = None,
proxy: ProxyServer = None,
downloadsPath: str = None,
downloadsPath: Union[str, Path] = None,
port: int = None,
chromiumSandbox: bool = None,
) -> BrowserServer:
Expand All @@ -100,8 +100,8 @@ async def launchServer(

async def launchPersistentContext(
self,
userDataDir: str,
executablePath: str = None,
userDataDir: Union[str, Path],
executablePath: Union[str, Path] = None,
args: List[str] = None,
ignoreDefaultArgs: Union[bool, List[str]] = None,
handleSIGINT: bool = None,
Expand All @@ -112,7 +112,7 @@ async def launchPersistentContext(
headless: bool = None,
devtools: bool = None,
proxy: ProxyServer = None,
downloadsPath: str = None,
downloadsPath: Union[str, Path] = None,
slowMo: int = None,
viewport: IntSize = None,
ignoreHTTPSErrors: bool = None,
Expand Down Expand Up @@ -163,3 +163,7 @@ def normalize_launch_params(params: Dict) -> None:
params["ignoreAllDefaultArgs"] = True
del params["ignoreDefaultArgs"]
params["env"] = {name: str(value) for [name, value] in params["env"].items()}
if "executablePath" in params:
params["executablePath"] = str(Path(params["executablePath"]))
if "downloadsPath" in params:
params["downloadsPath"] = str(Path(params["downloadsPath"]))
2 changes: 1 addition & 1 deletion playwright/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ async def failure(self) -> Optional[str]:
async def path(self) -> Optional[str]:
return await self._channel.send("path")

async def saveAs(self, path: Union[Path, str]) -> None:
async def saveAs(self, path: Union[str, Path]) -> None:
path = str(Path(path))
return await self._channel.send("saveAs", dict(path=path))

0 comments on commit fc5485c

Please sign in to comment.