Skip to content

Commit 5c1cc71

Browse files
committed
Update html2print.py
1 parent 343b7f2 commit 5c1cc71

3 files changed

Lines changed: 110 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: 35 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,15 @@
1516

1617
import browsers
1718
import requests
18-
from requests import Response, RequestException
19+
from my_linux_browsers import browsers as bb
20+
from requests import RequestException, Response
1921
from selenium import webdriver
2022
from selenium.webdriver.chrome.options import Options
2123
from selenium.webdriver.chrome.service import Service
2224
from webdriver_manager.core.os_manager import ChromeType, OperationSystemManager
2325

26+
logging.getLogger().setLevel(logging.DEBUG)
27+
2428
__version__ = "0.0.15"
2529

2630
PATH_TO_HTML2PDF_JS = os.path.join(
@@ -41,7 +45,24 @@
4145

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

4667
# If Web Driver Manager cannot detect Chrome, it returns None.
4768
if chrome_version is None:
@@ -55,6 +76,9 @@ def get_chrome_driver(self, path_to_cache_dir: str):
5576
print( # noqa: T201
5677
f"html2print: Installed Chrome version: {chrome_version}"
5778
)
79+
print( # noqa: T201
80+
f"html2print: Installed Chrome path: {chrome_path}"
81+
)
5882

5983
system_map = {
6084
"Windows": "win32",
@@ -181,8 +205,9 @@ def send_http_get_request(url, params=None, **kwargs) -> Response:
181205
f"html2print: "
182206
f"failed to get response for URL: {url} with error: {last_error}"
183207
)
184-
raise RequestException(f"GET request failed after 3 attempts: {url}") from last_error
185-
208+
raise RequestException(
209+
f"html2print: GET request failed after 3 attempts: {url}"
210+
) from last_error
186211

187212
@staticmethod
188213
def get_chrome_version():
@@ -329,7 +354,12 @@ def create_webdriver(
329354
else:
330355
service = Service(path_to_chrome)
331356

357+
path_to_chrome_browser = ""
358+
path_to_chrome_browser = browsers.get("chrome")["path"]
359+
print(f"html2print: Chrome available at path: {path_to_chrome_browser}") # noqa: T201
360+
332361
webdriver_options = Options()
362+
webdriver_options.binary_location = path_to_chrome_browser
333363
webdriver_options.add_argument("start-maximized")
334364
webdriver_options.add_argument("disable-infobars")
335365
# Doesn't seem to be needed.
@@ -462,7 +492,7 @@ def main():
462492
if browsers.get("chrome") is not None:
463493
path_to_chrome_browser = browsers.get("chrome")["path"]
464494
print( # noqa: T201
465-
f"html2print: Chrome available at path:{path_to_chrome_browser}"
495+
f"html2print: Chrome available at path: {path_to_chrome_browser}"
466496
)
467497
sys.exit(0)
468498

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 browsers 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)