Skip to content

Commit be47986

Browse files
committed
0.2.0 - add typehints for components
Add a module `components.typehints` for providing annotations of the component properties.
1 parent ffd320c commit be47986

File tree

6 files changed

+89
-4
lines changed

6 files changed

+89
-4
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
3. Add the cross-origin support when serving the data to clients (e.g. the browser).
1414
4. Add the external URL proxy support for fetching the cross-origin resources.
1515
5. Add a service `ServiceDownloader` which allows the MITM served by the local device.
16+
6. Add a module `components.typehints` for providing annotations of the component properties.
1617

1718
#### :wrench: Fix
1819

dash_file_cache/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# Import frequently-used classes.
3434
from .caches import CachePlain, CacheQueue, CacheFile
3535
from .services import ServiceData, ServiceDownloader
36-
from .components import PlainDownloader, Downloader
36+
from .components import PlainDownloader, Downloader, DownloaderURL, DownloaderStatus
3737

3838
__all__ = (
3939
"__version__",
@@ -49,6 +49,8 @@
4949
"ServiceDownloader",
5050
"PlainDownloader",
5151
"Downloader",
52+
"DownloaderURL",
53+
"DownloaderStatus",
5254
)
5355

5456
_js_dist = []

dash_file_cache/caches/typehints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
Description
1616
-----------
17-
Extra typehints used by this project.
17+
Extra typehints used by the definition of caches.
1818
"""
1919

2020
import os

dash_file_cache/components/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,17 @@
3131
from ._imports_ import PlainDownloader, Downloader
3232
from ._imports_ import __all__ as __import_all__
3333

34+
from . import typehints
35+
from .typehints import DownloaderURL, DownloaderStatus
3436

35-
__all__ = ("PlainDownloader", "Downloader")
37+
38+
__all__ = (
39+
"typehints",
40+
"PlainDownloader",
41+
"Downloader",
42+
"DownloaderURL",
43+
"DownloaderStatus",
44+
)
3645

3746
if not hasattr(_dash, "__plotly_dash") and not hasattr(_dash, "development"):
3847
print(
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
Typehints
3+
=========
4+
@ Dash File Cache: components
5+
6+
Author
7+
------
8+
Yuchen Jin (cainmagi)
9+
cainmagi@gmail.com
10+
11+
License
12+
-------
13+
MIT License
14+
15+
Description
16+
-----------
17+
Extra typehints used for annotating the component properties.
18+
"""
19+
20+
from typing_extensions import Literal, TypedDict
21+
22+
23+
__all__ = ("DownloaderURL", "DownloaderStatus")
24+
25+
26+
class DownloaderURL(TypedDict):
27+
"""The component property `Downloader(url=...)`.
28+
29+
The URL used to access the data to be downloaded.
30+
31+
Each time when this value is set, a download event will be triggered. After
32+
triggering the download event, this value will be reset by a blank string.
33+
"""
34+
35+
url: str
36+
"""The URL used to access the data to be downloaded."""
37+
38+
file_name_fallback: str
39+
"""A maunally configured file name. If this file name is configured, it will
40+
be used when the file name cannot be parsed in the headers. This configuration
41+
is useful when the URL is from a cross-origin site."""
42+
43+
44+
class DownloaderStatus(TypedDict):
45+
"""The component property `Downloader(status=...)`.
46+
47+
The status code when a downloading event is finalized.
48+
49+
If multiple downloading events are triggered by the same downloader, the later
50+
event will overwrite the status from the former events.
51+
"""
52+
53+
type: Literal[
54+
"success", "error-connect", "error-config", "error-io", "error-unknown"
55+
]
56+
"""The status code of the event. If the event is successful, this value should.
57+
be "success" once the downloading event is finalized.
58+
"""
59+
60+
http_code: int
61+
"""The HTTP code from the response. If the event is successful, this value should
62+
be in the range of 200-299.."""

examples/download_file.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
4343

4444
from dash_file_cache import CacheFile
45-
from dash_file_cache import Downloader
45+
from dash_file_cache import Downloader, DownloaderStatus
4646
from dash_file_cache import ServiceData, ServiceDownloader
4747

4848

@@ -72,6 +72,7 @@ def layout(self) -> html.Div:
7272
html.Div((html.P(("Progress:", html.Span(id="prog"))))),
7373
html.Div((html.P("Cache type:"), html.P(id="type"))),
7474
html.Div((html.P("Cache address:"), html.P(id="addr"))),
75+
html.Div((html.P("Downloader status:"), html.P(id="dlstats"))),
7576
Downloader(id="download", mitm="/dfc-downloader"),
7677
),
7778
)
@@ -119,6 +120,16 @@ def trigger_downloading_event(addr):
119120
return dash.no_update
120121
return addr
121122

123+
@app.callback(
124+
Output("dlstats", "children"),
125+
Input("download", "status"),
126+
prevent_initial_call=True,
127+
)
128+
def trigger_downloading_status(status: Optional[DownloaderStatus]):
129+
if not status:
130+
return ""
131+
return str(status)
132+
122133

123134
class WrappedApp:
124135
"""A wrapped app. This wrapped app is accessed by the tests."""

0 commit comments

Comments
 (0)