Skip to content

Commit fc5485c

Browse files
authored
enh: cleanup of path and pathlib usages (microsoft#166)
1 parent af97862 commit fc5485c

File tree

9 files changed

+149
-87
lines changed

9 files changed

+149
-87
lines changed

playwright/async_api.py

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ async def screenshot(
12951295
self,
12961296
timeout: int = None,
12971297
type: Literal["png", "jpeg"] = None,
1298-
path: str = None,
1298+
path: typing.Union[str, pathlib.Path] = None,
12991299
quality: int = None,
13001300
omitBackground: bool = None,
13011301
) -> bytes:
@@ -1309,7 +1309,7 @@ async def screenshot(
13091309
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.
13101310
type : Optional[Literal['png', 'jpeg']]
13111311
Specify screenshot type, defaults to `png`.
1312-
path : Optional[str]
1312+
path : Union[str, pathlib.Path, NoneType]
13131313
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.
13141314
quality : Optional[int]
13151315
The quality of the image, between 0-100. Not applicable to `png` images.
@@ -2039,7 +2039,11 @@ def isDetached(self) -> bool:
20392039
return mapping.from_maybe_impl(self._impl_obj.isDetached())
20402040

20412041
async def addScriptTag(
2042-
self, url: str = None, path: str = None, content: str = None, type: str = None
2042+
self,
2043+
url: str = None,
2044+
path: typing.Union[str, pathlib.Path] = None,
2045+
content: str = None,
2046+
type: str = None,
20432047
) -> "ElementHandle":
20442048
"""Frame.addScriptTag
20452049
@@ -2049,7 +2053,7 @@ async def addScriptTag(
20492053
----------
20502054
url : Optional[str]
20512055
URL of a script to be added.
2052-
path : Optional[str]
2056+
path : Union[str, pathlib.Path, NoneType]
20532057
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.
20542058
content : Optional[str]
20552059
Raw JavaScript content to be injected into frame.
@@ -2068,7 +2072,10 @@ async def addScriptTag(
20682072
)
20692073

20702074
async def addStyleTag(
2071-
self, url: str = None, path: str = None, content: str = None
2075+
self,
2076+
url: str = None,
2077+
path: typing.Union[str, pathlib.Path] = None,
2078+
content: str = None,
20722079
) -> "ElementHandle":
20732080
"""Frame.addStyleTag
20742081
@@ -2078,7 +2085,7 @@ async def addStyleTag(
20782085
----------
20792086
url : Optional[str]
20802087
URL of the `<link>` tag.
2081-
path : Optional[str]
2088+
path : Union[str, pathlib.Path, NoneType]
20822089
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.
20832090
content : Optional[str]
20842091
Raw CSS content to be injected into frame.
@@ -2748,7 +2755,7 @@ async def register(
27482755
self,
27492756
name: str,
27502757
source: str = None,
2751-
path: str = None,
2758+
path: typing.Union[str, pathlib.Path] = None,
27522759
contentScript: bool = None,
27532760
) -> NoneType:
27542761
"""Selectors.register
@@ -2939,14 +2946,14 @@ async def path(self) -> typing.Union[str, NoneType]:
29392946
"""
29402947
return mapping.from_maybe_impl(await self._impl_obj.path())
29412948

2942-
async def saveAs(self, path: typing.Union[pathlib.Path, str]) -> NoneType:
2949+
async def saveAs(self, path: typing.Union[str, pathlib.Path]) -> NoneType:
29432950
"""Download.saveAs
29442951
29452952
Saves the download to a user-specified path.
29462953
29472954
Parameters
29482955
----------
2949-
path : Union[pathlib.Path, str]
2956+
path : Union[str, pathlib.Path]
29502957
Path where the download should be saved.
29512958
"""
29522959
return mapping.from_maybe_impl(await self._impl_obj.saveAs(path=path))
@@ -3397,7 +3404,11 @@ async def evalOnSelectorAll(
33973404
)
33983405

33993406
async def addScriptTag(
3400-
self, url: str = None, path: str = None, content: str = None, type: str = None
3407+
self,
3408+
url: str = None,
3409+
path: typing.Union[str, pathlib.Path] = None,
3410+
content: str = None,
3411+
type: str = None,
34013412
) -> "ElementHandle":
34023413
"""Page.addScriptTag
34033414
@@ -3408,7 +3419,7 @@ async def addScriptTag(
34083419
----------
34093420
url : Optional[str]
34103421
URL of a script to be added.
3411-
path : Optional[str]
3422+
path : Union[str, pathlib.Path, NoneType]
34123423
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.
34133424
content : Optional[str]
34143425
Raw JavaScript content to be injected into frame.
@@ -3427,7 +3438,10 @@ async def addScriptTag(
34273438
)
34283439

34293440
async def addStyleTag(
3430-
self, url: str = None, path: str = None, content: str = None
3441+
self,
3442+
url: str = None,
3443+
path: typing.Union[str, pathlib.Path] = None,
3444+
content: str = None,
34313445
) -> "ElementHandle":
34323446
"""Page.addStyleTag
34333447
@@ -3438,7 +3452,7 @@ async def addStyleTag(
34383452
----------
34393453
url : Optional[str]
34403454
URL of the `<link>` tag.
3441-
path : Optional[str]
3455+
path : Union[str, pathlib.Path, NoneType]
34423456
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.
34433457
content : Optional[str]
34443458
Raw CSS content to be injected into frame.
@@ -3890,7 +3904,9 @@ async def bringToFront(self) -> NoneType:
38903904
"""
38913905
return mapping.from_maybe_impl(await self._impl_obj.bringToFront())
38923906

3893-
async def addInitScript(self, source: str = None, path: str = None) -> NoneType:
3907+
async def addInitScript(
3908+
self, source: str = None, path: typing.Union[str, pathlib.Path] = None
3909+
) -> NoneType:
38943910
"""Page.addInitScript
38953911
38963912
Adds a script which would be evaluated in one of the following scenarios:
@@ -3966,7 +3982,7 @@ async def screenshot(
39663982
self,
39673983
timeout: int = None,
39683984
type: Literal["png", "jpeg"] = None,
3969-
path: str = None,
3985+
path: typing.Union[str, pathlib.Path] = None,
39703986
quality: int = None,
39713987
omitBackground: bool = None,
39723988
fullPage: bool = None,
@@ -3982,7 +3998,7 @@ async def screenshot(
39823998
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.
39833999
type : Optional[Literal['png', 'jpeg']]
39844000
Specify screenshot type, defaults to `png`.
3985-
path : Optional[str]
4001+
path : Union[str, pathlib.Path, NoneType]
39864002
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.
39874003
quality : Optional[int]
39884004
The quality of the image, between 0-100. Not applicable to `png` images.
@@ -4618,7 +4634,7 @@ async def pdf(
46184634
height: typing.Union[str, float] = None,
46194635
preferCSSPageSize: bool = None,
46204636
margin: PdfMargins = None,
4621-
path: str = None,
4637+
path: typing.Union[str, pathlib.Path] = None,
46224638
) -> bytes:
46234639
"""Page.pdf
46244640
@@ -4692,7 +4708,7 @@ async def pdf(
46924708
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.
46934709
margin : Optional[{"top": Union[str, int, NoneType], "right": Union[str, int, NoneType], "bottom": Union[str, int, NoneType], "left": Union[str, int, NoneType]}]
46944710
Paper margins, defaults to none.
4695-
path : Optional[str]
4711+
path : Union[str, pathlib.Path, NoneType]
46964712
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.
46974713
46984714
Returns
@@ -5016,7 +5032,9 @@ async def setOffline(self, offline: bool) -> NoneType:
50165032
"""
50175033
return mapping.from_maybe_impl(await self._impl_obj.setOffline(offline=offline))
50185034

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

55695587
async def launch(
55705588
self,
5571-
executablePath: str = None,
5589+
executablePath: typing.Union[str, pathlib.Path] = None,
55725590
args: typing.List[str] = None,
55735591
ignoreDefaultArgs: typing.Union[bool, typing.List[str]] = None,
55745592
handleSIGINT: bool = None,
@@ -5579,7 +5597,7 @@ async def launch(
55795597
headless: bool = None,
55805598
devtools: bool = None,
55815599
proxy: ProxyServer = None,
5582-
downloadsPath: str = None,
5600+
downloadsPath: typing.Union[str, pathlib.Path] = None,
55835601
slowMo: int = None,
55845602
chromiumSandbox: bool = None,
55855603
) -> "Browser":
@@ -5594,7 +5612,7 @@ async def launch(
55945612
55955613
Parameters
55965614
----------
5597-
executablePath : Optional[str]
5615+
executablePath : Union[str, pathlib.Path, NoneType]
55985616
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.
55995617
args : Optional[List[str]]
56005618
Additional arguments to pass to the browser instance. The list of Chromium flags can be found here.
@@ -5616,7 +5634,7 @@ async def launch(
56165634
**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`.
56175635
proxy : Optional[{"server": str, "bypass": Optional[str], "username": Optional[str], "password": Optional[str]}]
56185636
Network proxy settings.
5619-
downloadsPath : Optional[str]
5637+
downloadsPath : Union[str, pathlib.Path, NoneType]
56205638
If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed.
56215639
slowMo : Optional[int]
56225640
Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
@@ -5649,7 +5667,7 @@ async def launch(
56495667

56505668
async def launchServer(
56515669
self,
5652-
executablePath: str = None,
5670+
executablePath: typing.Union[str, pathlib.Path] = None,
56535671
args: typing.List[str] = None,
56545672
ignoreDefaultArgs: typing.Union[bool, typing.List[str]] = None,
56555673
handleSIGINT: bool = None,
@@ -5660,7 +5678,7 @@ async def launchServer(
56605678
headless: bool = None,
56615679
devtools: bool = None,
56625680
proxy: ProxyServer = None,
5663-
downloadsPath: str = None,
5681+
downloadsPath: typing.Union[str, pathlib.Path] = None,
56645682
port: int = None,
56655683
chromiumSandbox: bool = None,
56665684
) -> "BrowserServer":
@@ -5670,7 +5688,7 @@ async def launchServer(
56705688
56715689
Parameters
56725690
----------
5673-
executablePath : Optional[str]
5691+
executablePath : Union[str, pathlib.Path, NoneType]
56745692
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.
56755693
args : Optional[List[str]]
56765694
Additional arguments to pass to the browser instance. The list of Chromium flags can be found here.
@@ -5692,7 +5710,7 @@ async def launchServer(
56925710
**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`.
56935711
proxy : Optional[{"server": str, "bypass": Optional[str], "username": Optional[str], "password": Optional[str]}]
56945712
Network proxy settings.
5695-
downloadsPath : Optional[str]
5713+
downloadsPath : Union[str, pathlib.Path, NoneType]
56965714
If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed.
56975715
port : Optional[int]
56985716
Port to use for the web socket. Defaults to 0 that picks any available port.
@@ -5725,8 +5743,8 @@ async def launchServer(
57255743

57265744
async def launchPersistentContext(
57275745
self,
5728-
userDataDir: str,
5729-
executablePath: str = None,
5746+
userDataDir: typing.Union[str, pathlib.Path],
5747+
executablePath: typing.Union[str, pathlib.Path] = None,
57305748
args: typing.List[str] = None,
57315749
ignoreDefaultArgs: typing.Union[bool, typing.List[str]] = None,
57325750
handleSIGINT: bool = None,
@@ -5737,7 +5755,7 @@ async def launchPersistentContext(
57375755
headless: bool = None,
57385756
devtools: bool = None,
57395757
proxy: ProxyServer = None,
5740-
downloadsPath: str = None,
5758+
downloadsPath: typing.Union[str, pathlib.Path] = None,
57415759
slowMo: int = None,
57425760
viewport: IntSize = None,
57435761
ignoreHTTPSErrors: bool = None,
@@ -5764,9 +5782,9 @@ async def launchPersistentContext(
57645782
57655783
Parameters
57665784
----------
5767-
userDataDir : str
5785+
userDataDir : Union[str, pathlib.Path]
57685786
Path to a User Data Directory, which stores browser session data like cookies and local storage. More details for Chromium and Firefox.
5769-
executablePath : Optional[str]
5787+
executablePath : Union[str, pathlib.Path, NoneType]
57705788
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.
57715789
args : Optional[List[str]]
57725790
Additional arguments to pass to the browser instance. The list of Chromium flags can be found here.
@@ -5788,7 +5806,7 @@ async def launchPersistentContext(
57885806
**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`.
57895807
proxy : Optional[{"server": str, "bypass": Optional[str], "username": Optional[str], "password": Optional[str]}]
57905808
Network proxy settings.
5791-
downloadsPath : Optional[str]
5809+
downloadsPath : Union[str, pathlib.Path, NoneType]
57925810
If specified, accepted downloads are downloaded into this folder. Otherwise, temporary folder is created and is deleted when browser is closed.
57935811
slowMo : Optional[int]
57945812
Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on. Defaults to 0.

playwright/browser_context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import asyncio
16+
from pathlib import Path
1617
from types import SimpleNamespace
1718
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union
1819

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

142-
async def addInitScript(self, source: str = None, path: str = None) -> None:
143+
async def addInitScript(
144+
self, source: str = None, path: Union[str, Path] = None
145+
) -> None:
143146
if path:
144147
with open(path, "r") as file:
145148
source = file.read()

playwright/browser_type.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def executablePath(self) -> str:
4848

4949
async def launch(
5050
self,
51-
executablePath: str = None,
51+
executablePath: Union[str, Path] = None,
5252
args: List[str] = None,
5353
ignoreDefaultArgs: Union[bool, List[str]] = None,
5454
handleSIGINT: bool = None,
@@ -59,7 +59,7 @@ async def launch(
5959
headless: bool = None,
6060
devtools: bool = None,
6161
proxy: ProxyServer = None,
62-
downloadsPath: str = None,
62+
downloadsPath: Union[str, Path] = None,
6363
slowMo: int = None,
6464
chromiumSandbox: bool = None,
6565
) -> Browser:
@@ -74,7 +74,7 @@ async def launch(
7474

7575
async def launchServer(
7676
self,
77-
executablePath: str = None,
77+
executablePath: Union[str, Path] = None,
7878
args: List[str] = None,
7979
ignoreDefaultArgs: Union[bool, List[str]] = None,
8080
handleSIGINT: bool = None,
@@ -85,7 +85,7 @@ async def launchServer(
8585
headless: bool = None,
8686
devtools: bool = None,
8787
proxy: ProxyServer = None,
88-
downloadsPath: str = None,
88+
downloadsPath: Union[str, Path] = None,
8989
port: int = None,
9090
chromiumSandbox: bool = None,
9191
) -> BrowserServer:
@@ -100,8 +100,8 @@ async def launchServer(
100100

101101
async def launchPersistentContext(
102102
self,
103-
userDataDir: str,
104-
executablePath: str = None,
103+
userDataDir: Union[str, Path],
104+
executablePath: Union[str, Path] = None,
105105
args: List[str] = None,
106106
ignoreDefaultArgs: Union[bool, List[str]] = None,
107107
handleSIGINT: bool = None,
@@ -112,7 +112,7 @@ async def launchPersistentContext(
112112
headless: bool = None,
113113
devtools: bool = None,
114114
proxy: ProxyServer = None,
115-
downloadsPath: str = None,
115+
downloadsPath: Union[str, Path] = None,
116116
slowMo: int = None,
117117
viewport: IntSize = None,
118118
ignoreHTTPSErrors: bool = None,
@@ -163,3 +163,7 @@ def normalize_launch_params(params: Dict) -> None:
163163
params["ignoreAllDefaultArgs"] = True
164164
del params["ignoreDefaultArgs"]
165165
params["env"] = {name: str(value) for [name, value] in params["env"].items()}
166+
if "executablePath" in params:
167+
params["executablePath"] = str(Path(params["executablePath"]))
168+
if "downloadsPath" in params:
169+
params["downloadsPath"] = str(Path(params["downloadsPath"]))

playwright/download.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ async def failure(self) -> Optional[str]:
4141
async def path(self) -> Optional[str]:
4242
return await self._channel.send("path")
4343

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy