Skip to content

Commit 912ce87

Browse files
author
Stefan Kuethe
committed
Convert icon to base64 automatically
1 parent 3174b0d commit 912ce87

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

src/openlayers/styles.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from __future__ import annotations
22

3-
from pydantic import BaseModel, ConfigDict, Field
3+
import os
4+
5+
from pathlib import Path
6+
from pydantic import BaseModel, ConfigDict, Field, field_validator
7+
8+
from .utils import create_icon_src_from_file
49

510

611
def fix_keys(d: dict) -> dict:
@@ -29,10 +34,18 @@ class FlatStyle(BaseModel):
2934
circle_stroke_width: float | int | list | None = None
3035
circle_stroke_color: str | list | None = None
3136

32-
icon_src: str | list | None = None
37+
icon_src: str | Path | list | None = None
38+
icon_scale: float | int | None = None
3339
icon_color: str | list | None = None
3440
icon_opacity: float | int | None = Field(None, gt=0, le=1)
3541

42+
@field_validator("icon_src")
43+
def validate_icon_src(cls, v) -> str:
44+
if os.path.isfile(v):
45+
return create_icon_src_from_file(v)
46+
47+
return v
48+
3649
def model_dump(self) -> dict:
3750
return fix_keys(super().model_dump(exclude_none=True))
3851

@@ -54,7 +67,7 @@ def default_style(**kwargs) -> FlatStyle:
5467
).model_copy(update=kwargs)
5568

5669

57-
class CircleStyle(FlatStyle): ...
70+
# class CircleStyle(FlatStyle): ...
5871

5972

6073
class IconStyle(FlatStyle):
@@ -64,4 +77,4 @@ class IconStyle(FlatStyle):
6477
icon_scale: float | int | None = None
6578

6679

67-
class FillStyle(FlatStyle): ...
80+
# class FillStyle(FlatStyle): ...

tests/data/icon.png

2.47 KB
Loading

tests/test_styles.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pathlib import Path
2+
13
from openlayers.styles import default_style, FlatStyle
24

35
def test_default_vector_style() -> None:
@@ -8,9 +10,34 @@ def test_default_vector_style() -> None:
810

911
assert json_def["stroke-color"] == "#3399CC"
1012

11-
def test_updates() -> None:
12-
update = dict(fill_color="green")
13-
style = default_style().model_copy(update=update)
13+
def test_update_default_style() -> None:
14+
# update = dict(fill_color="green")
15+
# style = default_style().model_copy(update=update)
16+
17+
style = default_style(fill_color="green")
1418

1519
print(style.model_dump())
1620
print(style)
21+
22+
def test_icon_style() -> None:
23+
icon_url = "http://xyz.de/icon.png"
24+
25+
style = FlatStyle(
26+
icon_src=icon_url
27+
)
28+
29+
dumped_style = style.model_dump()
30+
31+
print(dumped_style)
32+
assert dumped_style["icon-src"] == icon_url
33+
34+
def test_icon_from_file() -> None:
35+
icon_path = Path(__file__).parent / "data" / "icon.png"
36+
print(icon_path)
37+
38+
style = FlatStyle(icon_src=icon_path)
39+
dumped_style = style.model_dump()
40+
41+
print(type(style.icon_src))
42+
43+
print(dumped_style)

0 commit comments

Comments
 (0)