|
9 | 9 | import re
|
10 | 10 | import tempfile
|
11 | 11 | import urllib.parse
|
12 |
| -from typing import Dict, Union |
| 12 | +from typing import Any, Optional |
13 | 13 |
|
14 | 14 | import libzim.writer # pyright: ignore
|
15 | 15 |
|
|
23 | 23 |
|
24 | 24 |
|
25 | 25 | class Item(libzim.writer.Item):
|
26 |
| - """libzim.writer.Item returning props for path/title/mimetype plus a callback |
27 |
| -
|
28 |
| - Calls your `callback` prop on deletion""" |
29 |
| - |
30 |
| - def __init__(self, **kwargs: Dict[str, Union[str, bool, bytes]]): |
| 26 | + """libzim.writer.Item returning props for path/title/mimetype""" |
| 27 | + |
| 28 | + def __init__( |
| 29 | + self, |
| 30 | + path: Optional[str] = None, |
| 31 | + title: Optional[str] = None, |
| 32 | + mimetype: Optional[str] = None, |
| 33 | + hints: Optional[dict] = None, |
| 34 | + **kwargs: Any, |
| 35 | + ): |
31 | 36 | super().__init__()
|
| 37 | + if path: |
| 38 | + kwargs["path"] = path |
| 39 | + if title: |
| 40 | + kwargs["title"] = title |
| 41 | + if mimetype: |
| 42 | + kwargs["mimetype"] = mimetype |
| 43 | + if hints: |
| 44 | + kwargs["hints"] = hints |
32 | 45 | for k, v in kwargs.items():
|
33 | 46 | setattr(self, k, v)
|
34 | 47 |
|
@@ -57,21 +70,45 @@ class StaticItem(Item):
|
57 | 70 | more efficiently: now when the libzim destroys the CP, python will destroy
|
58 | 71 | the Item and we can be notified that we're effectively through with our content"""
|
59 | 72 |
|
| 73 | + def __init__( |
| 74 | + self, |
| 75 | + content: Optional[str] = None, |
| 76 | + fileobj: Optional[io.IOBase] = None, |
| 77 | + filepath: Optional[pathlib.Path] = None, |
| 78 | + path: Optional[str] = None, |
| 79 | + title: Optional[str] = None, |
| 80 | + mimetype: Optional[str] = None, |
| 81 | + hints: Optional[dict] = None, |
| 82 | + **kwargs: Any, |
| 83 | + ): |
| 84 | + if content: |
| 85 | + kwargs["content"] = content |
| 86 | + if fileobj: |
| 87 | + kwargs["fileobj"] = fileobj |
| 88 | + if filepath: |
| 89 | + kwargs["filepath"] = filepath |
| 90 | + super().__init__( |
| 91 | + path=path, title=title, mimetype=mimetype, hints=hints, **kwargs |
| 92 | + ) |
| 93 | + |
60 | 94 | def get_contentprovider(self) -> libzim.writer.ContentProvider:
|
61 | 95 | # content was set manually
|
62 |
| - if getattr(self, "content", None) is not None: |
63 |
| - return StringProvider(content=self.content, ref=self) |
| 96 | + content = getattr(self, "content", None) |
| 97 | + if content is not None: |
| 98 | + return StringProvider(content=content, ref=self) |
64 | 99 |
|
65 | 100 | # using a file-like object
|
66 |
| - if getattr(self, "fileobj", None): |
| 101 | + fileobj = getattr(self, "fileobj", None) |
| 102 | + if fileobj: |
67 | 103 | return FileLikeProvider(
|
68 |
| - fileobj=self.fileobj, ref=self, size=getattr(self, "size", None) |
| 104 | + fileobj=fileobj, ref=self, size=getattr(self, "size", None) |
69 | 105 | )
|
70 | 106 |
|
71 | 107 | # we had to download locally to get size
|
72 |
| - if getattr(self, "filepath", None): |
| 108 | + filepath = getattr(self, "filepath", None) |
| 109 | + if filepath: |
73 | 110 | return FileProvider(
|
74 |
| - filepath=self.filepath, ref=self, size=getattr(self, "size", None) |
| 111 | + filepath=filepath, ref=self, size=getattr(self, "size", None) |
75 | 112 | )
|
76 | 113 |
|
77 | 114 | raise NotImplementedError("No data to provide`")
|
@@ -106,8 +143,21 @@ def download_for_size(url, on_disk, tmp_dir=None):
|
106 | 143 | size, _ = stream_file(url.geturl(), fpath=fpath, byte_stream=stream)
|
107 | 144 | return fpath or stream, size
|
108 | 145 |
|
109 |
| - def __init__(self, url: str, **kwargs): |
110 |
| - super().__init__(**kwargs) |
| 146 | + def __init__( |
| 147 | + self, |
| 148 | + url: str, |
| 149 | + path: Optional[str] = None, |
| 150 | + title: Optional[str] = None, |
| 151 | + mimetype: Optional[str] = None, |
| 152 | + hints: Optional[dict] = None, |
| 153 | + use_disk: Optional[bool] = None, |
| 154 | + **kwargs: Any, |
| 155 | + ): |
| 156 | + if use_disk: |
| 157 | + kwargs["use_disk"] = use_disk |
| 158 | + super().__init__( |
| 159 | + path=path, title=title, mimetype=mimetype, hints=hints, **kwargs |
| 160 | + ) |
111 | 161 | self.url = urllib.parse.urlparse(url)
|
112 | 162 | use_disk = getattr(self, "use_disk", False)
|
113 | 163 |
|
|
0 commit comments