Skip to content

Commit c8d31f5

Browse files
authored
Generate layers string with create_layers function (#65)
1 parent a12f594 commit c8d31f5

File tree

5 files changed

+101
-20
lines changed

5 files changed

+101
-20
lines changed

docs/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ API Reference
1515

1616
.. automodapi:: hvpy.datasources
1717
:no-inheritance-diagram:
18+
19+
.. automodapi:: hvpy.utils

hvpy/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
from .config import set_api_url
33
from .version import __version__
44
from .datasources import *
5+
from .utils import create_layers

hvpy/facade.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from hvpy.core import execute_api_call
55
from hvpy.parameters import *
6-
from hvpy.utils import add_shared_docstring
6+
from hvpy.utils import _add_shared_docstring
77

88
__all__ = [
99
"getJP2Image",
@@ -25,7 +25,7 @@
2525
]
2626

2727

28-
@add_shared_docstring(getJP2ImageInputParameters)
28+
@_add_shared_docstring(getJP2ImageInputParameters)
2929
def getJP2Image(
3030
date: datetime,
3131
sourceId: int,
@@ -49,7 +49,7 @@ def getJP2Image(
4949
return execute_api_call(input_parameters=params)
5050

5151

52-
@add_shared_docstring(getJP2HeaderInputParameters)
52+
@_add_shared_docstring(getJP2HeaderInputParameters)
5353
def getJP2Header(
5454
id: int,
5555
callback: Optional[str] = None,
@@ -71,7 +71,7 @@ def getJP2Header(
7171
return execute_api_call(input_parameters=params)
7272

7373

74-
@add_shared_docstring(getJPXClosestToMidPointInputParameters)
74+
@_add_shared_docstring(getJPXClosestToMidPointInputParameters)
7575
def getJPXClosestToMidPoint(
7676
startTimes: List[datetime],
7777
endTimes: List[datetime],
@@ -111,7 +111,7 @@ def getJPXClosestToMidPoint(
111111
return execute_api_call(input_parameters=params)
112112

113113

114-
@add_shared_docstring(getJPXInputParameters)
114+
@_add_shared_docstring(getJPXInputParameters)
115115
def getJPX(
116116
startTime: List[datetime],
117117
endTime: List[datetime],
@@ -153,7 +153,7 @@ def getJPX(
153153
return execute_api_call(input_parameters=params)
154154

155155

156-
@add_shared_docstring(getStatusInputParameters)
156+
@_add_shared_docstring(getStatusInputParameters)
157157
def getStatus() -> Union[bytes, str, Dict[str, Any]]:
158158
"""
159159
Returns information about how far behind the latest available JPEG2000
@@ -172,7 +172,7 @@ def getStatus() -> Union[bytes, str, Dict[str, Any]]:
172172
return execute_api_call(input_parameters=params)
173173

174174

175-
@add_shared_docstring(getClosestImageInputParameters)
175+
@_add_shared_docstring(getClosestImageInputParameters)
176176
def getClosestImage(
177177
date: datetime,
178178
sourceId: int,
@@ -200,7 +200,7 @@ def getClosestImage(
200200
return execute_api_call(input_parameters=params)
201201

202202

203-
@add_shared_docstring(getDataSourcesInputParameters)
203+
@_add_shared_docstring(getDataSourcesInputParameters)
204204
def getDataSources(
205205
verbose: Optional[bool] = False,
206206
enable: Optional[str] = None,
@@ -226,7 +226,7 @@ def getDataSources(
226226
return execute_api_call(input_parameters=params)
227227

228228

229-
@add_shared_docstring(takeScreenshotInputParameters)
229+
@_add_shared_docstring(takeScreenshotInputParameters)
230230
def takeScreenshot(
231231
date: datetime,
232232
imageScale: float,
@@ -295,7 +295,7 @@ def takeScreenshot(
295295
return execute_api_call(input_parameters=params)
296296

297297

298-
@add_shared_docstring(downloadScreenshotInputParameters)
298+
@_add_shared_docstring(downloadScreenshotInputParameters)
299299
def downloadScreenshot(id: int) -> Union[bytes, str, Dict[str, Any]]:
300300
"""
301301
Download a custom screenshot that was generated using the
@@ -314,7 +314,7 @@ def downloadScreenshot(id: int) -> Union[bytes, str, Dict[str, Any]]:
314314
return execute_api_call(input_parameters=params)
315315

316316

317-
@add_shared_docstring(queueMovieInputParameters)
317+
@_add_shared_docstring(queueMovieInputParameters)
318318
def queueMovie(
319319
startTime: datetime,
320320
endTime: datetime,
@@ -398,7 +398,7 @@ def queueMovie(
398398
return execute_api_call(input_parameters=params)
399399

400400

401-
@add_shared_docstring(reQueueMovieInputParameters)
401+
@_add_shared_docstring(reQueueMovieInputParameters)
402402
def reQueueMovie(
403403
id: str,
404404
force: Optional[bool] = False,
@@ -422,7 +422,7 @@ def reQueueMovie(
422422
return execute_api_call(input_parameters=params)
423423

424424

425-
@add_shared_docstring(getMovieStatusInputParameters)
425+
@_add_shared_docstring(getMovieStatusInputParameters)
426426
def getMovieStatus(
427427
id: str,
428428
format: str,
@@ -452,7 +452,7 @@ def getMovieStatus(
452452
return execute_api_call(input_parameters=params)
453453

454454

455-
@add_shared_docstring(downloadMovieInputParameters)
455+
@_add_shared_docstring(downloadMovieInputParameters)
456456
def downloadMovie(
457457
id: str,
458458
format: str,
@@ -479,7 +479,7 @@ def downloadMovie(
479479
return execute_api_call(input_parameters=params)
480480

481481

482-
@add_shared_docstring(getNewsFeedInputParameters)
482+
@_add_shared_docstring(getNewsFeedInputParameters)
483483
def getNewsFeed(
484484
callback: Optional[str] = None,
485485
) -> Union[bytes, str, Dict[str, Any]]:
@@ -501,7 +501,7 @@ def getNewsFeed(
501501
return execute_api_call(input_parameters=params)
502502

503503

504-
@add_shared_docstring(shortenURLInputParameters)
504+
@_add_shared_docstring(shortenURLInputParameters)
505505
def shortenURL(
506506
queryString: str,
507507
callback: Optional[str] = None,
@@ -525,7 +525,7 @@ def shortenURL(
525525
return execute_api_call(input_parameters=params)
526526

527527

528-
@add_shared_docstring(getTileInputParameters)
528+
@_add_shared_docstring(getTileInputParameters)
529529
def getTile(
530530
id: int,
531531
x: int,

hvpy/tests/test_utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pytest
2+
3+
from hvpy import DataSources
4+
from hvpy.utils import _create_layer_string, _to_datasource, create_layers
5+
6+
7+
def test_create_layers():
8+
assert create_layers([(9, 100), (11, 50)]) == "[9,1,100],[11,1,50]"
9+
10+
with pytest.raises(ValueError, match="999 is not a valid DataSources"):
11+
create_layers([(9, 100), (999, 100)])
12+
13+
with pytest.raises(ValueError, match="must be between 0 and 100"):
14+
create_layers([(9, 101), (14, 100)])
15+
16+
with pytest.raises(ValueError, match="must be between 0 and 100"):
17+
create_layers([(9, 100), (14, -1)])
18+
19+
20+
def test_to_datasource():
21+
assert _to_datasource(9) == DataSources.AIA_131
22+
assert _to_datasource(DataSources.AIA_94) == DataSources.AIA_94
23+
with pytest.raises(ValueError, match="999 is not a valid DataSources"):
24+
_to_datasource(999)
25+
26+
27+
def test_create_layers_string():
28+
assert _create_layer_string(DataSources.AIA_131, 100) == "[9,1,100]"
29+
with pytest.raises(ValueError, match="must be between 0 and 100"):
30+
_create_layer_string(DataSources.AIA_131, 101)
31+
with pytest.raises(ValueError, match="must be between 0 and 100"):
32+
_create_layer_string(DataSources.AIA_131, -1)

hvpy/utils.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
from typing import Any, Callable
1+
from typing import Any, Union, Callable
22
from datetime import datetime
33

4-
__all__ = ["add_shared_docstring", "convert_date_to_isoformat", "convert_date_to_unix"]
4+
from hvpy.datasources import DataSources
55

6+
__all__ = [
7+
"convert_date_to_isoformat",
8+
"convert_date_to_unix",
9+
"create_layers",
10+
]
611

7-
def add_shared_docstring(input_class) -> Callable[[Any], Any]:
12+
13+
def _add_shared_docstring(input_class) -> Callable[[Any], Any]:
814
def decorator(func):
915
if "{Shared}" in input_class.__doc__:
1016
split_doc = input_class.__doc__.split("{Shared}")
@@ -29,3 +35,43 @@ def convert_date_to_unix(v: list) -> str:
2935
commas.
3036
"""
3137
return ",".join([str(int(datetime.timestamp(d))) for d in v])
38+
39+
40+
def _to_datasource(val: Union[int, DataSources]) -> DataSources:
41+
"""
42+
Validates the input and converts it to a DataSources enum.
43+
"""
44+
if isinstance(val, int) and val in [x.value for x in DataSources]:
45+
return DataSources(val)
46+
elif isinstance(val, DataSources):
47+
return val
48+
else:
49+
raise ValueError(f"{val} is not a valid DataSources")
50+
51+
52+
def _create_layer_string(source_id: DataSources, opacity: int) -> str:
53+
"""
54+
Generates a string of the form "[source_id,1,opacity]" for a layer.
55+
"""
56+
if not 0 <= opacity <= 100:
57+
raise ValueError(f"opacity ({opacity}) must be between 0 and 100")
58+
else:
59+
return f"[{source_id.value},1,{opacity}]"
60+
61+
62+
def create_layers(layer: list) -> str:
63+
"""
64+
Creates a string of layers separated by commas.
65+
66+
Parameters
67+
----------
68+
layer
69+
A list of tuples of the form (``source_id``, ``opacity``).
70+
71+
Examples
72+
--------
73+
>>> from hvpy import create_layers
74+
>>> create_layers([(3, 50), (10, 50)])
75+
'[3,1,50],[10,1,50]'
76+
"""
77+
return ",".join([_create_layer_string(_to_datasource(s), o) for s, o in layer])

0 commit comments

Comments
 (0)