|
3 | 3 | import uuid |
4 | 4 | from pathlib import Path |
5 | 5 |
|
6 | | -from qgis.PyQt.QtCore import QDir |
| 6 | +from qgis.PyQt.QtCore import QDir, QUrl |
| 7 | +from jinja2 import Environment, FileSystemLoader |
7 | 8 |
|
8 | 9 | from geocatbridge.utils import meta |
9 | 10 |
|
10 | | -#: GeoCat Bridge Python root directory |
11 | | -BRIDGE_ROOT_DIR = Path(__file__).parent.parent |
12 | | - |
13 | 11 | _DIR_NAME_WIDGETS = "ui" |
14 | 12 | _DIR_NAME_TRANSLATIONS = "i18n" |
15 | 13 | _DIR_NAME_ICONS = "icons" |
16 | 14 | _DIR_NAME_RESOURCES = "resources" |
17 | 15 | _DIR_NAME_DOCS = "docs" |
18 | 16 |
|
| 17 | +_ABOUT_SRCDIR = "geocat" |
| 18 | +_ABOUT_TEMPLATE = "template.html" |
| 19 | +_ABOUT_HTMLPAGE = "index.html" |
| 20 | + |
| 21 | +#: GeoCat Bridge Python root directory |
| 22 | +BRIDGE_ROOT_DIR = Path(__file__).parent.parent |
| 23 | +BRIDGE_ABOUT_DIR = BRIDGE_ROOT_DIR / _DIR_NAME_RESOURCES / _ABOUT_SRCDIR |
| 24 | + |
19 | 25 |
|
20 | 26 | def _fix_ext(name, ext): |
21 | 27 | """ Appends a given extension to a file name if it doesn't have one. """ |
@@ -104,3 +110,33 @@ def getDirectory(path) -> str: |
104 | 110 | """ Returns the parent directory path to the given file or directory path. """ |
105 | 111 | fix_path = Path(str(path).split('|')[0]) # Fix for GeoPackage layer paths |
106 | 112 | return str(fix_path.resolve().parent) |
| 113 | + |
| 114 | + |
| 115 | +def getAboutUrl(refresh: bool = False) -> QUrl: |
| 116 | + """ |
| 117 | + Returns the QUrl for the Bridge About page (HTML). |
| 118 | + If 'refresh' is True or the HTML page does not exist, it is rendered from the template in the same folder. |
| 119 | + If the template does not exist, |
| 120 | + """ |
| 121 | + target_path = (BRIDGE_ABOUT_DIR / _ABOUT_HTMLPAGE).resolve() |
| 122 | + |
| 123 | + # Render the index.html if it does not exist yet or if it should be updated (i.e. when version changed) |
| 124 | + if refresh or not target_path.is_file(): |
| 125 | + template_path = target_path.with_name(_ABOUT_TEMPLATE) |
| 126 | + if not template_path.is_file(): |
| 127 | + raise FileNotFoundError(f"HTML template at {template_path} does not exist") |
| 128 | + env = Environment(loader=FileSystemLoader(template_path.parent)) |
| 129 | + template = env.get_template(_ABOUT_TEMPLATE) |
| 130 | + html = template.render( |
| 131 | + app_name=meta.getAppName(), |
| 132 | + short_name=meta.getShortAppName(), |
| 133 | + doc_url=meta.getDocsUrl(), |
| 134 | + repo_url=meta.getRepoUrl(), |
| 135 | + support_url=meta.getSupportUrl(), |
| 136 | + homepage=meta.getHomeUrl(), |
| 137 | + is_enterprise=meta.isEnterprise() |
| 138 | + ) |
| 139 | + with open(target_path, mode="w+", encoding="utf-8") as fp: |
| 140 | + fp.write(html) |
| 141 | + |
| 142 | + return QUrl.fromLocalFile(str(target_path)) |
0 commit comments