def testOpenFile(self): from zim.gui.applications import open_file, NoApplicationFoundError widget = tests.MockObject() with self.assertRaises(FileNotFoundError): open_file(widget, File('/non-existing')) folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL) myfile = folder.file('test.txt') myfile.touch() manager = ApplicationManager() entry = manager.create('text/plain', 'test', 'test') manager.set_default_application('text/plain', entry) open_file(widget, myfile) self.assertEqual(self.calls[-1], (widget, entry, adapt_from_newfs(myfile), None)) open_file(widget, myfile, mimetype='text/plain') self.assertEqual(self.calls[-1], (widget, entry, adapt_from_newfs(myfile), None)) with self.assertRaises(NoApplicationFoundError): open_file(widget, myfile, mimetype='x-mimetype/x-with-no-application')
def testOpenFolderCreate(self): from zim.gui.applications import open_folder_prompt_create from zim.fs import adapt_from_newfs widget = tests.MockObject() folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL) myfolder = folder.folder('test') entry = ApplicationManager().get_fallback_filebrowser() def answer_yes(dialog): dialog.answer_yes() def answer_no(dialog): dialog.answer_no() with tests.DialogContext(answer_no): open_folder_prompt_create(widget, myfolder) self.assertFalse(myfolder.exists()) self.assertEqual(self.calls, []) with tests.DialogContext(answer_yes): open_folder_prompt_create(widget, myfolder) self.assertTrue(myfolder.exists()) self.assertEqual(self.calls[-1], (widget, entry, adapt_from_newfs(myfolder), None))
def init_notebook(dir, name=None): '''Initialize a new notebook in a directory''' dir = adapt_from_newfs(dir) assert isinstance(dir, Dir) from .notebook import NotebookConfig dir.touch() config = NotebookConfig(dir.file('notebook.zim')) config['Notebook']['name'] = name or dir.basename config.write()
def open_folder(widget, folder): '''Open a folder. @param widget: parent for new dialogs, C{Gtk.Widget} or C{None} @param folder: a L{Folder} object @raises FileNotFoundError: if C{folder} does not exist see L{open_folder_prompt_create} for alternative behavior when folder does not exist. ''' dir = adapt_from_newfs(folder) open_file(widget, dir)
def testOpenFolder(self): from zim.gui.applications import open_folder from zim.fs import adapt_from_newfs widget = tests.MockObject() with self.assertRaises(FileNotFoundError): open_folder(widget, File('/non-existing')) folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_REAL) myfolder = folder.folder('test') myfolder.touch() entry = ApplicationManager().get_fallback_filebrowser() open_folder(widget, myfolder) self.assertEqual(self.calls[-1], (widget, entry, adapt_from_newfs(myfolder), None))
def open_file(widget, file, mimetype=None, callback=None): '''Open a file or folder @param widget: parent for new dialogs, C{Gtk.Widget} or C{None} @param file: a L{File} or L{Folder} object @param mimetype: optionally specify the mimetype to force a specific application to open this file @param callback: callback function to be passed on to L{Application.spawn()} (if the application supports a callback, otherwise it is ignored silently) @raises FileNotFoundError: if C{file} does not exist @raises NoApplicationFoundError: if a specific mimetype was given, but no default application is known for this mimetype (will not use fallback in this case - fallback would ignore the specified mimetype) ''' logger.debug('open_file(%s, %s)', file, mimetype) file = adapt_from_newfs(file) assert isinstance(file, (File, Dir)) if isinstance(file, (File)) and file.isdir(): file = Dir(file.path) if not file.exists(): raise FileNotFoundError(file) if isinstance(file, File): # File manager = ApplicationManager() if mimetype is None: entry = manager.get_default_application(file.get_mimetype()) if entry is not None: _open_with(widget, entry, file, callback) else: _open_with_filebrowser(widget, file, callback) else: entry = manager.get_default_application(mimetype) if entry is not None: _open_with(widget, entry, file, callback) else: raise NoApplicationFoundError('No Application found for: %s' % mimetype) # Do not go to fallback, we can not force # mimetype for fallback else: # Dir _open_with_filebrowser(widget, file, callback)
def new_from_dir(klass, dir): '''Constructor to create a notebook based on a specific file system location. Since the file system is an external resource, this method will return unique objects per location and keep (weak) references for re-use. @param dir: a L{Dir} object @returns: a L{Notebook} object ''' dir = adapt_from_newfs(dir) assert isinstance(dir, Dir) nb = _NOTEBOOK_CACHE.get(dir.uri) if nb: return nb from .index import Index from .layout import FilesLayout config = NotebookConfig(dir.file('notebook.zim')) if config['Notebook']['shared']: cache_dir = _cache_dir_for_dir(dir) else: cache_dir = dir.subdir('.zim') cache_dir.touch() if not (cache_dir.exists() and _iswritable(cache_dir)): cache_dir = _cache_dir_for_dir(dir) folder = LocalFolder(dir.path) if config['Notebook']['notebook_layout'] == 'files': layout = FilesLayout(folder, config['Notebook']['endofline'], config['Notebook']['default_file_format'], config['Notebook']['default_file_extension']) else: raise ValueError('Unkonwn notebook layout: %s' % config['Notebook']['notebook_layout']) cache_dir.touch() # must exist for index to work index = Index(cache_dir.file('index.db').path, layout) nb = klass(cache_dir, config, folder, layout, index) _NOTEBOOK_CACHE[dir.uri] = nb return nb
def __init__(self, file): '''Constructor @param file: a L{File} object for the template file ''' file = adapt_from_newfs(file) self.filename = file.path try: self.parts = TemplateParser().parse(file.read()) except Exception as error: error.parser_file = file raise self.resources_dir = None if '.' in file.basename: name, ext = file.basename.rsplit('.') rdir = file.dir.subdir(name) if rdir.exists(): self.resources_dir = rdir self._resources_cache = {}