Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Declare the extension free-threading compatible so that importing it on a free-threaded CPython no longer forces the GIL back on (#240)
- **Breaking**: `Creator.config_nbworkers()` param `nbWorkers` renamed `nb_workers` (#200)
- **Breaking**: `Creator.set_mainpath()` param `mainPath` renamed `main_path` (#200)
- **Breaking**: `Creator.add_redirection()` param `targetPath` renamed `target_path` (#200)
- **Breaking**: `Creator.add_alias()` param `targetPath` renamed `target_path` (#200)
- **Breaking**: `Search.getEstimatedMatches()` renamed `Search.get_estimated_matches()` (#200)
- **Breaking**: `Search.getResults()` renamed `Search.get_results()` (#200)
- **Breaking**: `SuggestionSearch.getEstimatedMatches()` renamed `SuggestionSearch.get_estimated_matches()` (#200)
- **Breaking**: `SuggestionSearch.getResults()` renamed `SuggestionSearch.get_results()` (#200)

### Removed

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ search_string = "Welcome"
query = Query().set_query(search_string)
searcher = Searcher(zim)
search = searcher.search(query)
search_count = search.getEstimatedMatches()
search_count = search.get_estimated_matches()
print(f"there are {search_count} matches for {search_string}")
print(list(search.getResults(0, search_count)))
print(list(search.get_results(0, search_count)))

# accessing suggestions
search_string = "kiwix"
suggestion_searcher = SuggestionSearcher(zim)
suggestion = suggestion_searcher.suggest(search_string)
suggestion_count = suggestion.getEstimatedMatches()
suggestion_count = suggestion.get_estimated_matches()
print(f"there are {suggestion_count} matches for {search_string}")
print(list(suggestion.getResults(0, suggestion_count)))
print(list(suggestion.get_results(0, suggestion_count)))
```

### Write a ZIM file
Expand Down
40 changes: 20 additions & 20 deletions libzim/libzim.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -454,30 +454,30 @@ cdef class _Creator:
self.c_creator.configIndexing(indexing, language.encode('UTF-8'))
return self

def config_nbworkers(self, int nbWorkers: pyint) -> _Creator:
def config_nbworkers(self, int nb_workers: pyint) -> _Creator:
"""Configures the number of threads to use for internal workers (default: 4).

Args:
nbWorkers (int): The number of threads to allocate.
nb_workers (int): The number of threads to allocate.

Returns:
The Creator instance with updated worker thread settings.
"""
if self._started:
raise RuntimeError("Creator started")
self.c_creator.configNbWorkers(nbWorkers)
self.c_creator.configNbWorkers(nb_workers)
return self

def set_mainpath(self, str mainPath: str) -> _Creator:
def set_mainpath(self, str main_path: str) -> _Creator:
"""Set path of the main entry.

Args:
mainPath (str): The path of the main entry.
main_path (str): The path of the main entry.

Returns:
The Creator instance with the updated main entry path.
"""
self.c_creator.setMainPath(mainPath.encode('UTF-8'))
self.c_creator.setMainPath(main_path.encode('UTF-8'))
return self

def add_illustration(self, int size: pyint, content: bytes):
Expand Down Expand Up @@ -540,15 +540,15 @@ cdef class _Creator:
with nogil:
self.c_creator.addMetadata(_name, _content, _mimetype)

def add_redirection(self, str path: str, str title: str, str targetPath: str, dict hints: Dict[Hint, pyint]):
def add_redirection(self, str path: str, str title: str, str target_path: str, dict hints: Dict[Hint, pyint]):
"""Add redirection entry to the archive.

Refer to https://wiki.openzim.org/wiki/ZIM_file_format#Redirect_Entry for more details.

Args:
path (str): The path of the redirection entry.
title (str): The title associated with the redirection.
targetPath (str): The target path for the redirection.
target_path (str): The target path for the redirection.
hints (dict[Hint, int]): A dictionary of hints for the redirection.

Raises:
Expand All @@ -560,30 +560,30 @@ cdef class _Creator:

cdef string _path = path.encode('UTF-8')
cdef string _title = title.encode('UTF-8')
cdef string _targetPath = targetPath.encode('UTF-8')
cdef string _targetPath = target_path.encode('UTF-8')
cdef map[zim.HintKeys, uint64_t] _hints = convertToCppHints(hints)
with nogil:
self.c_creator.addRedirection(_path, _title, _targetPath, _hints)

def add_alias(self, str path: str, str title: str, str targetPath: str, dict hints: Dict[Hint, pyint]):
"""Alias the (existing) entry `targetPath` as a new entry `path`.
def add_alias(self, str path: str, str title: str, str target_path: str, dict hints: Dict[Hint, pyint]):
"""Alias the (existing) entry `target_path` as a new entry `path`.

Args:
path (str): The path for the new alias.
title (str): The title associated with the alias.
targetPath (str): The existing entry to be aliased.
target_path (str): The existing entry to be aliased.
hints (dict[Hint, int]): A dictionary of hints for the alias.

Raises:
RuntimeError: If the `targetPath` entry doesn't exist.
RuntimeError: If the `target_path` entry doesn't exist.
"""

if not self._started:
raise RuntimeError("Creator not started")

cdef string _path = path.encode('UTF-8')
cdef string _title = title.encode('UTF-8')
cdef string _targetPath = targetPath.encode('UTF-8')
cdef string _targetPath = target_path.encode('UTF-8')
cdef map[zim.HintKeys, uint64_t] _hints = convertToCppHints(hints)
with nogil:
self.c_creator.addAlias(_path, _title, _targetPath, _hints)
Expand Down Expand Up @@ -1491,7 +1491,7 @@ cdef class Search:
search.c_search = move(_search)
return search

def getEstimatedMatches(self) -> pyint:
def get_estimated_matches(self) -> pyint:
"""Estimated number of results in Archive for the search.


Expand All @@ -1500,7 +1500,7 @@ cdef class Search:
"""
return self.c_search.getEstimatedMatches()

def getResults(self, start: pyint, count: pyint) -> SearchResultSet:
def get_results(self, start: pyint, count: pyint) -> SearchResultSet:
"""Iterator over Entry paths found in Archive for the search.

Args:
Expand Down Expand Up @@ -1555,7 +1555,7 @@ archive = libzim.reader.Archive(fpath)
searcher = Searcher(archive)
query = Query().set_query("foo")
search = searcher.search(query)
for path in search.getResults(10, 10) # get result from 10 to 20 (10 results)
for path in search.get_results(10, 10) # get result from 10 to 20 (10 results)
print(path, archive.get_entry_by_path(path).title)
```"""
search_public_objects = [
Expand Down Expand Up @@ -1611,15 +1611,15 @@ cdef class SuggestionSearch:
search.c_search = move(_search)
return search

def getEstimatedMatches(self) -> pyint:
def get_estimated_matches(self) -> pyint:
"""Estimated number of results in Archive for the suggestion search.

Returns:
The number of estimated results for this suggestion search.
"""
return self.c_search.getEstimatedMatches()

def getResults(self, start: pyint, count: pyint) -> SuggestionResultSet:
def get_results(self, start: pyint, count: pyint) -> SuggestionResultSet:
"""Iterator over Entry paths found in Archive for the suggestion search.

Returns:
Expand Down Expand Up @@ -1668,7 +1668,7 @@ Usage:
archive = Archive(fpath)
suggestion_searcher = SuggestionSearcher(archive)
suggestions = suggestion_searcher.suggest("foo")
for path in suggestion.getResults(10, 10) # get result from 10 to 20 (10 results)
for path in suggestion.get_results(10, 10) # get result from 10 to 20 (10 results)
print(path, archive.get_entry_by_path(path).title)
```"""
suggestion_public_objects = [
Expand Down
4 changes: 2 additions & 2 deletions libzim/search.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class SearchResultSet:
def __iter__(self) -> Iterator[str]: ...

class Search:
def getEstimatedMatches(self) -> int: ... # noqa: N802
def getResults(self, start: int, count: int) -> SearchResultSet: ... # noqa: N802
def get_estimated_matches(self) -> int: ...
def get_results(self, start: int, count: int) -> SearchResultSet: ...

class Searcher:
def __init__(self, archive: Archive) -> None: ...
Expand Down
6 changes: 2 additions & 4 deletions libzim/suggestion.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ class SuggestionResultSet:
def __iter__(self) -> Iterator[str]: ...

class SuggestionSearch:
def getEstimatedMatches(self) -> int: ... # noqa: N802
def getResults( # noqa: N802
self, start: int, count: int
) -> SuggestionResultSet: ...
def get_estimated_matches(self) -> int: ...
def get_results(self, start: int, count: int) -> SuggestionResultSet: ...

class SuggestionSearcher:
def __init__(self, archive: Archive) -> None: ...
Expand Down
8 changes: 4 additions & 4 deletions libzim/writer.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class Creator:
def config_compression(self, compression: Compression | str) -> Self: ...
def config_clustersize(self, size: int) -> Self: ...
def config_indexing(self, indexing: bool, language: str) -> Self: ...
def config_nbworkers(self, nbWorkers: int) -> Self: ... # noqa: N803
def set_mainpath(self, mainPath: str) -> Self: ... # noqa: N803
def config_nbworkers(self, nb_workers: int) -> Self: ...
def set_mainpath(self, main_path: str) -> Self: ...
def add_illustration(self, size: int, content: bytes) -> None: ...
def add_item(self, writer_item: Item) -> None: ...
def add_metadata(
Expand All @@ -72,14 +72,14 @@ class Creator:
self,
path: str,
title: str,
targetPath: str, # noqa: N803
target_path: str,
hints: dict[Hint, int],
) -> None: ...
def add_alias(
self,
path: str,
title: str,
targetPath: str, # noqa: N803
target_path: str,
hints: dict[Hint, int],
) -> None: ...
def __enter__(self) -> Self: ...
Expand Down
30 changes: 15 additions & 15 deletions tests/test_libzim_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def test_creator_indexing(fpath, lipsum_item, indexing, language, expected):
query = Query().set_query("standard")
searcher = Searcher(zim)
search = searcher.search(query)
assert search.getEstimatedMatches() == expected
assert search.get_estimated_matches() == expected


@pytest.mark.parametrize("nb_workers", [1, 2, 3, 5])
Expand Down Expand Up @@ -472,10 +472,10 @@ def test_creator_redirection(fpath, lipsum_item):
# suggestions
sugg_searcher = SuggestionSearcher(zim)
sugg_hello = sugg_searcher.suggest("hello")
assert "home" in list(sugg_hello.getResults(0, sugg_hello.getEstimatedMatches()))
assert "home" in list(sugg_hello.get_results(0, sugg_hello.get_estimated_matches()))
sugg_bonjour = sugg_searcher.suggest("bonjour")
assert "accueil" in list(
sugg_bonjour.getResults(0, sugg_hello.getEstimatedMatches())
sugg_bonjour.get_results(0, sugg_hello.get_estimated_matches())
)


Expand Down Expand Up @@ -659,7 +659,7 @@ def test_hints_values(fpath):
# Hint values are casted to bool
c.add_item(StaticItem(path="3", title="", hints={Hint.FRONT_ARTICLE: "world"}))
c.add_redirection(
path="4", title="", targetPath="0", hints={Hint.COMPRESS: True}
path="4", title="", target_path="0", hints={Hint.COMPRESS: True}
)
# filtered-out values
c.add_item(StaticItem(path="5", title="", hints={5: True}))
Expand All @@ -683,13 +683,13 @@ def test_hints_values(fpath):
c.add_redirection( # pyright: ignore [reportCallIssue]
path="5",
title="",
targetPath="0",
target_path="0",
hints={Hint.YOLO: True}, # pyright: ignore [reportAttributeAccessIssue]
)


@pytest.mark.parametrize(
"indexData, customContent, search_expected",
"index_data, custom_content, search_expected",
[
(None, "", [("standard", 1), ("home", 0), ("computer", 0)]),
(False, "", [("standard", 1), ("home", 0), ("computer", 0)]),
Expand All @@ -699,22 +699,22 @@ def test_hints_values(fpath):
],
)
def test_custom_indexdata(
fpath, lipsum_item, lipsum, indexData, customContent, search_expected
fpath, lipsum_item, lipsum, index_data, custom_content, search_expected
):
item = StaticItem(path=HOME_PATH + "custom", content=lipsum, mimetype="text/html")
if indexData is None:
if index_data is None:
item.get_indexdata = lambda: None
else:

class CustomIndexData(IndexData):
def has_indexdata(self):
return indexData
return index_data

def get_title(self):
return ""

def get_content(self):
return customContent
return custom_content

def get_keywords(self):
return ""
Expand All @@ -733,7 +733,7 @@ def get_wordcount(self):
for search_query, expected in search_expected:
query = Query().set_query(search_query)
search = searcher.search(query)
assert search.getEstimatedMatches() == expected
assert search.get_estimated_matches() == expected


def test_indexdata_interface():
Expand Down Expand Up @@ -871,11 +871,11 @@ def test_accented_search_from_libzim(fpath):
ascii_query = Query().set_query("test article")
ascii_searcher = Searcher(zim)
ascii_search = ascii_searcher.search(ascii_query)
assert ascii_search.getEstimatedMatches() == zim.article_count
assert list(ascii_search.getResults(0, zim.article_count)) == ["path0", "path1"]
assert ascii_search.get_estimated_matches() == zim.article_count
assert list(ascii_search.get_results(0, zim.article_count)) == ["path0", "path1"]

accented_query = Query().set_query("test àrticlé")
accented_searcher = Searcher(zim)
accented_search = accented_searcher.search(accented_query)
assert accented_search.getEstimatedMatches() == zim.article_count
assert list(accented_search.getResults(0, zim.article_count)) == ["path0", "path1"]
assert accented_search.get_estimated_matches() == zim.article_count
assert list(accented_search.get_results(0, zim.article_count)) == ["path0", "path1"]
8 changes: 4 additions & 4 deletions tests/test_libzim_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,14 +488,14 @@ def test_reader_suggest_search(
query = Query().set_query(search_string)
searcher = Searcher(zim)
search = searcher.search(query)
assert search.getEstimatedMatches() == search_count
assert list(search.getResults(0, search_count)) == search_result
assert search.get_estimated_matches() == search_count
assert list(search.get_results(0, search_count)) == search_result

if suggestion_string is not None:
suggestion_searcher = SuggestionSearcher(zim)
suggestion = suggestion_searcher.suggest(suggestion_string)
assert suggestion.getEstimatedMatches() == suggestion_count
assert list(suggestion.getResults(0, suggestion_count)) == suggestion_result
assert suggestion.get_estimated_matches() == suggestion_count
assert list(suggestion.get_results(0, suggestion_count)) == suggestion_result


@skip_if_offline
Expand Down
Loading