def test_stat_dumps_to_string_correctly(self): po = Statistics(timestamp="1392540515970", pages=20, percentage=20.3) s = po.to_string() dumped = Statistics.from_string(s) self.assertEqual(dumped.percentage, 20.3) self.assertEqual(dumped.pages, 20) self.assertEqual(dumped.timestamp, "1392540515970")
def read_stat(self, text="", filename=""): """Update reading statistics representation from string of from specified file""" stat = None if text: stat = Statistics.from_string(stat) elif filename: stat = Statistics.from_file(filename) self._stats = stat return stat
def text_correctly_reads_from_file_p2(self, exists_mock, open_mock): open_mock.read.return_value = self.test_str exists_mock.return_value = True s = Statistics.from_file("aa" + STAT_EXTENSION) self.assertEqual(s.percentage, 7.8) self.assertEqual(s.pages, 15) self.assertEqual(s.uid, "1392540515970")
def from_file_tuple(cls, tpl): """Takes tuple of statistics file path, and notes file path and creates Book object from them""" stat_file, note_file = tpl fname = stat_file if stat_file else note_file title = cls._title_from_fname(fname) book_type = cls._get_book_type(fname) return cls(title, Statistics.from_file(note_file), MoonReaderNotes.from_file(stat_file), book_type=book_type)
def from_fobj_dict(cls, dct): """Takes a dictionary with note and statistics files paths and file descriptors and builds Book object from them General dict structure is as follows: dict = { "stat_file": ("/my/filename.po", <file_descriptor_1>), "note_file": ("/my/filename.an", <file_descriptor_2>) } """ fname = dct["stat_file"][0] if dct["stat_file"] else dct["note_file"][0] book_ext = cls._get_book_type(fname) book_title = cls._title_from_fname(fname) book_stat = Statistics.from_file_obj(dct["stat_file"][1]) book_notes = MoonReaderNotes.from_file_obj(dct["note_file"][1], book_ext) return cls(book_title, book_stat, book_notes)
def __init__(self, title, stats=None, notes=None, book_type=""): """ :param title: Book title :type title: str :param stats: Statistics object :type stats: moonreader_tools.stat.Statistics :param notes: list of Note objects :type notes: Iterable[Note] """ self.title = title self.stats = stats self.type = book_type if stats is None: self.stats = Statistics.empty_stats() else: self.stats = stats if notes is None or notes is False: self.notes = [] else: self.notes = notes
def stats(self, stats_obj): if isinstance(stats_obj, dict): self.__stats = Statistics.from_dict(stats_obj) else: self.__stats = stats_obj
def test_empty_stats_return_for_empty_file_p3(self, path_exists_mock): path_exists_mock.return_value = True s = Statistics.from_file("aaaa" + STAT_EXTENSION) self.assertTrue(s.is_empty())
def test_empty_fname_raises_error(self): with self.assertRaises(ValueError): Statistics.from_file("")
def test_incorrect_files(self, path_exists_mock): path_exists_mock.return_value = True fname = "noextensionfile" with self.assertRaises(AssertionError): Statistics.from_file(fname)
def test_full_str_is_parsed_correctly(self): po = Statistics.from_string(self.test_str) self.assertEqual(po.percentage, 7.8) self.assertEqual(po.pages, 15) self.assertEqual(po.timestamp, "1392540515970")