Skip to content

Commit 901983c

Browse files
committed
Update html2print.py
1 parent 343b7f2 commit 901983c

3 files changed

Lines changed: 111 additions & 6 deletions

File tree

.github/workflows/ci-linux-ubuntu-latest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
- name: Build HTML2PDF.js
4848
run: |
4949
invoke build
50-
50+
5151
- name: Run tests
5252
run: |
5353
invoke test

html2print/html2print.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import argparse
33
import atexit
44
import base64
5+
import logging
56
import os.path
67
import platform
78
import re
@@ -15,12 +16,14 @@
1516

1617
import browsers
1718
import requests
18-
from requests import Response, RequestException
19+
from requests import RequestException, Response
1920
from selenium import webdriver
2021
from selenium.webdriver.chrome.options import Options
2122
from selenium.webdriver.chrome.service import Service
2223
from webdriver_manager.core.os_manager import ChromeType, OperationSystemManager
2324

25+
logging.getLogger().setLevel(logging.DEBUG)
26+
2427
__version__ = "0.0.15"
2528

2629
PATH_TO_HTML2PDF_JS = os.path.join(
@@ -41,7 +44,26 @@
4144

4245
class ChromeDriverManager:
4346
def get_chrome_driver(self, path_to_cache_dir: str):
44-
chrome_version = self.get_chrome_version()
47+
for browser in browsers.browsers():
48+
print(f"html2print: browser {browser['browser_type']}:") # noqa: T201
49+
print(f" - path {browser['path']}") # noqa: T201
50+
print(f" - display_name {browser['display_name']}") # noqa: T201
51+
print(f" - version {browser['version']}:") # noqa: T201
52+
53+
if sys.platform == "linux":
54+
from .my_linux_browsers import browsers as bb
55+
56+
for browser in bb.browsers():
57+
print(f"my_linux_browsers: browser {browser['browser_type']}:") # noqa: T201
58+
print(f" - path {browser['path']}") # noqa: T201
59+
print( # noqa: T201
60+
f" - display_name {browser['display_name']}"
61+
)
62+
print(f" - version {browser['version']}:") # noqa: T201
63+
64+
chrome_browser_info = browsers.get("chrome")
65+
chrome_version = chrome_browser_info["version"]
66+
chrome_path = chrome_browser_info["path"]
4567

4668
# If Web Driver Manager cannot detect Chrome, it returns None.
4769
if chrome_version is None:
@@ -55,6 +77,9 @@ def get_chrome_driver(self, path_to_cache_dir: str):
5577
print( # noqa: T201
5678
f"html2print: Installed Chrome version: {chrome_version}"
5779
)
80+
print( # noqa: T201
81+
f"html2print: Installed Chrome path: {chrome_path}"
82+
)
5883

5984
system_map = {
6085
"Windows": "win32",
@@ -181,8 +206,9 @@ def send_http_get_request(url, params=None, **kwargs) -> Response:
181206
f"html2print: "
182207
f"failed to get response for URL: {url} with error: {last_error}"
183208
)
184-
raise RequestException(f"GET request failed after 3 attempts: {url}") from last_error
185-
209+
raise RequestException(
210+
f"html2print: GET request failed after 3 attempts: {url}"
211+
) from last_error
186212

187213
@staticmethod
188214
def get_chrome_version():
@@ -329,7 +355,12 @@ def create_webdriver(
329355
else:
330356
service = Service(path_to_chrome)
331357

358+
path_to_chrome_browser = ""
359+
path_to_chrome_browser = browsers.get("chrome")["path"]
360+
print(f"html2print: Chrome available at path: {path_to_chrome_browser}") # noqa: T201
361+
332362
webdriver_options = Options()
363+
webdriver_options.binary_location = path_to_chrome_browser
333364
webdriver_options.add_argument("start-maximized")
334365
webdriver_options.add_argument("disable-infobars")
335366
# Doesn't seem to be needed.
@@ -462,7 +493,7 @@ def main():
462493
if browsers.get("chrome") is not None:
463494
path_to_chrome_browser = browsers.get("chrome")["path"]
464495
print( # noqa: T201
465-
f"html2print: Chrome available at path:{path_to_chrome_browser}"
496+
f"html2print: Chrome available at path: {path_to_chrome_browser}"
466497
)
467498
sys.exit(0)
468499

html2print/my_linux_browsers.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import os
2+
import re
3+
import subprocess
4+
import sys
5+
from typing import Iterator
6+
7+
from .common import Browser
8+
9+
LINUX_DESKTOP_ENTRY_LIST = (
10+
# desktop entry name can be "firefox.desktop" or "firefox_firefox.desktop"
11+
("chrome", ("google-chrome",)),
12+
("chromium", ("chromium", "chromium_chromium")),
13+
("firefox", ("firefox", "firefox_firefox")),
14+
("msedge", ("microsoft-edge",)),
15+
("opera", ("opera_opera",)),
16+
("opera-beta", ("opera-beta_opera-beta",)),
17+
("opera-developer", ("opera-developer_opera-developer",)),
18+
("brave", ("brave-browser", "brave_brave")),
19+
("brave-beta", ("brave-browser-beta",)),
20+
("brave-nightly", ("brave-browser-nightly",)),
21+
)
22+
23+
# $XDG_DATA_HOME and $XDG_DATA_DIRS are not always set
24+
XDG_DATA_LOCATIONS = (
25+
"~/.local/share/applications",
26+
"/usr/share/applications",
27+
"/var/lib/snapd/desktop/applications",
28+
)
29+
30+
VERSION_PATTERN = re.compile(
31+
r"\b(\S+\.\S+)\b"
32+
) # simple pattern assuming all version strings have at least one dot with numbers around on them
33+
34+
35+
def browsers() -> Iterator[Browser]:
36+
yielded = False
37+
38+
if sys.platform == "linux":
39+
from xdg.DesktopEntry import DesktopEntry
40+
41+
for browser, desktop_entries in LINUX_DESKTOP_ENTRY_LIST:
42+
for application_dir in XDG_DATA_LOCATIONS:
43+
for desktop_entry in desktop_entries:
44+
path = os.path.join(
45+
application_dir, f"{desktop_entry}.desktop"
46+
)
47+
48+
if not os.path.isfile(path):
49+
continue
50+
51+
entry = DesktopEntry(path)
52+
executable_path = entry.getExec()
53+
54+
if executable_path.lower().endswith(" %u"):
55+
executable_path = executable_path[:-3].strip()
56+
57+
version = subprocess.getoutput(
58+
f"{executable_path} --version 2>&1"
59+
).strip()
60+
print(f"browser: version was {version}") # noqa: T201
61+
if match := VERSION_PATTERN.search(version):
62+
version = match[0]
63+
64+
yield Browser(
65+
browser_type=browser,
66+
path=executable_path,
67+
display_name=entry.getName(),
68+
version=version,
69+
)
70+
yielded = True
71+
72+
if not yielded:
73+
yield from ()
74+
return

0 commit comments

Comments
 (0)