File tree Expand file tree Collapse file tree 6 files changed +38
-1
lines changed Expand file tree Collapse file tree 6 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
88## [ Unreleased]
99
10+ ### Added
11+
1012- Set up documentation using ` mkdocs ` , published on readthedocs.com (#186 )
13+ - ` Archive.get_random_entry() `
1114
1215## [ 3.6.0] - 2024-10-15
1316
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,19 @@ def filename(self):
599599 assert zim != Different (fpath1 )
600600 assert zim == Sub (fpath1 )
601601 assert zim != Sub2 (fpath1 )
602+
603+
604+ def test_reader_get_random_entry (all_zims ):
605+ zim_1 = Archive (all_zims / "zimfile.zim" )
606+
607+ entry_1 = zim_1 .get_random_entry ()
608+ entry_2 = zim_1 .get_random_entry ()
609+ assert isinstance (entry_1 , Entry )
610+ assert isinstance (entry_2 , Entry )
611+ # this may occasionaly fail (1 chance over 129)
612+ assert entry_1 != entry_2
613+
614+ # example.zim has no FRONT_ARTICLE (article_count=0): random cannot yield any result
615+ zim_2 = Archive (all_zims / "example.zim" )
616+ with pytest .raises (KeyError ):
617+ zim_2 .get_random_entry ()
You can’t perform that action at this time.
0 commit comments