def runTest(self): for creator in self.creators: thumbdir = LocalFolder(self.create_tmp_dir(creator.__name__)) dir = self.SRC_DIR.folder('data/pixmaps') for i, basename in enumerate(dir.list_names()): if basename.endswith('.svg'): continue # fails on windows in some cases file = dir.file(basename) thumbfile = thumbdir.file('thumb--' + basename) self.assertFalse(thumbfile.exists()) pixbuf = creator(file, thumbfile, THUMB_SIZE_NORMAL) self.assertIsInstance(pixbuf, GdkPixbuf.Pixbuf) self.assertTrue(thumbfile.exists()) pixbuf = GdkPixbuf.Pixbuf.new_from_file(thumbfile.path) self.assertEqual(pixbuf.get_option('tEXt::Thumb::URI'), file.uri) self.assertTrue( pixbuf.get_option('tEXt::Thumb::URI').startswith( 'file:///')) # Specific requirement of spec to use file:/// and not file://localhost/ self.assertEqual(int(pixbuf.get_option('tEXt::Thumb::MTime')), int(file.mtime())) self.assertTrue(i > 3) thumbfile = thumbdir.file('thumb-test.txt') self.assertRaises(ThumbnailCreatorFailure, creator, self.SRC_DIR.file('README.txt'), thumbfile, THUMB_SIZE_NORMAL)
def setUp(self): folder = LocalFolder(XDG_DATA_HOME.subdir('zim/templates').path) assert 'tests/tmp' in folder.path.replace('\\', '/') if folder.exists(): folder.remove_children() for name, content in ( ('html/foo_test.html', 'Test 123\n'), ('html/bar_test.html', 'Test 123\n'), ): folder.file(name).write(content)
def setUp(self): folder = LocalFolder(XDG_DATA_HOME.subdir('zim/templates').path) assert 'tests/tmp' in folder.path.replace('\\', '/') if folder.exists(): folder.remove_children() for name, content in ( ('html/foo_test.html', 'Test 123\n'), ('html/bar_test.html', 'Test 123\n'), ): folder.file(name).write(content) manager = ApplicationManager() entry = manager.create('text/plain', 'test', 'test') manager.set_default_application('text/plain', entry)
def testCreateThumbnail(self): manager = ThumbnailManager() dir = LocalFolder(self.create_tmp_dir()) file = dir.file('zim.png') self.SRC_DIR.file('data/zim.png').copyto(file) self.assertTrue(file.exists()) self.assertTrue(file.isimage()) self.removeThumbnail(manager, file) # Thumbfile does not exist thumbfile, pixbuf = manager.get_thumbnail(file, 64, create=False) self.assertEqual((thumbfile, pixbuf), (None, None)) thumbfile, pixbuf = manager.get_thumbnail(file, 64) self.assertTrue(thumbfile.exists()) self.assertIsInstance(pixbuf, GdkPixbuf.Pixbuf) thumbfile, pixbuf = manager.get_thumbnail(file, 64) self.assertTrue(thumbfile.exists()) self.assertIsInstance(pixbuf, GdkPixbuf.Pixbuf) if os.name != 'nt': # Windows support chmod() is limitted import stat mode = os.stat(thumbfile.path).st_mode self.assertEqual(stat.S_IMODE(mode), 0o600) mode = os.stat( thumbfile.parent().parent().path).st_mode # thumnails dir self.assertEqual(stat.S_IMODE(mode), 0o700) # Change mtime to make thumbfile invalid oldmtime = file.mtime() os.utime(file.path, None) self.assertNotEqual(file.mtime(), oldmtime) thumbfile, pixbuf = manager.get_thumbnail(file, 64, create=False) self.assertEqual((thumbfile, pixbuf), (None, None)) thumbfile, pixbuf = manager.get_thumbnail(file, 64) self.assertTrue(thumbfile.exists()) self.assertIsInstance(pixbuf, GdkPixbuf.Pixbuf) # ensure next call to get_thumbnail cannot call create_thumbnail manager.create_thumbnail = None thumbfile, pixbuf = manager.get_thumbnail(file, 64) self.assertTrue(thumbfile.exists()) self.assertIsInstance(pixbuf, GdkPixbuf.Pixbuf) # Test remove self.removeThumbnail(manager, file)
def _iswritable(dir): if os.name == 'nt': # Test access - (iswritable turns out to be unreliable for folders on windows..) if isinstance(dir, Dir): dir = LocalFolder(dir.path) # only newfs supports cleanup=False f = dir.file('.zim.tmp') try: f.write('Test') f.remove(cleanup=False) except: return False else: return True else: return dir.iswritable()
def export_attachments_iter(self, notebook, page): # XXX FIXME remove need for notebook here # XXX what to do with folders that do not map to a page ? source = notebook.get_attachments_dir(page) target = self.layout.attachments_dir(page) assert isinstance(target, Dir) target = LocalFolder(target.path) # XXX convert try: for file in source.list_files(): yield file targetfile = target.file(file.basename) if targetfile.exists(): targetfile.remove() # Export does overwrite by default file.copyto(targetfile) except FileNotFoundError: pass
def export_iter(self, pages): basename = encode_filename(pages.name) folder = LocalFolder(get_tmpdir().subdir('mhtml_export_tmp_dir').path) # XXX if folder.exists(): folder.remove_children() else: folder.touch() file = folder.file(basename + '.html') layout = SingleFileLayout(file, pages.prefix) exporter = SingleFileExporter(layout, self.template, 'html', document_root_url=self.document_root_url) for p in exporter.export_iter(pages): yield p encoder = MHTMLEncoder() linker = ExportLinker(pages.notebook, layout, output=file, usebase=True) self.file.write(encoder(layout, linker))
def testQueue(self): queue = ThumbnailQueue() self.assertTrue(queue.queue_empty()) # Test input / output src_file = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL).file('test.txt') src_file.write('Test 123\n') queue.queue_thumbnail_request(src_file, 64) # put an error in the queue dir = LocalFolder(tests.ZIM_DATADIR).folder('pixmaps') pixmaps = set() for basename in dir.list_names(): if not basename.endswith('.svg'): file = dir.file(basename) pixmaps.add(file.path) queue.queue_thumbnail_request(file, 64) self.assertFalse(queue.queue_empty()) with tests.LoggingFilter('zim.plugins.attachmentbrowser', 'Exception'): queue.start() seen = set() i = len(pixmaps) while i > 0: i -= 1 file, size, thumbfile, pixbuf, mtime = queue.get_ready_thumbnail(block=True) seen.add(file.path) self.assertEqual(size, 64) self.assertTrue(thumbfile.exists()) self.assertIsInstance(pixbuf, GdkPixbuf.Pixbuf) self.assertEqual(mtime, file.mtime()) self.assertEqual(seen, pixmaps) # Test clear self.assertTrue(queue.queue_empty()) for path in pixmaps: file = LocalFile(path) queue.queue_thumbnail_request(file, 64) self.assertFalse(queue.queue_empty()) queue.start() time.sleep(0.1) queue.clear_queue() self.assertTrue(queue.queue_empty())
def testThumbnailFile(self): manager = ThumbnailManager() folder = LocalFolder(self.get_tmp_name('empty')) file = folder.file(u'./foo-\u00e8\u00e1\u00f1.png' ) # non-existing path with unicode name self.assertTrue('%C3%A8%C3%A1%C3%B1' in file.uri) # utf encoded! basename = hashlib.md5(file.uri).hexdigest() + '.png' for file, size, wanted in ( (file, 28, LOCAL_THUMB_STORAGE_NORMAL.file(basename)), (file, 64, LOCAL_THUMB_STORAGE_NORMAL.file(basename)), (file, 128, LOCAL_THUMB_STORAGE_NORMAL.file(basename)), (file, 200, LOCAL_THUMB_STORAGE_LARGE.file(basename)), (file, 500, LOCAL_THUMB_STORAGE_LARGE.file(basename)), ): thumbfile = manager.get_thumbnail_file(file, size) self.assertEqual(thumbfile, wanted) self.assertTrue(len(thumbfile.basename) == 32 + 4) # lenght hexdigest according to spec + ".png"