Skip to content

feat: accept pathlib in the API #110

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
Jul 30, 2020
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
5 changes: 3 additions & 2 deletions build_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
import re
import shutil
import subprocess
from pathlib import Path

_dirname = Path(os.path.dirname(os.path.abspath(__file__)))
from playwright.path_utils import get_file_dirname

_dirname = get_file_dirname()

driver_path = _dirname / "driver"
package_path = _dirname / "playwright"
Expand Down
20 changes: 12 additions & 8 deletions build_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import shutil
import subprocess

folder = os.path.dirname(os.path.abspath(__file__))
if os.path.exists(os.path.join(folder, "build")):
shutil.rmtree(os.path.join(folder, "build"))
if os.path.exists(os.path.join(folder, "dist")):
shutil.rmtree(os.path.join(folder, "dist"))
if os.path.exists(os.path.join(folder, "playwright.egg-info")):
shutil.rmtree(os.path.join(folder, "playwright.egg-info"))
from playwright.path_utils import get_file_dirname

_dirname = get_file_dirname()
_build_dir = _dirname / "build"
if _build_dir.exists():
shutil.rmtree(_build_dir)
_dist_dir = _dirname / "dist"
if _dist_dir.exists():
shutil.rmtree(_dist_dir)
_egg_dir = _dirname / "playwright.egg-info"
if _egg_dir.exists():
shutil.rmtree(_egg_dir)

subprocess.run("python setup.py sdist bdist_wheel", shell=True)
15 changes: 13 additions & 2 deletions playwright/async_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.


import pathlib
import sys
import typing

