Skip to content

Commit 2fd3a98

Browse files
committed
Move public writer classes in a writer sub-module.
Fix #17
1 parent 7900d3e commit 2fd3a98

File tree

5 files changed

+213
-196
lines changed

5 files changed

+213
-196
lines changed

libzim/__init__.py

Lines changed: 2 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -18,175 +18,6 @@
1818
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1919

2020

21-
import datetime
22-
from collections import defaultdict
21+
from libzim_wrapper import Blob
2322

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"]

libzim/examples.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
# Write the article
2121
import uuid
2222

23-
from libzim import ZimArticle, ZimBlob, ZimCreator
23+
from libzim.writer import Article, Blob, Creator
2424

2525

26-
class ZimTestArticle(ZimArticle):
26+
class TestArticle(Article):
2727
def __init__(self, url, title, content):
28-
ZimArticle.__init__(self)
28+
Article.__init__(self)
2929
self.url = url
3030
self.title = title
3131
self.content = content
@@ -52,10 +52,10 @@ def should_index(self):
5252
return True
5353

5454
def get_data(self):
55-
return ZimBlob(self.content)
55+
return Blob(self.content)
5656

5757

58-
# Create a ZimTestArticle article
58+
# Create a TestArticle article
5959

6060
content = """<!DOCTYPE html>
6161
<html class="client-js">
@@ -71,8 +71,8 @@ def get_data(self):
7171
</head>
7272
<h1> ñññ Hello, it works 2 ñññ </h1></html>"""
7373

74-
article = ZimTestArticle("Monadical_SAS", "Monadical", content)
75-
article2 = ZimTestArticle("Monadical_2", "Monadical 2", content2)
74+
article = TestArticle("Monadical_SAS", "Monadical", content)
75+
article2 = TestArticle("Monadical_2", "Monadical 2", content2)
7676

7777
print(article.content)
7878

@@ -81,7 +81,7 @@ def get_data(self):
8181

8282
test_zim_file_path = "/opt/python-libzim/tests/kiwix-test"
8383

84-
zim_creator = ZimCreator(
84+
zim_creator = Creator(
8585
test_zim_file_path + "-" + rnd_str + ".zim",
8686
main_page="Monadical",
8787
index_language="eng",
@@ -112,7 +112,7 @@ def get_data(self):
112112

113113
rnd_str = str(uuid.uuid1())
114114

115-
with ZimCreator(test_zim_file_path + "-" + rnd_str + ".zim") as zc:
115+
with Creator(test_zim_file_path + "-" + rnd_str + ".zim") as zc:
116116
zc.add_article(article)
117117
zc.add_article(article2)
118118
zc.update_metadata(

libzim/libzim_wrapper.pyx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ import datetime
3434

3535

3636
#########################
37-
# ZimBlob #
37+
# Blob #
3838
#########################
3939

40-
cdef class ZimBlob:
40+
cdef class Blob:
4141
cdef clibzim.Blob* c_blob
4242
cdef bytes ref_content
4343

@@ -53,10 +53,6 @@ cdef class ZimBlob:
5353
del self.c_blob
5454

5555

56-
#########################
57-
# ZimArticle #
58-
#########################
59-
6056
#------ Helper for pure virtual methods --------
6157

6258
cdef get_article_method_from_object(object obj, string method, int *error) with gil:
@@ -80,7 +76,7 @@ cdef public api:
8076

8177
clibzim.Blob blob_cy_call_fct(object obj, string method, int *error) with gil:
8278
"""Lookup and execute a pure virtual method on ZimArticle returning a Blob"""
83-
cdef ZimBlob blob
79+
cdef Blob blob
8480

8581
func = get_article_method_from_object(obj, method, error)
8682
blob = func()
@@ -96,7 +92,7 @@ cdef public api:
9692
func = get_article_method_from_object(obj, method, error)
9793
return <uint64_t> func()
9894

99-
cdef class ZimCreator:
95+
cdef class Creator:
10096
"""
10197
A class to represent a Zim Creator.
10298

0 commit comments

Comments
 (0)