Skip to content

Commit 1f5e68c

Browse files
committed
Update README.md and examples/basic_writer.py
1 parent 786848f commit 1f5e68c

File tree

2 files changed

+35
-148
lines changed

2 files changed

+35
-148
lines changed

README.md

Lines changed: 33 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@ pip3 install libzim
1010

1111
This library allows you to interact with `.zim` files via Python.
1212

13-
It just provides a shallow Python interface on top of the `libzim` C++ library (maintained by [OpenZIM](https://github.com/openzim)). The versions are pinned between the two packages (`python-libzim==6.1.1 <=> libzim==6.1.1`).
13+
It just provides a shallow Python interface on top of the `libzim` C++ library (maintained by [OpenZIM](https://github.com/openzim)).
1414

1515
It is primarily used by [`sotoki`](https://github.com/openzim/sotoki).
1616

1717
## Quickstart
1818

19+
### Reader API
20+
1921
```python3
20-
# Writer API
21-
from libzim import ZimCreator, ZimArticle, ZimBlob, ...
22+
from libzim.reader import File
2223

23-
with zimcreator('test.zim') as zc:
24-
zc.add_article(ZimArticle(...))
24+
f = File("test.zim")
25+
article = f.get_article("article/url.html")
26+
print(article.url, article.title)
27+
if not article.is_redirect():
28+
print(article.content)
2529
```
2630

27-
```python3
28-
# Reader API (coming soon...)
29-
from libzim import zimreader
31+
### Write API
32+
33+
See [example](examples/basic_writer.py) for a basic usage of the writer API.
3034

31-
with zimreader('test.zim') as zr:
32-
for article in zr.namespace('A'):
33-
print(article.url, article.title, article.content.decode())
34-
```
3535

3636
---
3737

@@ -40,30 +40,29 @@ with zimreader('test.zim') as zr:
4040
### Setup: Ubuntu/Debian `x86_64` (Recommended)
4141

4242
Install the python `libzim` package from PyPI.
43+
4344
```bash
4445
pip3 install libzim
45-
python -c "from libzim import ZimArticle"
4646
```
4747

4848
The `x86_64` linux wheel automatically includes the `libzim.so` dylib and headers, but other platforms may need to install `libzim` and its headers manually.
4949

50-
#### Installing the `libzim` dylib and headers manually
50+
51+
### Installing the `libzim` dylib and headers manually
52+
53+
If you are not on a linux `x86_64` platform, you will have to install libzim manually.
54+
55+
Either by get a prebuilt binary at https://download.openzim.org/release/libzim
56+
or [compile `libzim` from source](https://github.com/openzim/libzim).
57+
58+
If you have not installed libzim in standard directory, you will have to set `LD_LIBRARY_PATH` to allow python to find the library :
59+
60+
Assuming you have extracted (or installed) the library if LIBZIM_DIR:
61+
5162

5263
```bash
53-
# Install the `libzim` dylib and headers from a pre-built release
54-
LIBZIM_VERSION=6.1.1
55-
LIBZIM_RELEASE=libzim_linux-x86_64-$LIBZIM_VERSION
56-
LIBZIM_LIBRARY_PATH=lib/x86_64-linux-gnu/libzim.so.$LIBZIM_VERSION
57-
LIBZIM_INCLUDE_PATH=include/zim
58-
59-
wget -qO- https://download.openzim.org/release/libzim/$LIBZIM_RELEASE.tar.gz | tar -xz -C .
60-
sudo mv $LIBZIM_RELEASE/$LIBZIM_LIBRARY_PATH lib/libzim.so
61-
sudo mv $LIBZIM_RELEASE/$LIBZIM_INCLUDE_PATH include/zim
62-
export LD_LIBRARY_PATH="$PWD/lib:$LD_LIBRARY_PATH"
63-
sudo ldconfig
64+
export LD_LIBRARY_PATH="${LIBZIM_DIR}/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH"
6465
```
65-
If a pre-built release is not available for your platform, you can also [install `libzim` from source](https://github.com/openzim/libzim#dependencies).
66-
6766

6867
## Setup: Docker (Optional)
6968

@@ -75,7 +74,7 @@ docker run -it openzim:python-libzim ./some_example_script.py
7574

7675
# Or use the python repl interactively
7776
docker run -it openzim:python-libzim
78-
>>> from libzim import ZimCreator, ZimArticle, ZimBlob
77+
>>> import libzim
7978
```
8079

8180
---
@@ -95,12 +94,13 @@ apt install coreutils wget git ca-certificates \
9594

9695
pip3 install --upgrade pip pipenv
9796

97+
export CFLAGS="-I${LIBZIM_DIR}/include"
98+
export LDFLAGS="-L${LIBZIM_DIR}/lib/x86_64-linux-gnu"
9899
git clone https://github.com/openzim/python-libzim
99100
cd python-libzim
100101
python setup.py build_ext
101102
pipenv install --dev
102103
pipenv run pip install -e .
103-
pipenv run python -c "from libzim import ZimArticle"
104104
```
105105

106106
### Setup: Docker
@@ -116,7 +116,7 @@ $ pipenv install --dev <newpackagehere>
116116
$ python setup.py build_ext
117117
$ python setup.py sdist bdist_wheel
118118
$ python setup.py install
119-
$ python -c "from libzim import ZimArticle"
119+
$ python -c "import libzim"
120120

121121
```
122122

@@ -160,90 +160,12 @@ twine upload dist/*
160160
### Use a specific `libzim` dylib and headers when compiling `python-libzim`
161161

162162
```bash
163-
export CFLAGS="-I/tmp/libzim_linux-x86_64-6.1.1/include"
164-
export LDFLAGS="-L/tmp/libzim_linux-x86_64-6.1.1/lib/x86_64-linux-gnu"
165-
export LD_LIBRARY_PATH="/tmp/libzim_linux-x86_64-6.1.1/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH"
163+
export CFLAGS="-I${LIBZIM_DIR}/include"
164+
export LDFLAGS="-L${LIBZIM_DIR}/lib/x86_64-linux-gnu"
165+
export LD_LIBRARY_PATH="${LIBZIM_DIR}/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH"
166166
python setup.py build_ext
167167
python setup.py install
168168
```
169-
170-
---
171-
172-
## Examples
173-
174-
```python3
175-
from libzim import zimcreator, ZimArticle, ZimBlob, ZimCreator
176-
177-
class ZimTestArticle(ZimArticle):
178-
content = '''<!DOCTYPE html>
179-
<html class="client-js">
180-
<head><meta charset="UTF-8">
181-
<title>Monadical</title>
182-
</head>
183-
<h1> ñññ Hello, it works ñññ </h1></html>'''
184-
185-
def __init__(self):
186-
ZimArticle.__init__(self)
187-
188-
def is_redirect(self):
189-
return False
190-
191-
def get_url(self):
192-
return "A/Monadical_SAS"
193-
194-
def get_title(self):
195-
return "Monadical SAS"
196-
197-
def get_mime_type(self):
198-
return "text/html"
199-
200-
def get_filename(self):
201-
return ""
202-
203-
def should_compress(self):
204-
return True
205-
206-
def should_index(self):
207-
return True
208-
209-
def get_data(self):
210-
return ZimBlob(self.content.encode('UTF-8'))
211-
212-
213-
# Set up a ZimCreator instance to collect articles into one .zim file
214-
zim_creator = ZimCreator(
215-
'test.zim',
216-
main_page="welcome",
217-
index_language="eng",
218-
min_chunk_size=2048,
219-
)
220-
zim_creator.update_metadata(
221-
name='Hola',
222-
title='Test Zim',
223-
publisher='Monadical',
224-
creator='python-libzim',
225-
description='Created in python',
226-
)
227-
228-
# Write the test article to .zim file by adding it via the ZimCreator
229-
zim_creator.add_article(ZimTestArticle())
230-
231-
# ZimCreator.finalize() must be called in order to save the writes to disk
232-
zim_creator.finalize()
233-
234-
# Alternatively, use the context manager form of ZimCreator
235-
# to avoid having to call .finalize() manually:
236-
with zimcreator('test.zim', main_index="welcome", ...) as zc:
237-
zc.add_article(article)
238-
if not zc.mandatory_metadata_ok():
239-
zc.update_metadata(creator='python-libzim',
240-
description='Created in python',
241-
name='Hola',
242-
publisher='Monadical',
243-
title='Test Zim')
244-
# zc.finalize() is called automatically when context manager exits
245-
```
246-
247169
---
248170

249171
## Further Reading

examples/basic.py renamed to examples/basic_writer.py

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -74,45 +74,10 @@ def get_data(self):
7474
article = TestArticle("Monadical_SAS", "Monadical", content)
7575
article2 = TestArticle("Monadical_2", "Monadical 2", content2)
7676

77-
print(article.content)
78-
79-
80-
rnd_str = str(uuid.uuid1())
81-
82-
test_zim_file_path = "/opt/python-libzim/tests/kiwix-test"
83-
84-
zim_creator = Creator(
85-
test_zim_file_path + "-" + rnd_str + ".zim",
86-
main_page="Monadical",
87-
index_language="eng",
88-
min_chunk_size=2048,
89-
)
90-
91-
# Add articles to zim file
92-
zim_creator.add_article(article)
93-
zim_creator.add_article(article2)
94-
95-
# Set mandatory metadata
96-
if not zim_creator.mandatory_metadata_ok():
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-
)
104-
105-
print(zim_creator._get_metadata())
106-
107-
# Write articles to zim file
108-
zim_creator.finalize()
109-
110-
111-
# Example using context manager to ensure finalize is called.
112-
11377
rnd_str = str(uuid.uuid1())
78+
zim_file_path = f"kiwix-test-{rnd_str}.zim"
11479

115-
with Creator(test_zim_file_path + "-" + rnd_str + ".zim") as zc:
80+
with Creator(zim_file_path, main_page="Monadical", index_language="eng", min_chunk_size=2048) as zc:
11681
zc.add_article(article)
11782
zc.add_article(article2)
11883
zc.update_metadata(

0 commit comments

Comments
 (0)