Expand Down Expand Up @@ -870,7 +871,12 @@ async def selectText(self, timeout: int = None) -> NoneType:
async def setInputFiles(
self,
files: typing.Union[
str, FilePayload, typing.List[str], typing.List[FilePayload]
str,
pathlib.Path,
FilePayload,
typing.List[str],
typing.List[pathlib.Path],
typing.List[FilePayload],
],
timeout: int = None,
noWaitAfter: bool = None,
Expand Down Expand Up @@ -1877,7 +1883,12 @@ async def setInputFiles(
self,
selector: str,
files: typing.Union[
str, FilePayload, typing.List[str], typing.List[FilePayload]
str,
pathlib.Path,
FilePayload,
typing.List[str],
typing.List[pathlib.Path],
typing.List[FilePayload],
],
timeout: int = None,
noWaitAfter: bool = None,
Expand Down
9 changes: 5 additions & 4 deletions playwright/element_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import mimetypes
import os
import sys
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union, cast

from playwright.connection import ChannelOwner, from_nullable_channel
Expand Down Expand Up @@ -123,7 +124,7 @@ async def selectText(self, timeout: int = None) -> None:

async def setInputFiles(
self,
files: Union[str, FilePayload, List[str], List[FilePayload]],
files: Union[str, Path, FilePayload, List[str], List[Path], List[FilePayload]],
timeout: int = None,
noWaitAfter: bool = None,
) -> None:
Expand Down Expand Up @@ -242,16 +243,16 @@ def convert_select_option_values(arg: ValuesToSelect) -> Any:


def normalize_file_payloads(
files: Union[str, FilePayload, List[str], List[FilePayload]]
files: Union[str, Path, FilePayload, List[str], List[Path], List[FilePayload]]
) -> List[FilePayload]:
file_list = files if isinstance(files, list) else [files]
file_payloads: List[FilePayload] = []
for item in file_list:
if isinstance(item, str):
if isinstance(item, str) or isinstance(item, Path):
with open(item, mode="rb") as fd:
file: FilePayload = {
"name": os.path.basename(item),
"mimeType": mimetypes.guess_type(item)[0]
"mimeType": mimetypes.guess_type(str(Path(item)))[0]
or "application/octet-stream",
"buffer": base64.b64encode(fd.read()).decode(),
}
Expand Down
9 changes: 6 additions & 3 deletions playwright/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import asyncio
import sys
from pathlib import Path
from typing import TYPE_CHECKING, Any, Awaitable, Dict, List, Optional, Set, Union, cast

from pyee import BaseEventEmitter
Expand Down Expand Up @@ -280,7 +281,7 @@ async def addScriptTag(
params = locals_to_params(locals())
if path:
with open(path, "r") as file:
params["content"] = file.read() + "\n//# sourceURL=" + path
params["content"] = file.read() + "\n//# sourceURL=" + str(Path(path))
del params["path"]
return from_channel(await self._channel.send("addScriptTag", params))

Expand All @@ -290,7 +291,9 @@ async def addStyleTag(
params = locals_to_params(locals())
if path:
with open(path, "r") as file:
params["content"] = file.read() + "\n/*# sourceURL=" + path + "*/"
params["content"] = (
file.read() + "\n/*# sourceURL=" + str(Path(path)) + "*/"
)
del params["path"]
return from_channel(await self._channel.send("addStyleTag", params))

Expand Down Expand Up @@ -366,7 +369,7 @@ async def selectOption(
async def setInputFiles(
self,
selector: str,
files: Union[str, FilePayload, List[str], List[FilePayload]],
files: Union[str, Path, FilePayload, List[str], List[Path], List[FilePayload]],
timeout: int = None,
noWaitAfter: bool = None,
) -> None:
Expand Down
19 changes: 10 additions & 9 deletions playwright/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from playwright.connection import Connection
from playwright.helper import Error, not_installed_error
from playwright.object_factory import create_remote_object
from playwright.path_utils import get_file_dirname
from playwright.playwright import Playwright
from playwright.sync_api import Playwright as SyncPlaywright
from playwright.sync_base import dispatcher_fiber, set_dispatcher_fiber
Expand All @@ -44,20 +45,20 @@ def compute_driver_name() -> str:


async def run_driver_async() -> Connection:
package_path = os.path.dirname(os.path.abspath(__file__))
package_path = get_file_dirname()
driver_name = compute_driver_name()
driver_executable = os.path.join(package_path, driver_name)
archive_name = os.path.join(package_path, "drivers", driver_name + ".gz")
driver_executable = package_path / driver_name
archive_name = package_path / "drivers" / (driver_name + ".gz")

if not os.path.exists(driver_executable) or os.path.getmtime(
if not driver_executable.exists() or os.path.getmtime(
driver_executable
) < os.path.getmtime(archive_name):
raise not_installed_error(
"Playwright requires additional post-installation step to be made."
)

proc = await asyncio.create_subprocess_exec(
driver_executable,
str(driver_executable),
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
Expand Down Expand Up @@ -124,12 +125,12 @@ def main() -> None:
if "install" not in sys.argv:
print('Run "python -m playwright install" to complete installation')
return
package_path = os.path.dirname(os.path.abspath(__file__))
package_path = get_file_dirname()
driver_name = compute_driver_name()
driver_executable = os.path.join(package_path, driver_name)
archive_name = os.path.join(package_path, "drivers", driver_name + ".gz")
driver_executable = package_path / driver_name
archive_name = package_path / "drivers" / (driver_name + ".gz")

if not os.path.exists(driver_executable) or os.path.getmtime(
if not driver_executable.exists() or os.path.getmtime(
driver_executable
) < os.path.getmtime(archive_name):
print(f"Extracting {archive_name} into {driver_executable}...")
Expand Down
10 changes: 10 additions & 0 deletions playwright/path_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import inspect
from pathlib import Path


def get_file_dirname() -> Path:
"""Returns the callee (`__file__`) directory name"""
frame = inspect.stack()[1]
module = inspect.getmodule(frame[0])
assert module
return Path(module.__file__).parent.absolute()
15 changes: 13 additions & 2 deletions playwright/sync_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.


import pathlib
import sys
import typing

Expand Down Expand Up @@ -906,7 +907,12 @@ def selectText(self, timeout: int = None) -> NoneType:
def setInputFiles(
self,
files: typing.Union[
str, FilePayload, typing.List[str], typing.List[FilePayload]
str,
pathlib.Path,
FilePayload,
typing.List[str],
typing.List[pathlib.Path],
typing.List[FilePayload],
],
timeout: int = None,
noWaitAfter: bool = None,
Expand Down Expand Up @@ -1968,7 +1974,12 @@ def setInputFiles(
self,
selector: str,
files: typing.Union[
str, FilePayload, typing.List[str], typing.List[FilePayload]
str,
pathlib.Path,
FilePayload,
typing.List[str],
typing.List[pathlib.Path],
typing.List[FilePayload],
],
timeout: int = None,
noWaitAfter: bool = None,
Expand Down
6 changes: 3 additions & 3 deletions scripts/documentation_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re
import sys
from pathlib import Path
from typing import Any, Dict, List, cast

_dirname = Path(os.path.dirname(os.path.abspath(__file__)))
from playwright.path_utils import get_file_dirname

_dirname = get_file_dirname()


class DocumentationProvider:
Expand Down
1 change: 1 addition & 0 deletions scripts/generate_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def return_value(value: Any) -> List[str]:

import typing
import sys
import pathlib

if sys.version_info >= (3, 8): # pragma: no cover
from typing import Literal
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def skip_by_browser(request, browser_name):
)

if browser_name in skip_browsers_names:
pytest.skip("skipped for this browser: {}".format(browser_name))
pytest.skip(f"skipped for this browser: {browser_name}")


@pytest.fixture(autouse=True)
Expand All @@ -196,7 +196,7 @@ def skip_by_platform(request):
)

if sys.platform in skip_platform_names:
pytest.skip("skipped on this platform: {}".format(sys.platform))
pytest.skip(f"skipped on this platform: {sys.platform}")


def pytest_addoption(parser):
Expand Down
8 changes: 4 additions & 4 deletions tests/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
import asyncio
import gzip
import mimetypes
import os
import socket
import threading
from contextlib import closing
from http import HTTPStatus
from pathlib import Path

from OpenSSL import crypto
from twisted.internet import reactor, ssl
from twisted.web import http

_dirname = Path(os.path.join(os.path.dirname(__file__)))
from playwright.path_utils import get_file_dirname

_dirname = get_file_dirname()


def _find_free_port():
Expand Down Expand Up @@ -107,7 +107,7 @@ def process(self):
file_content = None
try:
file_content = open(
os.path.join(static_path, request.path.decode()[1:]), "rb"
static_path / request.path.decode()[1:], "rb"
).read()
except (FileNotFoundError, IsADirectoryError):
request.setResponseCode(HTTPStatus.NOT_FOUND)
Expand Down
17 changes: 5 additions & 12 deletions tests/test_add_init_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from playwright import Error
from playwright.path_utils import get_file_dirname

_dirname = get_file_dirname()


async def test_add_init_script_evaluate_before_anything_else_on_the_page(page):
Expand All @@ -24,11 +25,7 @@ async def test_add_init_script_evaluate_before_anything_else_on_the_page(page):


async def test_add_init_script_work_with_a_path(page):
await page.addInitScript(
path=os.path.join(
os.path.dirname(os.path.abspath(__file__)), "assets/injectedfile.js"
)
)
await page.addInitScript(path=_dirname / "assets/injectedfile.js")
await page.goto("data:text/html,<script>window.result = window.injected</script>")
assert await page.evaluate("window.result") == 123

Expand Down Expand Up @@ -59,11 +56,7 @@ async def test_add_init_script_work_with_browser_context_scripts(page, context):
async def test_add_init_script_work_with_browser_context_scripts_with_a_path(
page, context
):
await context.addInitScript(
path=os.path.join(
os.path.dirname(os.path.abspath(__file__)), "assets/injectedfile.js"
)
)
await context.addInitScript(path=_dirname / "assets/injectedfile.js")
page = await context.newPage()
await page.goto("data:text/html,<script>window.result = window.injected</script>")
assert await page.evaluate("window.result") == 123
Expand Down
Loading