Skip to content

Commit 7900d3e

Browse files
committed
Reformat code using black and isort.
1 parent 8ebad9d commit 7900d3e

File tree

5 files changed

+63
-45
lines changed

5 files changed

+63
-45
lines changed

libzim/__init__.py

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

2020

21+
import datetime
22+
from collections import defaultdict
23+
2124
import libzim_wrapper
2225

2326
ZimBlob = libzim_wrapper.ZimBlob
2427

25-
from collections import defaultdict
26-
import datetime
2728

28-
__all__ = ["ZimArticle", "ZimCreator", "ZimBlob"]
29+
__all__ = ["ZimArticle", "ZimCreator", "ZimBlob"]
30+
2931

3032
class ZimArticle:
3133
def __init__(self):
@@ -95,14 +97,16 @@ def get_data(self):
9597
return ZimBlob(self.metadata_content)
9698

9799

98-
MANDATORY_METADATA_KEYS =[
100+
MANDATORY_METADATA_KEYS = [
99101
"Name",
100102
"Title",
101103
"Creator",
102104
"Publisher",
103105
"Date",
104106
"Description",
105-
"Language"]
107+
"Language",
108+
]
109+
106110

107111
class ZimCreator:
108112
"""
@@ -131,10 +135,8 @@ class ZimCreator:
131135
def __init__(self, filename, main_page, index_language, min_chunk_size):
132136
print(filename)
133137
self._creatorWrapper = libzim_wrapper.ZimCreator(
134-
filename,
135-
main_page,
136-
index_language,
137-
min_chunk_size)
138+
filename, main_page, index_language, min_chunk_size
139+
)
138140
self.filename = filename
139141
self.main_page = main_page
140142
self.language = index_language
@@ -162,15 +164,13 @@ def mandatory_metadata_ok(self):
162164
metadata_item_ok = [k in self._metadata for k in MANDATORY_METADATA_KEYS]
163165
return all(metadata_item_ok)
164166

165-
166167
def update_metadata(self, **kwargs):
167-
"Updates article metadata"""
168+
"Updates article metadata" ""
168169
# Converts python case to pascal case. example: long_description-> LongDescription
169170
pascalize = lambda keyword: "".join(keyword.title().split("_"))
170171
new_metadata = {pascalize(k): v for k, v in kwargs.items()}
171172
self._metadata.update(new_metadata)
172173

173-
174174
def write_metadata(self):
175175
for key, value in self._metadata.items():
176176
if key == "date" and isinstance(value, datetime.date):
@@ -182,9 +182,7 @@ def write_metadata(self):
182182
self._creatorWrapper.add_article(article)
183183

184184
def _get_counter_string(self):
185-
return ";".join(
186-
["%s=%s" % (k,v) for (k,v) in self._article_counter.items()]
187-
)
185+
return ";".join(["%s=%s" % (k, v) for (k, v) in self._article_counter.items()])
188186

189187
def close(self):
190188
self.write_metadata()

libzim/examples.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

1919

20+
# Write the article
21+
import uuid
22+
2023
from libzim import ZimArticle, ZimBlob, ZimCreator
2124

22-
class ZimTestArticle(ZimArticle):
2325

26+
class ZimTestArticle(ZimArticle):
2427
def __init__(self, url, title, content):
2528
ZimArticle.__init__(self)
2629
self.url = url
@@ -35,13 +38,13 @@ def get_url(self):
3538

3639
def get_title(self):
3740
return f"{self.title}"
38-
41+
3942
def get_mime_type(self):
4043
return "text/html"
41-
44+
4245
def get_filename(self):
4346
return ""
44-
47+
4548
def should_compress(self):
4649
return True
4750

@@ -51,42 +54,53 @@ def should_index(self):
5154
def get_data(self):
5255
return ZimBlob(self.content)
5356

57+
5458
# Create a ZimTestArticle article
5559

