Skip to content

Commit 73ed188

Browse files
authored
chore: use black as a formatter for Python (microsoft#32)
1 parent 1519d95 commit 73ed188

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3821
-2937
lines changed

.github/workflows/ci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ jobs:
3939
python -m pip install --upgrade pip
4040
pip install -r local-requirements.txt
4141
pip install .
42+
- name: Lint
43+
run: black --check .
4244
- name: Build driver
4345
run: python build_driver.py
4446
env:

build_driver.py

+22-19
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,30 @@
1717
import shutil
1818
import subprocess
1919

20-
driver_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'driver')
21-
package_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'playwright')
22-
drivers_path = os.path.join(package_path, 'drivers')
20+
driver_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "driver")
21+
package_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "playwright")
22+
drivers_path = os.path.join(package_path, "drivers")
2323

24-
if os.path.exists(os.path.join(driver_path, 'package-lock.json')):
25-
os.remove(os.path.join(driver_path, 'package-lock.json'))
26-
if os.path.exists(os.path.join(driver_path, 'node_modules')):
27-
shutil.rmtree(os.path.join(driver_path, 'node_modules'))
28-
if os.path.exists(os.path.join(driver_path, 'out')):
29-
shutil.rmtree(os.path.join(driver_path, 'out'))
24+
if os.path.exists(os.path.join(driver_path, "package-lock.json")):
25+
os.remove(os.path.join(driver_path, "package-lock.json"))
26+
if os.path.exists(os.path.join(driver_path, "node_modules")):
27+
shutil.rmtree(os.path.join(driver_path, "node_modules"))
28+
if os.path.exists(os.path.join(driver_path, "out")):
29+
shutil.rmtree(os.path.join(driver_path, "out"))
3030

31-
subprocess.run('npm i', cwd=driver_path, shell=True)
32-
subprocess.run('npm run bake', cwd=driver_path, shell=True)
31+
subprocess.run("npm i", cwd=driver_path, shell=True)
32+
subprocess.run("npm run bake", cwd=driver_path, shell=True)
3333

34-
for driver in ['driver-linux', 'driver-macos', 'driver-win.exe']:
35-
if os.path.exists(os.path.join(package_path, driver)):
36-
os.remove(os.path.join(package_path, driver))
34+
for driver in ["driver-linux", "driver-macos", "driver-win.exe"]:
35+
if os.path.exists(os.path.join(package_path, driver)):
36+
os.remove(os.path.join(package_path, driver))
3737

38-
in_path = os.path.join(driver_path, 'out', driver)
39-
out_path = os.path.join(drivers_path, driver + '.gz')
40-
with open(in_path, 'rb') as f_in, gzip.open(out_path, 'wb') as f_out:
41-
shutil.copyfileobj(f_in, f_out)
38+
in_path = os.path.join(driver_path, "out", driver)
39+
out_path = os.path.join(drivers_path, driver + ".gz")
40+
with open(in_path, "rb") as f_in, gzip.open(out_path, "wb") as f_out:
41+
shutil.copyfileobj(f_in, f_out)
4242

43-
shutil.copyfile(os.path.join(driver_path, 'node_modules', 'playwright', 'browsers.json'), os.path.join(drivers_path, 'browsers.json'))
43+
shutil.copyfile(
44+
os.path.join(driver_path, "node_modules", "playwright", "browsers.json"),
45+
os.path.join(drivers_path, "browsers.json"),
46+
)

build_package.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
import subprocess
1818

1919
folder = os.path.dirname(os.path.abspath(__file__))
20-
if os.path.exists(os.path.join(folder, 'build')):
21-
shutil.rmtree(os.path.join(folder, 'build'))
22-
if os.path.exists(os.path.join(folder, 'dist')):
23-
shutil.rmtree(os.path.join(folder, 'dist'))
24-
if os.path.exists(os.path.join(folder, 'playwright.egg-info')):
25-
shutil.rmtree(os.path.join(folder, 'playwright.egg-info'))
20+
if os.path.exists(os.path.join(folder, "build")):
21+
shutil.rmtree(os.path.join(folder, "build"))
22+
if os.path.exists(os.path.join(folder, "dist")):
23+
shutil.rmtree(os.path.join(folder, "dist"))
24+
if os.path.exists(os.path.join(folder, "playwright.egg-info")):
25+
shutil.rmtree(os.path.join(folder, "playwright.egg-info"))
2626

