File tree Expand file tree Collapse file tree 5 files changed +30
-1
lines changed Expand file tree Collapse file tree 5 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -144,6 +144,7 @@ class Archive : public Wrapper<zim::Archive>
144144 FORWARD (wrapper::Entry, getEntryByPath)
145145 FORWARD (wrapper::Entry, getEntryByTitle)
146146 FORWARD (wrapper::Entry, getMainEntry)
147+ FORWARD (wrapper::Entry, getRandomEntry)
147148 FORWARD (wrapper::Item, getIllustrationItem)
148149 FORWARD (std::set<unsigned int >, getIllustrationSizes)
149150 std::string getUuid () const
Original file line number Diff line number Diff line change @@ -1101,6 +1101,21 @@ cdef class Archive:
11011101 raise KeyError(str(e))
11021102 return Entry.from_entry(move(entry))
11031103
1104+ def get_random_entry(self) -> Entry:
1105+ """ Get a random `Entry`.
1106+
1107+ Returns:
1108+ A random entry.
1109+
1110+ Raises:
1111+ KeyError : If no valid random entry is found.
1112+ """
1113+ try:
1114+ entry = move(self.c_archive.getRandomEntry())
1115+ except RuntimeError as e:
1116+ raise KeyError(str(e))
1117+ return Entry.from_entry(move(entry))
1118+
11041119 @property
11051120 def metadata_keys(self) -> List[str]:
11061121 """ List of Metadata keys present in this archive.
Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ class Archive:
4141 def get_entry_by_path (self , path : str ) -> Entry : ...
4242 def has_entry_by_title (self , title : str ) -> bool : ...
4343 def get_entry_by_title (self , title : str ) -> Entry : ...
44+ def get_random_entry (self ) -> Entry : ...
4445 @property
4546 def metadata_keys (self ) -> list [str ]: ...
4647 def get_metadata_item (self , name : str ) -> Item : ...
Original file line number Diff line number Diff line change @@ -153,6 +153,7 @@ cdef extern from "libwrapper.h" namespace "wrapper":
153153 vector[string] getMetadataKeys() except +
154154
155155 Entry getMainEntry() except +
156+ Entry getRandomEntry() except +
156157 Item getIllustrationItem() except +
157158 Item getIllustrationItem(int size) except +
158159 size_type getEntryCount() except +
Original file line number Diff line number Diff line change 99import pytest
1010
1111import libzim .writer # pyright: ignore [reportMissingModuleSource]
12- from libzim .reader import Archive # pyright: ignore [reportMissingModuleSource]
12+ from libzim .reader import Archive , Entry # pyright: ignore [reportMissingModuleSource]
1313from libzim .search import Query , Searcher # pyright: ignore [reportMissingModuleSource]
1414from libzim .suggestion import ( # pyright: ignore [reportMissingModuleSource]
1515 SuggestionSearcher ,
@@ -599,3 +599,14 @@ def filename(self):
599599 assert zim != Different (fpath1 )
600600 assert zim == Sub (fpath1 )
601601 assert zim != Sub2 (fpath1 )
602+
603+
604+ @skip_if_offline
605+ def test_reader_get_random_entry (all_zims ):
606+ fpath1 = Archive (all_zims / "zimfile.zim" )
607+ fpath2 = Archive (all_zims / "example.zim" )
608+
609+ assert isinstance (fpath1 .get_random_entry (), Entry )
610+
611+ with pytest .raises (KeyError ):
612+ fpath2 .get_random_entry ()
You can’t perform that action at this time.
0 commit comments