def testImportPageDialog(self): '''Test ImportPageDialog''' tmp_dir = self.create_tmp_dir('testImportPageDialog') file = File((tmp_dir, 'import_page.txt')) file.write('test 123\n') self.assertTrue(file.exists()) dialog = zim.gui.ImportPageDialog(self.ui) dialog.set_file(file)
def testImportPageDialog(self): '''Test ImportPageDialog''' tmp_dir = self.create_tmp_dir('testImportPageDialog') file = File((tmp_dir, 'import_page.txt')) file.write('test 123\n') self.assertTrue(file.exists()) dialog = zim.gui.ImportPageDialog(self.ui) dialog.set_file(file)
def runTest(self): tmp_dir = self.create_tmp_dir() file = File((tmp_dir, 'test.txt')) file.write('test 123') self.assertTrue(file.exists()) dialog = FileDialog(None, 'Test') dialog.set_file(file)
def runTest(self): tmp_dir = self.create_tmp_dir() file = File((tmp_dir, 'test.txt')) file.write('test 123') self.assertTrue(file.exists()) dialog = FileDialog(None, 'Test') dialog.set_file(file)
def testAttachFileDialog(self): '''Test AttachFileDialog''' tmp_dir = self.create_tmp_dir('testAttachFileDialog') file = File((tmp_dir, 'file_to_be_attached')) file.write('Test 1 2 3\n') newfile = File((tmp_dir, 'attachments', 'Test', 'foo', 'file_to_be_attached')) self.assertTrue(file.exists()) self.assertFalse(newfile.exists()) dialog = zim.gui.AttachFileDialog(self.ui, path=Path('Test:foo')) dialog.set_file(file)
def testAttachFileDialog(self): '''Test AttachFileDialog''' tmp_dir = self.create_tmp_dir('testAttachFileDialog') file = File((tmp_dir, 'file_to_be_attached')) file.write('Test 1 2 3\n') newfile = File( (tmp_dir, 'attachments', 'Test', 'foo', 'file_to_be_attached')) self.assertTrue(file.exists()) self.assertFalse(newfile.exists()) dialog = zim.gui.AttachFileDialog(self.ui, path=Path('Test:foo')) dialog.set_file(file)
def generate_image(self, text): (version, text) = self.extract_version(text) text = ''.join(text) #~ print '>>>%s<<<' % text # Write to tmp file using the template for the header / footer scorefile = self.scorefile lines = [] self.template.process( lines, { 'score': text, 'version': version or '', 'include_header': self.include_header or '', 'include_footer': self.include_footer or '', }) scorefile.writelines(lines) #~ print '>>>%s<<<' % scorefile.read() # Call convert-ly to convert document of current version of # Lilypond. clogfile = File(scorefile.path[:-3] + '-convertly.log') # len('.ly) == 3 try: convertly = Application(convertly_cmd) convertly.run((scorefile.basename, ), cwd=scorefile.dir) except ApplicationError: clogfile.write('convert-ly failed.\n') return None, clogfile # Call lilypond to generate image. logfile = File(scorefile.path[:-3] + '.log') # len('.ly') == 3 try: lilypond = Application(lilypond_cmd) lilypond.run(( '-dlog-file=' + logfile.basename[:-4], scorefile.basename, ), cwd=scorefile.dir) except ApplicationError: # log should have details of failure return None, logfile pngfile = File(scorefile.path[:-3] + '.png') # len('.ly') == 3 return pngfile, logfile
def generate_image(self, text): (version, text) = self.extract_version(text) text = ''.join(text) #~ print '>>>%s<<<' % text # Write to tmp file using the template for the header / footer scorefile = self.scorefile lines = [] self.template.process(lines, { 'score': text, 'version': version or '', 'include_header': self.include_header or '', 'include_footer': self.include_footer or '', } ) scorefile.writelines(lines) #~ print '>>>%s<<<' % scorefile.read() # Call convert-ly to convert document of current version of # Lilypond. clogfile = File(scorefile.path[:-3] + '-convertly.log') # len('.ly) == 3 try: convertly = Application(convertly_cmd) convertly.run((scorefile.basename,), cwd=scorefile.dir) except ApplicationError: clogfile.write('convert-ly failed.\n') return None, clogfile # Call lilypond to generate image. logfile = File(scorefile.path[:-3] + '.log') # len('.ly') == 3 try: lilypond = Application(lilypond_cmd) lilypond.run(('-dlog-file=' + logfile.basename[:-4], scorefile.basename,), cwd=scorefile.dir) except ApplicationError: # log should have details of failure return None, logfile pngfile = File(scorefile.path[:-3] + '.png') # len('.ly') == 3 return pngfile, logfile
def testAttachFileDialog(self): '''Test AttachFileDialog''' tmp_dir = self.create_tmp_dir('testAttachFileDialog') file = File((tmp_dir, 'file_to_be_attached')) file.write('Test 1 2 3\n') newfile = File( (tmp_dir, 'attachments', 'Test', 'foo', 'file_to_be_attached')) self.assertTrue(file.exists()) self.assertFalse(newfile.exists()) mainwindow = tests.MockObject() mainwindow.pageview = tests.MockObject() mainwindow.ui = self.ui dialog = zim.gui.AttachFileDialog(mainwindow, self.ui.notebook, path=Path('Test:foo')) dialog.set_file(file) tests.gtk_process_events() dialog.assert_response_ok() self.assertTrue(file.exists()) # No move or delete happened self.assertTrue(newfile.exists()) self.assertTrue(newfile.compare(file))
class ConfigFile(object): '''Container object for a config file Maps to a "base" file in the home folder, used to write new values, and one or more default files, e.g. in C{/usr/share/zim}, which are the fallback to get default values @ivar file: the underlying file object for the base config file in the home folder @note: this class implement similar API to the L{File} class but is explicitly not a sub-class of L{File} because config files should typically not be moved, renamed, etc. It just implements the reading and writing methods. ''' def __init__(self, path, file=None): '''Constructor @param path: either basename as string or tuple with relative path, is resolved relative to the default config dir for zim. @param file: optional argument for some special case to override the base file in the home folder. ''' if isinstance(path, basestring): path = (path,) self._path = tuple(path) if file: self.file = file else: self.file = File((XDG_CONFIG_HOME, 'zim') + self._path) def __repr__(self): return '<%s: %s>' % (self.__class__.__name__, self.file.path) def __eq__(self, other): return isinstance(other, ConfigFile) \ and other._path == self._path \ and other.file == self.file @property def basename(self): return self.file.basename def default_files(self): '''Generator that yields default config files (read-only) to use instead of the standard file when it is still empty. Typically only the first one is used. ''' for dir in config_dirs(): default = dir.file(self._path) if default.exists(): yield default def touch(self): '''Ensure the custom file in the home folder exists. Either by copying a default config file, or touching an empty file. Intended to be called before trying to edit the file with an external editor. ''' if not self.file.exists(): for file in self.default_files(): file.copyto(self.file) break else: self.file.touch() # create empty file def read(self, fail=False): '''Read the base file or first default file @param fail: if C{True} a L{FileNotFoundError} error is raised when neither the base file or a default file are found. If C{False} it will return C{''} for a non-existing file. @returns: file content as a string ''' try: return self.file.read() except FileNotFoundError: for file in self.default_files(): return file.read() else: if fail: raise else: return '' def readlines(self, fail=False): '''Read the base file or first default file @param fail: if C{True} a L{FileNotFoundError} error is raised when neither the base file or a default file are found. If C{False} it will return C{[]} for a non-existing file. @returns: file content as a list of lines ''' try: return self.file.readlines() except FileNotFoundError: for file in self.default_files(): return file.readlines() else: if fail: raise else: return [] # Not implemented: read_async and readlines_async def write(self, text): '''Write base file, see L{File.write()}''' self.file.write(text) def writelines(self, lines): '''Write base file, see L{File.writelines()}''' self.file.writelines(lines) def write_async(self, text, callback=None, data=None): '''Write base file async, see L{File.write_async()}''' return self.file.write_async(text, callback=callback, data=data) def writelines_async(self, lines, callback=None, data=None): '''Write base file async, see L{File.writelines_async()}''' return self.file.writelines_async(lines, callback=callback, data=data) def remove(self): '''Remove user file, leaves default files in place''' if self.file.exists(): return self.file.remove()