27-
subprocess.run('python setup.py sdist bdist_wheel', shell=True)
27+
subprocess.run("python setup.py sdist bdist_wheel", shell=True)

client.py

+48-38
Original file line numberDiff line numberDiff line change
@@ -16,57 +16,67 @@
1616

1717
from playwright import browser_types
1818

19+
1920
async def run():
20-
print('Launching browser...')
21-
browser = await browser_types['webkit'].launch(headless=False)
22-
print('Contexts in browser: %d' % len(browser.contexts))
23-
print('Creating context...')
21+
print("Launching browser...")
22+
browser = await browser_types["webkit"].launch(headless=False)
23+
print("Contexts in browser: %d" % len(browser.contexts))
24+
print("Creating context...")
2425
context = await browser.newContext(viewport=None)
25-
print('Contexts in browser: %d' % len(browser.contexts))
26-
print('Pages in context: %d' % len(context.pages))
26+
print("Contexts in browser: %d" % len(browser.contexts))
27+
print("Pages in context: %d" % len(context.pages))
2728

28-
print('\nCreating page1...')
29+
print("\nCreating page1...")
2930
page1 = await context.newPage()
30-
print('Pages in context: %d' % len(context.pages))
31-
page1.on('framenavigated', lambda frame: print('Frame navigated to %s' % frame.url))
32-
page1.on('request', lambda request: print('Request %s' % request.url))
33-
page1.on('requestFinished', lambda request: print('Request finished %s' % request.url))
34-
page1.on('response', lambda response: print('Response %s, request %s in frame %s' % (response.url, response.request.url, response.frame.url)))
35-
print('Navigating page1 to https://example.com...')
36-
await page1.goto('https://example.com')
37-
print('Page1 main frame url: %s' % page1.mainFrame.url)
38-
print('Page1 tile: %s' % await page1.title())
39-
print('Frames in page1: %d' % len(page1.frames))
40-
await page1.screenshot(path='example.png')
31+
print("Pages in context: %d" % len(context.pages))
32+
page1.on("framenavigated", lambda frame: print("Frame navigated to %s" % frame.url))
33+
page1.on("request", lambda request: print("Request %s" % request.url))
34+
page1.on(
35+
"requestFinished", lambda request: print("Request finished %s" % request.url)
36+
)
37+
page1.on(
38+
"response",
39+
lambda response: print(
40+
"Response %s, request %s in frame %s"
41+
% (response.url, response.request.url, response.frame.url)
42+
),
43+
)
44+
print("Navigating page1 to https://example.com...")
45+
await page1.goto("https://example.com")
46+
print("Page1 main frame url: %s" % page1.mainFrame.url)
47+
print("Page1 tile: %s" % await page1.title())
48+
print("Frames in page1: %d" % len(page1.frames))
49+
await page1.screenshot(path="example.png")
4150

42-
print('\nCreating page2...')
51+
print("\nCreating page2...")
4352
page2 = await context.newPage()
44-
page2.on('framenavigated', lambda frame: print('Frame navigated to %s' % frame.url))
53+
page2.on("framenavigated", lambda frame: print("Frame navigated to %s" % frame.url))
4554

46-
print('Navigating page2 to https://webkit.org...')
47-
await page2.goto('https://webkit.org')
48-
print('Page2 tile: %s' % await page2.title())
49-
print('Pages in context: %d' % len(context.pages))
55+
print("Navigating page2 to https://webkit.org...")
56+
await page2.goto("https://webkit.org")
57+
print("Page2 tile: %s" % await page2.title())
58+
print("Pages in context: %d" % len(context.pages))
5059

