Exemple #1
0
    def get_books(self, path="", book_count=None):
        """Obtains book objects from dropbox folder
        :param path: Dropbox directory with syncronized\
        book data
        :param book_count: number of books to read
        """
        if not path and not self.books_path:
            raise ValueError("Path to read data from is not specified")
        if not path:
            path = self.books_path

        client = dropbox.client.DropboxClient(self.access_token)
        meta = client.metadata(path)
        files = filepaths_from_metadata(meta)
        moonreader_files = get_moonreader_files_from_filelist(files)

        if book_count is not None:
            file_pairs = get_same_book_files(moonreader_files)[:book_count]
        else:
            file_pairs = get_same_book_files(moonreader_files)

        for book_dict in dicts_from_pairs(client,
                                          file_pairs,
                                          workers=self.workers):
            try:
                book = Book.from_fobj_dict(book_dict)
                yield book
            except Exception:
                err_msg = "Exception occured when creating book object."
                logging.exception(err_msg)
def test_book_object_notes_are_constructed_from_dict():
    notes_dicts = [{'note_id': 1, 'text': 'text 1'},
                   {'note_id': 2, 'text': 'text 2'}]
    book = Book('title', notes=notes_dicts)

    assert len(book.notes) == 2
    assert book.notes[0].text == 'text 1'
    assert isinstance(book.notes[0], Note)
    assert book.notes[1].text == 'text 2'
    assert isinstance(book.notes[1], Note)
    def get_books(self, path="", book_count=None):
        """Obtains book objects from local directory"""
        if not path and not self.books_path:
            raise ValueError("Path to read data from is not specified")
        if not path:
            path = self.books_path

        moonreader_files = get_moonreader_files(path)
        tuples = get_same_book_files(moonreader_files)
        try:
            for book_files_tuple in tuples:
                b = Book.from_file_tuple(book_files_tuple)
                yield b
        except Exception:
            err_msg = "Exception occured when creating book object."
            logging.exception(err_msg)
 def test_book_type_correctly_parsed_from_simple_name(self):
     simple_name = "/test/book.pdf.po"
     self.assertEqual(Book._get_book_type(simple_name), "pdf")
def test_book_object_stat_is_constructed_from_dict():
    book = Book('title', stats={'percentage': 20, 'pages': 10})

    assert book.title == 'title'
    assert book.percentage == 20
    assert book.pages == 10
 def test_attempting_to_save_book_with_all_paths_params_raises_error(self):
     book = Book("test", None, None)
     with self.assertRaises(ParamTypeError):
         book.save(path="test", notes_file="test.an", stats_file="test.po")
 def test_incorrect_name_with_dot_in_path_raises_error(self):
     filename = "/test/.tricky_dir/filename.po"
     with self.assertRaises(BookTypeError):
         Book._get_book_type(filename)
 def test_default_type_returned_when_parsing_generic_file(self):
     simple_name = "/test/tricky_book"
     book_type = Book._get_book_type(simple_name, default_type="pdf")
     self.assertEqual(book_type, "pdf")
 def test_incorrect_extension_parsing_raises_error(self):
     filename = "/test/book.ext"
     with self.assertRaises(BookTypeError):
         Book._get_book_type(filename, extensions=(".po", ".an"))
 def test_exception_raised_when_parsing_noextname(self):
     simple_name = "/test/book_with_no_extension"
     with self.assertRaises(BookTypeError):
         Book._get_book_type(simple_name, allowed_types=("fb2", "pdf"))
 def test_exception_raised_when_type_is_not_correct(self):
     simple_name = "/test/book.djvu"
     with self.assertRaises(BookTypeError):
         Book._get_book_type(simple_name, allowed_types=("fb2", "pdf"))
 def test_book_type_correctly_get_from_zip_ext(self):
     simple_name = "/test/book.zip.po"
     self.assertEqual(Book._get_book_type(simple_name), "zip")
 def test_book_type_correctly_parsed_from_name_with_double_zip_ext(self):
     simple_name = "/test/book.fb2.zip.po"
     self.assertEqual(Book._get_book_type(simple_name), "fb2")