Skip to content

Commit 732712c

Browse files
akash5100nabobalis
andauthored
Save binary response with save_file function (#72)
* Save file function * overwrite option for already existing function * test with tmp_path * update typehint for filename and tests * Apply suggestions from code review Co-authored-by: Nabil Freij <[email protected]> * Fix mypy error * test for string input Co-authored-by: Nabil Freij <[email protected]>
1 parent a56ebfe commit 732712c

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

hvpy/tests/test_utils.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
from datetime import datetime
2+
13
import pytest
24

3-
from hvpy import DataSource, EventType
5+
from hvpy import DataSource, EventType, takeScreenshot
46
from hvpy.utils import (
57
_create_events_string,
68
_create_layer_string,
79
_to_datasource,
810
_to_event_type,
911
create_events,
1012
create_layers,
13+
save_file,
1114
)
1215

1316

@@ -65,3 +68,27 @@ def test_create_events():
6568
def test_create_events_string():
6669
assert _create_events_string(EventType.ACTIVE_REGION) == "[AR,all,1]"
6770
assert _create_events_string(EventType.ACTIVE_REGION, "xyz") == "[AR,xyz,1]"
71+
72+
73+
def test_save_file(tmp_path):
74+
f1 = tmp_path / "test.png"
75+
res = takeScreenshot(
76+
date=datetime.today(),
77+
imageScale=2.44,
78+
layers="[10,1,100]",
79+
x0=0,
80+
y0=0,
81+
width=1920,
82+
height=1200,
83+
display=True,
84+
)
85+
save_file(res, f1, overwrite=False)
86+
assert f1.exists()
87+
with pytest.raises(ValueError, match="already exists"):
88+
save_file(res, f1, overwrite=False)
89+
save_file(res, f1, overwrite=True)
90+
assert f1.exists()
91+
92+
f2 = tmp_path / "test2.png"
93+
save_file(res, str(f2), overwrite=False)
94+
assert f2.exists()

hvpy/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Any, List, Union, Callable
2+
from pathlib import Path
23
from datetime import datetime
34

45
from hvpy.datasource import DataSource
@@ -9,6 +10,7 @@
910
"convert_date_to_unix",
1011
"create_layers",
1112
"create_events",
13+
"save_file",
1214
]
1315

1416

@@ -127,3 +129,24 @@ def create_events(events: List[Union[EventType, str, tuple]]) -> str:
127129
raise ValueError(f"{event} is not a EventType or str or two-length tuple")
128130
# Strips the final comma
129131
return constructed_events[:-1]
132+
133+
134+
def save_file(data: bytearray, filename: Union[Path, str], overwrite: bool = False) -> None:
135+
"""
136+
Saves a file to the specified path.
137+
138+
Parameters
139+
----------
140+
data
141+
The data to save.
142+
filename
143+
The path to save the file to.
144+
overwrite
145+
Whether to overwrite the file if it already exists.
146+
Default is `False`.
147+
"""
148+
if isinstance(filename, str):
149+
filename = Path(filename)
150+
if filename.exists() and not overwrite:
151+
raise ValueError(f"{filename} already exists. Use overwrite=True to overwrite.")
152+
filename.write_bytes(data)

0 commit comments

Comments
 (0)