51-
print('\nQuerying body...')
52-
body1 = await page1.querySelector('body')
53-
print('Body text %s' % await body1.textContent())
60+
print("\nQuerying body...")
61+
body1 = await page1.querySelector("body")
62+
print("Body text %s" % await body1.textContent())
5463

55-
print('Closing page1...')
64+
print("Closing page1...")
5665
await page1.close()
57-
print('Pages in context: %d' % len(context.pages))
66+
print("Pages in context: %d" % len(context.pages))
5867

59-
print('Navigating page2 to https://cnn.com...')
60-
await page2.goto('https://cnn.com')
61-
print('Page2 main frame url: %s' % page2.mainFrame.url)
62-
print('Page2 tile: %s' % await page2.title())
63-
print('Frames in page2: %d' % len(page2.frames))
64-
print('Pages in context: %d' % len(context.pages))
68+
print("Navigating page2 to https://cnn.com...")
69+
await page2.goto("https://cnn.com")
70+
print("Page2 main frame url: %s" % page2.mainFrame.url)
71+
print("Page2 tile: %s" % await page2.title())
72+
print("Frames in page2: %d" % len(page2.frames))
73+
print("Pages in context: %d" % len(context.pages))
6574

66-
print('Closing context...')
75+
print("Closing context...")
6776
await context.close()
68-
print('Contexts in browser: %d' % len(browser.contexts))
69-
print('Closing browser')
77+
print("Contexts in browser: %d" % len(browser.contexts))
78+
print("Closing browser")
7079
await browser.close()
7180

81+
7282
asyncio.get_event_loop().run_until_complete(run())

docs/development.md

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
# Development
22

3-
Build driver:
3+
## Build driver:
4+
45
```sh
56
python ./build_driver.py
67
```
78

8-
Run tests:
9+
## Run tests:
10+
911
```sh
1012
pytest
1113
```
1214

13-
Run tests with coverage:
15+
## Run tests with coverage:
16+
1417
```sh
1518
pytest --cov=playwright --cov-report html
1619
open htmlcov/index.html
1720
```
1821

19-
Deploy:
22+
## Deploy:
23+
2024
```sh
2125
python ./build_package.py
2226
... check
2327
python ./upload_package.py
2428
```
2529

26-
Checking for typing errors
30+
## Checking for typing errors
31+
2732
```sh
2833
mypy playwright
2934
```
35+
36+
## Format the code
37+
38+
```sh
39+
black .
40+
```

local-requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ mypy==0.782
77
setuptools==49.1.0
88
twisted==20.3.0
99
wheel==0.34.2
10+
black==19.10b0

playwright/__init__.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,4 @@
2222
browser_types = playwright_object.browser_types
2323
TimeoutError = helper.TimeoutError
2424

25-
__all__ = [
26-
'browser_types',
27-
'chromium',
28-
'firefox',
29-
'webkit',
30-
'devices',
31-
'TimeoutError'
32-
]
25+
__all__ = ["browser_types", "chromium", "firefox", "webkit", "devices", "TimeoutError"]

playwright/accessibility.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
from playwright.element_handle import ElementHandle
1818
from typing import Dict
1919

20-
class Accessibility:
2120

22-
def __init__(self, channel: Channel) -> None:
23-
self._channel = channel
21+
class Accessibility:
22+
def __init__(self, channel: Channel) -> None:
23+
self._channel = channel
2424

25-
async def snapshot(self,
26-
interestingOnly: bool = None,
27-
root: ElementHandle = None
25+
async def snapshot(
26+
self, interestingOnly: bool = None, root: ElementHandle = None
2827
) -> Dict:
29-
root = root._channel if root else None
30-
return await self._channel.send('snapshot', dict(root=root, interestingOnly=interestingOnly))
28+
root = root._channel if root else None
29+
return await self._channel.send(
30+
"snapshot", dict(root=root, interestingOnly=interestingOnly)
31+
)

0 commit comments

Comments
 (0)