56-
content = '''<!DOCTYPE html>
60+
content = """<!DOCTYPE html>
5761
<html class="client-js">
5862
<head><meta charset="UTF-8">
5963
<title>Monadical</title>
6064
</head>
61-
<h1> ñññ Hello, it works ñññ </h1></html>'''
65+
<h1> ñññ Hello, it works ñññ </h1></html>"""
6266

63-
content2 = '''<!DOCTYPE html>
67+
content2 = """<!DOCTYPE html>
6468
<html class="client-js">
6569
<head><meta charset="UTF-8">
6670
<title>Monadical 2</title>
6771
</head>
68-
<h1> ñññ Hello, it works 2 ñññ </h1></html>'''
72+
<h1> ñññ Hello, it works 2 ñññ </h1></html>"""
6973

7074
article = ZimTestArticle("Monadical_SAS", "Monadical", content)
7175
article2 = ZimTestArticle("Monadical_2", "Monadical 2", content2)
7276

7377
print(article.content)
7478

75-
# Write the article
76-
import uuid
77-
rnd_str = str(uuid.uuid1())
79+
80+
rnd_str = str(uuid.uuid1())
7881

7982
test_zim_file_path = "/opt/python-libzim/tests/kiwix-test"
8083

81-
zim_creator = ZimCreator(test_zim_file_path + '-' + rnd_str + '.zim',main_page = "Monadical",index_language= "eng", min_chunk_size= 2048)
84+
zim_creator = ZimCreator(
85+
test_zim_file_path + "-" + rnd_str + ".zim",
86+
main_page="Monadical",
87+
index_language="eng",
88+
min_chunk_size=2048,
89+
)
8290

8391
# Add articles to zim file
8492
zim_creator.add_article(article)
8593
zim_creator.add_article(article2)
8694

8795
# Set mandatory metadata
8896
if not zim_creator.mandatory_metadata_ok():
89-
zim_creator.update_metadata(creator='python-libzim',description='Created in python',name='Hola',publisher='Monadical',title='Test Zim')
97+
zim_creator.update_metadata(
98+
creator="python-libzim",
99+
description="Created in python",
100+
name="Hola",
101+
publisher="Monadical",
102+
title="Test Zim",
103+
)
90104

91105
print(zim_creator._get_metadata())
92106

@@ -98,11 +112,13 @@ def get_data(self):
98112

99113
rnd_str = str(uuid.uuid1())
100114

101-
with ZimCreator(test_zim_file_path + '-' + rnd_str + '.zim') as zc:
115+
with ZimCreator(test_zim_file_path + "-" + rnd_str + ".zim") as zc:
102116
zc.add_article(article)
103117
zc.add_article(article2)
104-
zc.update_metadata(creator='python-libzim',
105-
description='Created in python',
106-
name='Hola',publisher='Monadical',
107-
title='Test Zim')
108-
118+
zc.update_metadata(
119+
creator="python-libzim",
120+
description="Created in python",
121+
name="Hola",
122+
publisher="Monadical",
123+
title="Test Zim",
124+
)

libzim/libzim_wrapper.pxd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
# You should have received a copy of the GNU General Public License
1818
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1919

20-
from libcpp.string cimport string
20+
from cpython.ref cimport PyObject
21+
2122
from libc.stdint cimport uint32_t, uint64_t
2223
from libcpp cimport bool
2324
from libcpp.memory cimport shared_ptr
25+
from libcpp.string cimport string
2426
from libcpp.vector cimport vector
2527

2628

27-
from cpython.ref cimport PyObject
28-
2929
cdef extern from "zim/blob.h" namespace "zim":
3030
cdef cppclass Blob:
3131
Blob() except +

libzim/libzim_wrapper.pyx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020

2121
cimport libzim.libzim_wrapper as clibzim
22+
2223
from cython.operator import dereference
24+
from cpython.ref cimport PyObject
2325

2426
from libc.stdint cimport uint64_t
2527
from libcpp.string cimport string
@@ -28,7 +30,8 @@ from libcpp.memory cimport shared_ptr, make_shared
2830

2931
import datetime
3032

31-
from cpython.ref cimport PyObject
33+
34+
3235

3336
#########################
3437
# ZimBlob #
@@ -162,4 +165,3 @@ cdef class ZimCreator:
162165
with nogil:
163166
self.c_creator.finalize()
164167
self._finalized = True
165-

tests/test_libzim.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import pytest
20-
import os,sys,inspect
2120

2221
from libzim import ZimArticle, ZimBlob, ZimCreator
2322

@@ -29,7 +28,7 @@
2928
def metadata():
3029
return {
3130
# Mandatory
32-
"Name" : "wikipedia_fr_football",
31+
"Name": "wikipedia_fr_football",
3332
"Title": "English Wikipedia",
3433
"Creator": "English speaking Wikipedia contributors",
3534
"Publisher": "Wikipedia user Foobar",
@@ -43,22 +42,24 @@ def metadata():
4342
"Flavour": "nopic",
4443
"Source": "https://en.wikipedia.org/",
4544
"Counter": "image/jpeg=5;image/gif=3;image/png=2",
46-
"Scraper": "sotoki 1.2.3"
45+
"Scraper": "sotoki 1.2.3",
4746
}
4847

48+
4949
@pytest.fixture(scope="session")
5050
def article_content():
51-
content = '''<!DOCTYPE html>
51+
content = """<!DOCTYPE html>
5252
<html class="client-js">
5353
<head><meta charset="UTF-8">
5454
<title>Monadical</title>
5555
</head>
56-
<h1> ñññ Hello, it works ñññ </h1></html>'''
56+
<h1> ñññ Hello, it works ñññ </h1></html>"""
5757
url = "A/Monadical_SAS"
5858
title = "Monadical SAS"
5959
mime_type = "text/html"
6060
return (content, url, title, mime_type)
6161

62+
6263
class ZimTestArticle(ZimArticle):
6364
def __init__(self, content, url, title, mime_type):
6465
ZimArticle.__init__(self)
@@ -93,7 +94,8 @@ def should_index(self):
9394
return True
9495

9596
def get_data(self):
96-
return ZimBlob(self.content.encode('UTF-8'))
97+
return ZimBlob(self.content.encode("UTF-8"))
98+
9799

98100
@pytest.fixture(scope="session")
99101
def article(article_content):

0 commit comments

Comments
 (0)