Skip to content

Commit 70a74b1

Browse files
author
renaud gaudin
committed
Fixed #38: raising NotFound on unavailable article
raise RuntimeError on out-of-bound get_article_by_id() changed get_article_by_id signature to not use the reserved python keyword `id`
1 parent 0080f58 commit 70a74b1

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

libzim/reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# flake8: noqa
22

3-
from .wrapper import FilePy as File
3+
from .wrapper import FilePy as File, NotFound
44
from .wrapper import ReadArticle as Article

libzim/wrapper.pyx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ from libcpp.memory cimport shared_ptr, make_shared, unique_ptr
3131

3232
import datetime
3333

34+
class NotFound(RuntimeError):
35+
pass
36+
3437
#########################
3538
# Blob #
3639
#########################
@@ -392,13 +395,13 @@ cdef class FilePy:
392395
The Article object
393396
Raises
394397
------
395-
RuntimeError
398+
NotFound
396399
If an article with the provided long url is not found in the file
397400
"""
398401
# Read to a zim::Article
399402
cdef wrapper.Article art = self.c_file.getArticleByUrl(url.encode('UTF-8'))
400403
if not art.good():
401-
raise RuntimeError("Article not found for url")
404+
raise NotFound("Article not found for url")
402405

403406
article = ReadArticle.from_read_article(art)
404407
return article
@@ -413,12 +416,12 @@ cdef class FilePy:
413416
article = self.get_article(f"M/{name}")
414417
return article.content
415418

416-
def get_article_by_id(self, id):
419+
def get_article_by_id(self, article_id):
417420
"""Get a ZimFileArticle with a copy of the file article by article id.
418421
419422
Parameters
420423
----------
421-
id : int
424+
article_id : int
422425
The id of the article
423426
Returns
424427
-------
@@ -427,13 +430,15 @@ cdef class FilePy:
427430
Raises
428431
------
429432
RuntimeError
433+
If there is a problem in retrieving article (ex: id is out of bound)
434+
NotFound
430435
If an article with the provided id is not found in the file
431436
"""
432437

433438
# Read to a zim::Article
434-
cdef wrapper.Article art = self.c_file.getArticle(<int> id)
439+
cdef wrapper.Article art = self.c_file.getArticle(<int> article_id)
435440
if not art.good():
436-
raise RuntimeError("Article not found for id")
441+
raise NotFound("Article not found for id")
437442

438443
article = ReadArticle.from_read_article(art)
439444
return article

tests/test_libzim_file_reader.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from libzim.reader import File
6+
from libzim.reader import File, NotFound
77

88
DATA_DIR = Path(__file__).parent
99

@@ -110,9 +110,9 @@ def test_search(reader):
110110

111111

112112
def test_get_wrong_article(reader):
113-
with pytest.raises(RuntimeError):
113+
with pytest.raises(RuntimeError): # out of range
114114
reader.get_article_by_id(reader.article_count + 100)
115-
with pytest.raises(RuntimeError):
115+
with pytest.raises(NotFound):
116116
reader.get_article("A/I_do_not_exists")
117117

118118

0 commit comments

Comments
 (0)