Skip to content

Commit cac931d

Browse files
authored
Merge pull request #24 from openzim/context-manager-classmethod
2 parents 8b9bec2 + ccbcaa1 commit cac931d

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

README.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,19 @@ class ZimTestArticle(ZimArticle):
5858
# Create a ZimTestArticle article
5959

6060
article = ZimTestArticle()
61-
print(article.content)
6261

6362
# Write the articles
63+
6464
import uuid
6565
rnd_str = str(uuid.uuid1())
66-
6766
test_zim_file_path = "/opt/python-libzim/tests/kiwix-test"
6867

69-
zim_creator = ZimCreator(test_zim_file_path + '-' + rnd_str + '.zim',main_page = "welcome",index_language= "eng", min_chunk_size= 2048)
70-
71-
# Add article to zim file
72-
zim_creator.add_article(article)
73-
74-
75-
# Set mandatory metadata
76-
if not zim_creator.mandatory_metadata_ok():
77-
zim_creator.update_metadata(creator='python-libzim',description='Created in python',name='Hola',publisher='Monadical',title='Test Zim')
78-
79-
# Write article to zim file
80-
zim_creator.finalize()
68+
with ZimCreator(test_zim_file_path + '-' + rnd_str + '.zim') as zc:
69+
zc.add_article(article)
70+
if not zc.mandatory_metadata_ok():
71+
zc.update_metadata(creator='python-libzim',
72+
description='Created in python',
73+
name='Hola',publisher='Monadical',
74+
title='Test Zim')
8175

8276
```

libzim/examples.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,18 @@ def get_data(self):
7272
print(zim_creator._get_metadata())
7373

7474
# Write articles to zim file
75-
zim_creator.finalize()
75+
zim_creator.finalize()
76+
77+
78+
# Example using context manager to ensure finalize is called.
79+
80+
rnd_str = str(uuid.uuid1())
81+
82+
with ZimCreator(test_zim_file_path + '-' + rnd_str + '.zim') as zc:
83+
zc.add_article(article)
84+
zc.add_article(article2)
85+
zc.update_metadata(creator='python-libzim',
86+
description='Created in python',
87+
name='Hola',publisher='Monadical',
88+
title='Test Zim')
89+

libzim/libzim.pyx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ from libcpp cimport bool
88
from libcpp.memory cimport shared_ptr, make_shared
99

1010
import datetime
11-
11+
from contextlib import contextmanager
1212
from collections import defaultdict
1313

1414
#########################
@@ -172,7 +172,7 @@ cdef class ZimCreator:
172172
173173
Attributes
174174
----------
175-
*c_creator : zim.ZimCreator
175+
*c_creator : zim.ZimCreatorWrapper
176176
a pointer to the C++ Creator object
177177
_finalized : bool
178178
flag if the creator was finalized
@@ -224,6 +224,12 @@ cdef class ZimCreator:
224224
self._article_counter = defaultdict(int)
225225
self.update_metadata(date=datetime.date.today(), language= index_language)
226226

227+
def __enter__(self):
228+
return self
229+
230+
def __exit__(self, *args):
231+
self.finalize()
232+
227233
def __dealloc__(self):
228234
del self.c_creator
229235

0 commit comments

Comments
 (0)