Skip to content

Commit c824112

Browse files
authored
F-string formatting, and error standardization (#27)
* fix: f-string and raise ValueError when failing to get url * fix: exclude extra test code from package
1 parent 3e56709 commit c824112

File tree

15 files changed

+52
-62
lines changed

15 files changed

+52
-62
lines changed

.github/workflows/test-macOS-sur.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ jobs:
3636
3737
- name: Test with pytest
3838
run: |
39-
pytest pyderman/test.py
39+
pytest -vv

.github/workflows/test-macOS.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ jobs:
3636
3737
- name: Test with pytest
3838
run: |
39-
pytest pyderman/test.py
39+
pytest -vv

.github/workflows/test-ubuntu.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ jobs:
3636
3737
- name: Test with pytest
3838
run: |
39-
pytest pyderman/test.py
39+
pytest -vv

.github/workflows/test-windows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ jobs:
3636
3737
- name: Test with pytest
3838
run: |
39-
pytest pyderman/test.py
39+
pytest -vv

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ repos:
5353
rev: 'typos-v0.9.0'
5454
hooks:
5555
- id: typos
56+
- repo: https://github.com/igorshubovych/markdownlint-cli
57+
rev: v0.31.1
58+
hooks:
59+
- id: markdownlint
60+
args: [--disable=MD013]

README.md

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,9 @@
11
# Pyderman (Selenium Web Driver Installer)
22

3-
[![Ubuntu](
4-
https://github.com/shadowmoose/pyderman/workflows/Ubuntu/badge.svg
5-
)](
6-
https://github.com/shadowmoose/pyderman/actions?query=workflow%3AUbuntu
7-
) [![MacOS](
8-
https://github.com/shadowmoose/pyderman/workflows/MacOS/badge.svg
9-
)](
10-
https://github.com/shadowmoose/pyderman/actions?query=workflow%3AMacOS
11-
) [![Windows](
12-
https://github.com/shadowmoose/pyderman/workflows/Windows/badge.svg
13-
)](
14-
https://github.com/shadowmoose/pyderman/actions?query=workflow%3AWindows
15-
) [![MacOS (SUR)](
16-
https://github.com/shadowmoose/pyderman/actions/workflows/test-macOS-sur.yml/badge.svg
17-
)](
18-
https://github.com/shadowmoose/pyderman/actions/workflows/test-macOS-sur.yml
19-
)
3+
[![Ubuntu](https://github.com/shadowmoose/pyderman/workflows/Ubuntu/badge.svg)](https://github.com/shadowmoose/pyderman/actions?query=workflow%3AUbuntu) [![MacOS](https://github.com/shadowmoose/pyderman/workflows/MacOS/badge.svg)](https://github.com/shadowmoose/pyderman/actions?query=workflow%3AMacOS) [![Windows](https://github.com/shadowmoose/pyderman/workflows/Windows/badge.svg)](https://github.com/shadowmoose/pyderman/actions?query=workflow%3AWindows) [![MacOS (SUR)](https://github.com/shadowmoose/pyderman/actions/workflows/test-macOS-sur.yml/badge.svg)](https://github.com/shadowmoose/pyderman/actions/workflows/test-macOS-sur.yml)
204

215
This is a fast, simple, dependency-free package that can automatically find & download any version of
22-
the *Google Chrome (chromeDriver), Firefox (geckoDriver), PhantomJS, Opera (operaDriver), and Edge (edgeDriver)* web drivers.
6+
the _Google Chrome (chromeDriver), Firefox (geckoDriver), PhantomJS, Opera (operaDriver), and Edge (edgeDriver)_ web drivers.
237

248
This project was built to allow developers to seamlessly include selenium support on the user-side, without requiring any manual configuration on their part. It will automatically locate the correct driver binary for the platform & version you choose, as well as setting the os-specific permissions after downloading.
259

@@ -40,22 +24,22 @@ After installed, call it in your code like so:
4024
```python
4125
import pyderman as driver
4226
path = driver.install(browser=driver.firefox)
43-
print('Installed geckodriver driver to path: %s' % path)
27+
print(f"Installed geckodriver driver to path: {path}")
4428
```
4529

4630
There are options for the output directory, disabling printout, running chmod on the downloaded executable,
4731
automatic overwriting, executable file name, and version number.
4832
All parameters are optional, and the default values are listed below.
4933

50-
This example downloads the Chrome Driver instead, by changing ```browser``` like so:
34+
This example downloads the Chrome Driver instead, by changing `browser` like so:
5135

5236
```python
5337
import pyderman as dr
5438
path = dr.install(browser=dr.chrome, file_directory='./lib/', verbose=True, chmod=True, overwrite=False, version=None, filename=None, return_info=False)
55-
print('Installed chromedriver to path: %s' % path)
39+
print(f"Installed chromedriver to path: {path}")
5640
```
5741

58-
The download is very fast, and will skip downloading if the file already exists. This behavior can be toggled with ```overwrite```.
42+
The download is very fast, and will skip downloading if the file already exists. This behavior can be toggled with `overwrite`.
5943

6044
## Notes
6145

@@ -73,4 +57,4 @@ Apple broke a ton of compatibility moving to Big Sur, and now many of the web dr
7357

7458
## Why's it called 'Pyderman'?
7559

76-
Because it installs *web*-drivers. [Get it?](https://youtu.be/4o29VoxtsFk)
60+
Because it installs _web_-drivers. [Get it?](https://youtu.be/4o29VoxtsFk)

pyderman/__init__.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def install(
6060
:return: The absolute path of the downloaded driver, or None if something failed.
6161
"""
6262
if not _current_os:
63-
raise Exception("Cannot determine OS version! [%s]" % platform.system())
63+
raise Exception(f"Cannot determine OS version! [{platform.system()}]")
6464
if not version:
6565
version = "latest"
6666
if not browser:
@@ -75,26 +75,23 @@ def install(
7575
exts = [e for e in [".zip", ".tar.gz", ".tar.bz2"] if url.endswith(e)]
7676
if len(exts) != 1:
7777
raise Exception(
78-
"Unable to locate file extension in URL: %s (%s)"
79-
% (url, ",".join(exts))
78+
f"Unable to locate file extension in URL: {url} ({','.join(exts)})"
8079
)
8180
archive = exts[0]
8281

83-
archive_path = join(
84-
abspath(file_directory), "{}_{}{}".format(driver, ver, archive)
85-
)
86-
file_path = join(abspath(file_directory), "{}_{}{}".format(driver, ver, _ext))
82+
archive_path = join(abspath(file_directory), f"{driver}_{ver}{archive}")
83+
file_path = join(abspath(file_directory), f"{driver}_{ver}{_ext}")
8784
if filename:
8885
file_path = join(abspath(file_directory), filename)
8986

9087
if not overwrite and isfile(file_path):
9188
if verbose:
92-
print("%s is already installed." % driver)
89+
print(f"{driver} is already installed.")
9390
return file_path
9491

9592
if not _download(url, archive_path, verbose):
9693
if verbose:
97-
print("Download for %s version failed; Trying alternates." % _os_bit)
94+
print(f"Download for {_os_bit} version failed; Trying alternates.")
9895
continue
9996

10097
out = _extract(archive_path, driver_path, file_path)
@@ -128,7 +125,7 @@ def _extract(path: str, driver_pattern: str, out_file: str) -> str | None:
128125
out_file = abspath(out_file)
129126
if not isfile(path):
130127
return None
131-
tmp_path = join(dirname(out_file), "tmp_dl_dir_%s" % basename(path))
128+
tmp_path = join(dirname(out_file), f"tmp_dl_dir_{basename(path)}")
132129
zip_ref: zipfile.ZipFile | tarfile.TarFile | None = None
133130
namelist: list[str] = []
134131
if path.endswith(".zip"):

pyderman/drivers/chrome.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pyderman.util import downloader
66

77
_base_version = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE"
8-
_base_download = "https://chromedriver.storage.googleapis.com/%s/chromedriver_%s%s.zip"
8+
_base_download = "https://chromedriver.storage.googleapis.com/{version}/chromedriver_{os}{os_bit}.zip"
99

1010

1111
def get_url(
@@ -15,20 +15,20 @@ def get_url(
1515
if version == "latest":
1616
resolved_version = downloader.raw(_base_version)
1717
elif match:
18-
major, minor, patch, build = match.groups()
18+
major, minor, patch, _build = match.groups()
1919
if patch:
20-
patch_version = "{}.{}.{}".format(major, minor, patch)
20+
patch_version = f"{major}.{minor}.{patch}"
2121
else:
2222
patch_version = major
23-
resolved_version = downloader.raw("{}_{}".format(_base_version, patch_version))
23+
resolved_version = downloader.raw(f"{_base_version}_{patch_version}")
2424
else:
2525
resolved_version = version
2626
if not resolved_version:
27-
raise Exception("Unable to locate ChromeDriver version: {}!".format(version))
27+
raise ValueError(f"Unable to locate ChromeDriver version! [{version}]")
2828
if _os == "mac-m1":
2929
_os = "mac" # chromedriver_mac64_m1
3030
_os_bit = "%s_m1" % _os_bit
31-
download = _base_download % (resolved_version, _os, _os_bit)
31+
download = _base_download.format(version=resolved_version, os=_os, os_bit=_os_bit)
3232
return "chromedriver", download, resolved_version
3333

3434

pyderman/drivers/edge.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
_stable = "https://msedgedriver.azureedge.net/LATEST_STABLE"
88
_base_version = "https://msedgedriver.azureedge.net/LATEST_RELEASE"
9-
_base_download = "https://msedgedriver.azureedge.net/{}/edgedriver_{}{}.zip"
9+
_base_download = (
10+
"https://msedgedriver.azureedge.net/{version}/edgedriver_{os}{os_bit}.zip"
11+
)
1012

1113

1214
def get_url(
@@ -20,7 +22,7 @@ def get_url(
2022
elif _os == "mac":
2123
_os_name = "MACOS"
2224
else:
23-
raise OSError("There is no valid EdgeDriver release for {}".format(_os))
25+
raise OSError(f"There is no valid EdgeDriver release for {_os}")
2426

2527
if version in ("latest", "stable"):
2628
resolved_version = downloader.raw(_stable, "utf-16")
@@ -29,17 +31,17 @@ def get_url(
2931
if version == "latest":
3032
match = re.match(r"^(\d*)[.]?(\d*)[.]?(\d*)[.]?(\d*)$", resolved_version)
3133
if match:
32-
major, minor, patch, build = match.groups()
33-
_url = "{}_{}_{}".format(_base_version, major, _os_name)
34+
major, _minor, _patch, _build = match.groups()
35+
_url = f"{_base_version}_{major}_{_os_name}"
3436
resolved_version = downloader.raw(_url, "utf-16")
3537
resolved_version = resolved_version.strip() if resolved_version else ""
3638
else:
3739
resolved_version = version
3840

39-
url = _base_download.format(resolved_version, _os, _os_bit)
41+
url = _base_download.format(version=resolved_version, os=_os, os_bit=_os_bit)
4042

4143
if not resolved_version:
42-
raise Exception("Unable to locate EdgeDriver version: {}!".format(version))
44+
raise ValueError(f"Unable to locate EdgeDriver version! [{version}]")
4345
return "msedgedriver", url, resolved_version
4446

4547

pyderman/drivers/firefox.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ def get_url(
1010
) -> tuple[str, str, str]:
1111
urls = github.find_links("mozilla", "geckodriver", version)
1212
for u in urls:
13-
target = "{}{}.".format(_os, _os_bit) if _os != "mac" else "macos."
13+
target = f"{_os}{_os_bit}." if _os != "mac" else "macos."
1414
if _os == "mac-m1":
1515
target = "macos-aarch64."
1616
if target in u:
1717
ver = re.search(r"v(\d{1,2}\.\d{1,2}\.\d{1,2})", u)
1818
if ver is not None:
1919
return "geckodriver", u, str(ver.group(1))
20-
raise ValueError("Unable to get url")
20+
raise ValueError(f"Unable to locate FirefoxDriver version! [{version}]")
2121

2222

2323
if __name__ == "__main__":

pyderman/drivers/opera.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ def get_url(
1212
"operasoftware", "operachromiumdriver", version, prefix="v."
1313
)
1414
for u in urls:
15-
if "{}{}".format(_os, _os_bit) in u:
15+
if f"{_os}{_os_bit}" in u:
1616
ver = re.search(r"v\.(\d{1,2}\.\d{1,2})", u)
1717
if ver is not None:
1818
return "operadriver.*/operadriver", u, str(ver.group(1))
19-
raise ValueError("Unable to get url")
19+
raise ValueError(f"Unable to locate OperaDriver version! [{version}]")
2020

2121

2222
if __name__ == "__main__":

pyderman/drivers/phantomjs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ def get_url(
3030
release["links"]["self"]["href"],
3131
str(ver.group(1)),
3232
)
33-
raise ValueError("Unable to get url")
33+
raise ValueError(f"Unable to locate PhantomJSDriver version! [{version}]")
3434

3535

3636
def _releases() -> Generator[dict[str, Any], None, None]:
3737
page = "https://api.bitbucket.org/2.0/repositories/ariya/phantomjs/downloads/"
3838
while page:
3939
s = downloader.raw(page)
4040
if s is None:
41-
raise ValueError("Unable to get page: %s" % page)
41+
raise ValueError(f"Unable to get page: {page}")
4242
else:
4343
data = json.loads(s)
4444
for release in data["values"]:

pyderman/util/github.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ def find_links(
1212
version = "latest"
1313
if version != "latest" and not version.startswith(prefix):
1414
version = "{}{}".format(prefix, version)
15-
repo = "https://github.com/{}/{}/releases/{}".format(author, project, version)
15+
repo = f"https://github.com/{author}/{project}/releases/{version}"
1616
html = downloader.raw(repo)
1717
if html is None:
18-
raise Exception("Unable to download {} version: {}".format(project, version))
18+
raise Exception(f"Unable to download {project} version: {version}")
1919
return [
2020
"https://github.com%s" % str(u)
2121
for u in re.findall(r"\"(.+?/download.+?)\"", html)

setup.cfg

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ classifiers =
2222
[options]
2323
packages = find:
2424
python_requires = >=3.7
25-
packages_data =
26-
pyderman = py.typed
25+
include_package_data = True
2726

2827
[options.extras_require]
2928
dev =
@@ -37,6 +36,9 @@ dev =
3736
types-setuptools
3837
wheel
3938

39+
[options.package_data]
40+
pyderman = py.typed
41+
4042
[isort]
4143
profile = black
4244

pyderman/test.py renamed to test_pyderman.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def test_get_nonsense(self) -> None:
112112
with self.assertRaises(Exception) as exc:
113113
self.chrome_version("25.25.25.25")
114114
self.assertEqual(
115-
str(exc.exception), "Unable to locate ChromeDriver version: 25.25.25.25!"
115+
str(exc.exception), "Unable to locate ChromeDriver version! [25.25.25.25]"
116116
)
117117
return
118118

@@ -231,9 +231,9 @@ def test_get_invalid_os(self) -> None:
231231

232232
def test_unresolved_version(self) -> None:
233233
with self.assertRaises(Exception) as exc:
234-
edge.get_url(None, _os="mac", _os_bit="64") # type: ignore
234+
edge.get_url(None, _os="mac", _os_bit="64") # type: ignore[arg-type]
235235
self.assertEqual(
236-
str(exc.exception), "Unable to locate EdgeDriver version: None!"
236+
str(exc.exception), "Unable to locate EdgeDriver version! [None]"
237237
)
238238
return
239239

0 commit comments

Comments
 (0)