Skip to content
This repository was archived by the owner on Jul 26, 2024. It is now read-only.

Commit eab98f9

Browse files
authored
Merge pull request #25 from python-paling/refactoring-and-typing
Refactoring and typing
2 parents 4cc85b7 + b0e6735 commit eab98f9

4 files changed

Lines changed: 100 additions & 5 deletions

File tree

paling/browsers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ def _build_urls(start_pages: Iterable[Union[str, Dict[str, str]]], options: Opti
4747

4848

4949
def open(start_pages: Iterable[Union[str, Dict[str, str]]], options: OptionsDictT) -> None:
50+
"""
51+
Open a browser and navigate to the specified start pages.
52+
53+
Args:
54+
start_pages (Iterable[Union[str, Dict[str, str]]]): A collection of start pages to navigate to.
55+
Each start page can be either a URL string or a dictionary with 'url' and 'params' keys.
56+
options (OptionsDictT): A dictionary of options for configuring the browser.
57+
58+
Raises:
59+
TypeError: If the 'mode' option is not a string, boolean, or None.
60+
TypeError: If the 'cmdline_args' option is not a list of strings.
61+
EnvironmentError: If the specified browser installation cannot be found.
62+
63+
Returns:
64+
None
65+
"""
5066
# Build full URLs for starting pages (including host and port)
5167
start_urls = _build_urls(start_pages, options)
5268

paling/chrome.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@
88
name: str = 'Google Chrome/Chromium'
99

1010
def run(path: str, options: OptionsDictT, start_urls: List[str]) -> None:
11+
"""
12+
Run the Chrome browser with the specified path, options, and start URLs.
13+
14+
Args:
15+
path (str): The path to the Chrome browser executable.
16+
options (OptionsDictT): A dictionary containing the options for running Chrome.
17+
start_urls (List[str]): A list of URLs to open in Chrome.
18+
19+
Raises:
20+
TypeError: If the 'cmdline_args' option is not of type List[str].
21+
22+
Returns:
23+
None
24+
"""
1125
if not isinstance(options['cmdline_args'], list):
1226
raise TypeError("'cmdline_args' option must be of type List[str]")
1327
if options['app_mode']:
@@ -22,6 +36,12 @@ def run(path: str, options: OptionsDictT, start_urls: List[str]) -> None:
2236

2337

2438
def find_path() -> Optional[str]:
39+
"""
40+
Finds the path of the Chrome executable based on the current operating system.
41+
42+
Returns:
43+
Optional[str]: The path of the Chrome executable if found, otherwise None.
44+
"""
2545
if sys.platform in ['win32', 'win64']:
2646
return _find_chrome_win()
2747
elif sys.platform == 'darwin':
@@ -33,6 +53,12 @@ def find_path() -> Optional[str]:
3353

3454

3555
def _find_chrome_mac() -> Optional[str]:
56+
"""
57+
Find the path of Google Chrome executable on macOS.
58+
59+
Returns:
60+
Optional[str]: The path of Google Chrome executable if found, otherwise None.
61+
"""
3662
default_dir = r'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
3763
if os.path.exists(default_dir):
3864
return default_dir
@@ -45,6 +71,12 @@ def _find_chrome_mac() -> Optional[str]:
4571

4672

4773
def _find_chromium_mac() -> Optional[str]:
74+
"""
75+
Find the Chromium executable path on macOS.
76+
77+
Returns:
78+
Optional[str]: The path to the Chromium executable if found, None otherwise.
79+
"""
4880
default_dir = r'/Applications/Chromium.app/Contents/MacOS/Chromium'
4981
if os.path.exists(default_dir):
5082
return default_dir
@@ -57,6 +89,12 @@ def _find_chromium_mac() -> Optional[str]:
5789

5890

5991
def _find_chrome_linux() -> Optional[str]:
92+
"""
93+
Finds the path of the Chrome executable on a Linux system.
94+
95+
Returns:
96+
Optional[str]: The path of the Chrome executable if found, None otherwise.
97+
"""
6098
import whichcraft as wch
6199
chrome_names = ['chromium-browser',
62100
'chromium',
@@ -71,6 +109,12 @@ def _find_chrome_linux() -> Optional[str]:
71109

72110

73111
def _find_chrome_win() -> Optional[str]:
112+
"""
113+
Find the path of the Chrome executable on Windows.
114+
115+
Returns:
116+
Optional[str]: The path of the Chrome executable if found, None otherwise.
117+
"""
74118
import winreg as reg
75119
reg_path = r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe'
76120
chrome_path: Optional[str] = None

paling/edge.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import sys, subprocess as sps, os
2+
from typing import Optional
23

34
name = 'Edge'
45

5-
def run(path, options, start_urls):
6+
def run(path: str, options: dict, start_urls: list) -> None:
7+
"""
8+
Run the specified web browser with the given options and start URLs.
9+
10+
Args:
11+
path (str): The path to the web browser executable.
12+
options (dict): A dictionary of options for the web browser.
13+
start_urls (list): A list of URLs to open in the web browser.
14+
15+
Returns:
16+
None
17+
"""
618
if path != 'edge_legacy':
719
if options['app_mode']:
820
for url in start_urls:
@@ -16,13 +28,19 @@ def run(path, options, start_urls):
1628
cmd = 'start microsoft-edge:{}'.format(start_urls[0])
1729
sps.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=sps.PIPE, shell=True)
1830

19-
def find_path():
31+
def find_path() -> Optional[str]:
2032
if sys.platform in ['win32', 'win64']:
2133
return _find_edge_win()
2234
else:
2335
return None
2436

25-
def _find_edge_win():
37+
def _find_edge_win() -> str:
38+
"""
39+
Finds the path of the Microsoft Edge browser executable on Windows.
40+
41+
Returns:
42+
str: The path of the Microsoft Edge browser executable.
43+
"""
2644
import winreg as reg
2745
reg_path = r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe'
2846

@@ -33,7 +51,7 @@ def _find_edge_win():
3351
reg_key.Close()
3452
if not os.path.isfile(edge_path):
3553
continue
36-
except WindowsError:
54+
except FileNotFoundError:
3755
edge_path = 'edge_legacy'
3856
else:
3957
break

paling/electron.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
name: str = 'Electron'
1111

1212
def run(path: str, options: OptionsDictT, start_urls: List[str]) -> None:
13+
"""
14+
Run the specified Electron application.
15+
16+
Args:
17+
path (str): The path to the Electron application executable.
18+
options (OptionsDictT): A dictionary containing options for the Electron application.
19+
start_urls (List[str]): A list of URLs to open in the Electron application.
20+
21+
Raises:
22+
TypeError: If the 'cmdline_args' option is not of type List[str].
23+
"""
1324
if not isinstance(options['cmdline_args'], list):
1425
raise TypeError("'cmdline_args' option must be of type List[str]")
1526
cmd = [path] + options['cmdline_args']
@@ -18,12 +29,18 @@ def run(path: str, options: OptionsDictT, start_urls: List[str]) -> None:
1829

1930

2031
def find_path() -> Optional[str]:
32+
"""
33+
Finds the path to the Electron executable.
34+
35+
Returns:
36+
Optional[str]: The path to the Electron executable if found, otherwise None.
37+
"""
2138
if sys.platform in ['win32', 'win64']:
2239
# It doesn't work well passing the .bat file to Popen, so we get the actual .exe
2340
bat_path = wch.which('electron')
2441
return os.path.join(bat_path, r'..\node_modules\electron\dist\electron.exe')
2542
elif sys.platform in ['darwin', 'linux']:
26-
# This should work find...
43+
# This should work fine...
2744
return wch.which('electron') # type: ignore # whichcraft doesn't currently have type hints
2845
else:
2946
return None

0 commit comments

Comments
 (0)