def get_manuscript(self, id_: int) -> Manuscript: try: return next( manuscript for manuscript in self.manuscripts if manuscript.id == id_ ) except StopIteration as error: raise NotFoundError(f"No manuscripts with id {id_}.") from error
def query_by_file_name(self, file_name: str) -> File: grid_out = self._fs.find_one({"filename": file_name}) if grid_out is None: raise NotFoundError(f"File {file_name} not found.") else: return GridFsFile(grid_out)
def test_find_chapter_raises_exception_if_references_not_found( corpus, text_repository, bibliography, when) -> None: when(text_repository).find_chapter( CHAPTER.id_).thenReturn(CHAPTER_WITHOUT_DOCUMENTS) when(bibliography).find(...).thenRaise(NotFoundError()) with pytest.raises(Defect): corpus.find_chapter(CHAPTER.id_)
def create_chapter_id(genre: str, category: str, index: str, stage: str, name: str) -> ChapterId: try: return ChapterId(create_text_id(genre, category, index), Stage(stage), name) except (ValueError, NotFoundError) as error: raise NotFoundError( f"Chapter {genre} {category}.{index} {stage} {name} not found." ) from error
def query_by_museum_number(self, number: MuseumNumber): data = self._fragments.aggregate( [ {"$match": museum_number_is(number)}, *join_reference_documents(), *join_joins(), ] ) try: fragment_data = next(data) return FragmentSchema(unknown=EXCLUDE).load(fragment_data) except StopIteration as error: raise NotFoundError(f"Fragment {number} not found.") from error
def query_next_and_previous_folio(self, folio_name, folio_number, number): sort_ascending = {"$sort": {"key": 1}} sort_descending = {"$sort": {"key": -1}} def create_pipeline(*parts): return [ {"$match": {"folios.name": folio_name}}, {"$unwind": "$folios"}, { "$project": { "name": "$folios.name", "number": "$folios.number", "key": {"$concat": ["$folios.number", "-", "$_id"]}, } }, {"$match": {"name": folio_name}}, *parts, {"$limit": 1}, ] def get_numbers(pipeline): cursor = self._fragments.aggregate(pipeline) try: entry = next(cursor) return {"fragmentNumber": entry["_id"], "folioNumber": entry["number"]} except StopIteration: return None first = create_pipeline(sort_ascending) previous = create_pipeline( {"$match": {"key": {"$lt": f"{folio_number}-{number}"}}}, sort_descending ) next_ = create_pipeline( {"$match": {"key": {"$gt": f"{folio_number}-{number}"}}}, sort_ascending ) last = create_pipeline(sort_descending) result = { "previous": get_numbers(previous) or get_numbers(last), "next": get_numbers(next_) or get_numbers(first), } if has_none_values(result): raise NotFoundError("Could not retrieve any fragments") else: return result
def on_get( self, _, resp: falcon.Response, genre: str, category: str, index: str, stage: str, name: str, number: str, ) -> None: chapter_id = create_chapter_id(genre, category, index, stage, name) try: line, manuscripts = self._corpus.find_line(chapter_id, int(number)) schema = LineDetailsSchema(context={"manuscripts": manuscripts}) resp.media = schema.dump(line) except (IndexError, ValueError) as error: raise NotFoundError( f"{chapter_id} line {number} not found.") from error
def find_implicit_chapter(self, text_id: TextId) -> ChapterName: try: chapter = next( self._chapters.find_many( { "textId.genre": text_id.genre.value, "textId.category": text_id.category, "textId.index": text_id.index, }, sort=[("order", 1)], projection={ "_id": False, "stage": True, "name": True, "version": True, }, )) return ChapterNameSchema().load(chapter) except StopIteration as error: raise NotFoundError( f"No chapters found for text {text_id}.") from error
def __not_found_error(self, query): return NotFoundError(f"{self.__resource_noun} {query} not found.")
def line_not_found(id_: ChapterId, number: int) -> Exception: return NotFoundError(f"Chapter {id_} line {number} not found.")
def chapter_not_found(id_: ChapterId) -> Exception: return NotFoundError(f"Chapter {id_} not found.")
def text_not_found(id_: TextId) -> Exception: return NotFoundError(f"Text {id_} not found.")
def create_text_id(genre: str, category: str, index: str) -> TextId: try: return TextId(Genre(genre), int(category), int(index)) except ValueError as error: raise NotFoundError( f"Text {genre} {category}.{index} not found.") from error
def expect_invalid_references(bibliography, when) -> None: when(bibliography).find(...).thenRaise(NotFoundError())