|
18 | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
|
20 | 20 |
|
21 | | -import datetime |
22 | | -from collections import defaultdict |
| 21 | +from libzim_wrapper import Blob |
23 | 22 |
|
24 | | -import libzim_wrapper |
25 | | - |
26 | | -ZimBlob = libzim_wrapper.ZimBlob |
27 | | - |
28 | | - |
29 | | -__all__ = ["ZimArticle", "ZimCreator", "ZimBlob"] |
30 | | - |
31 | | - |
32 | | -class ZimArticle: |
33 | | - def __init__(self): |
34 | | - self._blob = None |
35 | | - |
36 | | - def get_url(self): |
37 | | - raise NotImplementedError |
38 | | - |
39 | | - def get_title(self): |
40 | | - raise NotImplementedError |
41 | | - |
42 | | - def is_redirect(self): |
43 | | - raise NotImplementedError |
44 | | - |
45 | | - def get_mime_type(self): |
46 | | - raise NotImplementedError |
47 | | - |
48 | | - def get_filename(self): |
49 | | - raise NotImplementedError |
50 | | - |
51 | | - def should_compress(self): |
52 | | - raise NotImplementedError |
53 | | - |
54 | | - def should_index(self): |
55 | | - raise NotImplementedError |
56 | | - |
57 | | - def redirect_url(self): |
58 | | - raise NotImplementedError |
59 | | - |
60 | | - def _get_data(self): |
61 | | - if self._blob is None: |
62 | | - self._blob = self.get_data() |
63 | | - return self._blob |
64 | | - |
65 | | - def get_data(self): |
66 | | - raise NotImplementedError |
67 | | - |
68 | | - |
69 | | -class ZimMetadataArticle(ZimArticle): |
70 | | - def __init__(self, url, metadata_content): |
71 | | - ZimArticle.__init__(self) |
72 | | - self.url = url |
73 | | - self.metadata_content = metadata_content |
74 | | - |
75 | | - def is_redirect(self): |
76 | | - return False |
77 | | - |
78 | | - def get_url(self): |
79 | | - return f"M/{self.url}" |
80 | | - |
81 | | - def get_title(self): |
82 | | - return "" |
83 | | - |
84 | | - def get_mime_type(self): |
85 | | - return "text/plain" |
86 | | - |
87 | | - def get_filename(self): |
88 | | - return "" |
89 | | - |
90 | | - def should_compress(self): |
91 | | - return True |
92 | | - |
93 | | - def should_index(self): |
94 | | - return False |
95 | | - |
96 | | - def get_data(self): |
97 | | - return ZimBlob(self.metadata_content) |
98 | | - |
99 | | - |
100 | | -MANDATORY_METADATA_KEYS = [ |
101 | | - "Name", |
102 | | - "Title", |
103 | | - "Creator", |
104 | | - "Publisher", |
105 | | - "Date", |
106 | | - "Description", |
107 | | - "Language", |
108 | | -] |
109 | | - |
110 | | - |
111 | | -class ZimCreator: |
112 | | - """ |
113 | | - A class to represent a Zim Creator. |
114 | | -
|
115 | | - Attributes |
116 | | - ---------- |
117 | | - *c_creator : zim.ZimCreatorWrapper |
118 | | - a pointer to the C++ Creator object |
119 | | - _finalized : bool |
120 | | - flag if the creator was finalized |
121 | | - _filename : str |
122 | | - Zim file path |
123 | | - _main_page : str |
124 | | - Zim file main page |
125 | | - _index_language : str |
126 | | - Zim file Index language |
127 | | - _min_chunk_size : str |
128 | | - Zim file minimum chunk size |
129 | | - _article_counter |
130 | | - Zim file article counter |
131 | | - _metadata |
132 | | - Zim file metadata |
133 | | - """ |
134 | | - |
135 | | - def __init__(self, filename, main_page, index_language, min_chunk_size): |
136 | | - print(filename) |
137 | | - self._creatorWrapper = libzim_wrapper.ZimCreator( |
138 | | - filename, main_page, index_language, min_chunk_size |
139 | | - ) |
140 | | - self.filename = filename |
141 | | - self.main_page = main_page |
142 | | - self.language = index_language |
143 | | - self._metadata = {} |
144 | | - self._article_counter = defaultdict(int) |
145 | | - self.update_metadata(date=datetime.date.today(), language=index_language) |
146 | | - |
147 | | - def __enter__(self): |
148 | | - return self |
149 | | - |
150 | | - def __exit__(self, *args): |
151 | | - self.close() |
152 | | - |
153 | | - def add_article(self, article): |
154 | | - self._creatorWrapper.add_article(article) |
155 | | - if not article.is_redirect(): |
156 | | - self._update_article_counter(article) |
157 | | - |
158 | | - def _update_article_counter(self, article): |
159 | | - # default dict update |
160 | | - self._article_counter[article.get_mime_type().strip()] += 1 |
161 | | - |
162 | | - def mandatory_metadata_ok(self): |
163 | | - """Flag if mandatory metadata is complete and not empty""" |
164 | | - metadata_item_ok = [k in self._metadata for k in MANDATORY_METADATA_KEYS] |
165 | | - return all(metadata_item_ok) |
166 | | - |
167 | | - def update_metadata(self, **kwargs): |
168 | | - "Updates article metadata" "" |
169 | | - # Converts python case to pascal case. example: long_description-> LongDescription |
170 | | - pascalize = lambda keyword: "".join(keyword.title().split("_")) |
171 | | - new_metadata = {pascalize(k): v for k, v in kwargs.items()} |
172 | | - self._metadata.update(new_metadata) |
173 | | - |
174 | | - def write_metadata(self): |
175 | | - for key, value in self._metadata.items(): |
176 | | - if key == "date" and isinstance(value, datetime.date): |
177 | | - value = value.strftime("%Y-%m-%d") |
178 | | - article = ZimMetadataArticle(key, value) |
179 | | - self._creatorWrapper.add_article(article) |
180 | | - |
181 | | - article = ZimMetadataArticle("Counter", self._get_counter_string()) |
182 | | - self._creatorWrapper.add_article(article) |
183 | | - |
184 | | - def _get_counter_string(self): |
185 | | - return ";".join(["%s=%s" % (k, v) for (k, v) in self._article_counter.items()]) |
186 | | - |
187 | | - def close(self): |
188 | | - self.write_metadata() |
189 | | - self._creatorWrapper.finalize() |
190 | | - |
191 | | - def __repr__(self): |
192 | | - return f"ZimCreator(filename={self.filename})" |
| 23 | +__all__ = ["Blob"] |
0 commit comments