Skip to content

Commit

Permalink
Add function about side car media dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Oct 22, 2024
1 parent 6e5d706 commit 7d513da
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
44 changes: 43 additions & 1 deletion lizmap/test/test_toolbelt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Test toolbelt."""

import tempfile
import unittest

from pathlib import Path

from lizmap.toolbelt.lizmap import sidecar_media_dirs
from lizmap.toolbelt.strings import human_size

__copyright__ = 'Copyright 2024, 3Liz'
Expand All @@ -17,3 +20,42 @@ def test_human_size(self):
self.assertEqual("53 KB", human_size("54512"))
self.assertEqual("14 KB", human_size(15145))
self.assertEqual("14 KB", human_size("15145"))

def test_lizmap_sidecar_dirs(self):
""" Test to detect side-car files related to JavaScript or theme. """
with tempfile.TemporaryDirectory() as tmp_dir_name:
test = Path(tmp_dir_name) / "project.qgs"
test.touch()

side_1 = Path(tmp_dir_name) / "media" / "js" / "default" / "default_included.js"
side_1.parent.mkdir(parents=True)
side_1.touch()

side_2 = Path(tmp_dir_name) / "media" / "js" / "project" / "project_included.js"
side_2.parent.mkdir(parents=True)
side_2.touch()

side_3 = Path(tmp_dir_name) / "media" / "js" / "excluded" / "excluded.js"
side_3.parent.mkdir(parents=True)
side_3.touch()

side_4 = Path(tmp_dir_name) / "media" / "theme" / "default" / "css" / "default_included.css"
side_4.parent.mkdir(parents=True)
side_4.touch()

side_5 = Path(tmp_dir_name) / "media" / "theme" / "excluded" / "css" / "excluded.css"
side_5.parent.mkdir(parents=True)
side_5.touch()

side_6 = Path(tmp_dir_name) / "media" / "theme" / "project" / "css" / "project_included.css"
side_6.parent.mkdir(parents=True)
side_6.touch()

expected = [
Path(tmp_dir_name) / "media" / "js" / "default",
Path(tmp_dir_name) / "media" / "js" / "project",
Path(tmp_dir_name) / "media" / "theme" / "default",
Path(tmp_dir_name) / "media" / "theme" / "project",
]
expected.sort()
self.assertListEqual(expected, sidecar_media_dirs(test))
29 changes: 29 additions & 0 deletions lizmap/toolbelt/lizmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import re

from pathlib import Path
from typing import List, Tuple

from qgis.core import QgsVectorLayer
Expand Down Expand Up @@ -35,3 +36,31 @@ def convert_lizmap_popup(content: str, layer: QgsVectorLayer) -> Tuple[str, List
errors.append(variable[1])

return content, errors


def sidecar_media_dirs(file_path: Path) -> List[Path]:
""" Look for all side-car dirs in "media" directory.
Like a Lizmap theme or a JavaScript.
"""
media_root = file_path.parent.joinpath("media")
if not media_root.exists():
return []

results = []
for one_media in ("js", "theme"):
folder = media_root.joinpath(one_media)
if not folder.exists():
continue

default_media = folder.joinpath("default")
if default_media.exists():
results.append(default_media)

project_media = folder.joinpath(file_path.stem)
if project_media.exists():
results.append(project_media)
continue

results.sort()
return results

0 comments on commit 7d513da

Please sign in to comment.