Skip to content

Commit 3729ae9

Browse files
jdcaballerovmgautierfr
authored andcommitted
Add write metadata function
1 parent 39fbc31 commit 3729ae9

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

libzim/examples.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def get_data(self):
5959
if not zim_creator.mandatory_metadata_ok:
6060
zim_creator.update_metadata(creator='python-libzim',description='Created in python',name='Hola',publisher='Monadical',title='Test Zim')
6161

62+
zim_creator.write_metadata(zim_creator._get_metadata())
63+
6264
# Write articles to zim file
6365
zim_creator.finalize()
6466

libzim/libzim.pyx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,42 @@ cdef public api:
134134

135135
#TODO Write metadata
136136

137+
class ZimMetadataArticle(ZimArticle):
138+
139+
def __init__(self,url, metadata_content):
140+
ZimArticle.__init__(self)
141+
self.url = url
142+
self.metadata_content = metadata_content
143+
144+
def is_redirect(self):
145+
return False
146+
147+
@property
148+
def can_write(self):
149+
return True
150+
151+
def get_url(self):
152+
return f"M/{self.url}"
153+
154+
def get_title(self):
155+
return f"{self.url}"
156+
157+
def get_mime_type(self):
158+
return "text/plain"
159+
160+
def get_filename(self):
161+
return ""
162+
163+
def should_compress(self):
164+
return True
165+
166+
def should_index(self):
167+
return True
168+
169+
def get_data(self):
170+
return ZimBlob(self.metadata_content)
171+
172+
137173
MANDATORY_METADATA_KEYS =[
138174
"Name",
139175
"Title",
@@ -301,6 +337,11 @@ cdef class ZimCreator:
301337
if not article.is_redirect():
302338
self._update_article_counter(article)
303339

340+
def write_metadata(self, dict metadata):
341+
for key in metadata:
342+
metadata_article = ZimMetadataArticle(url=key, metadata_content=metadata[key])
343+
self.add_article(metadata_article)
344+
304345
def finalize(self):
305346
"""finalize and write added articles to the file.
306347

tests/test_libzim.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ def test_write_article(self):
9393
rnd_str = str(uuid.uuid1())
9494
zim_creator = ZimCreator(self.test_zim_file_path + '-' + rnd_str + '.zim',main_page = "welcome",index_language= "eng", min_chunk_size= 2048)
9595
zim_creator.add_article(self.test_article)
96+
# Set mandatory metadata
97+
zim_creator.update_metadata(creator='python-libzim',description='Created in python',name='Hola',publisher='Monadical',title='Test Zim')
98+
zim_creator.write_metadata(zim_creator._get_metadata())
9699
zim_creator.finalize()
97100

98101

0 commit comments

Comments
 (0)