示例#1
0
	def print_to_file(self, notebook, page):
		file = TmpFile('print-to-browser.html', persistent=True, unique=False)
		template = zim.templates.get_template('html', 'Print')
		template.set_linker(StaticLinker('html', notebook, page))
		html = template.process(notebook, page)
		file.writelines(html)
		return file
示例#2
0
class SequenceDiagramGenerator(ImageGeneratorClass):

    uses_log_file = False

    object_type = 'seqdiagram'
    scriptname = 'seqdiagram.diag'
    imagename = 'seqdiagram.png'

    def __init__(self, plugin):
        ImageGeneratorClass.__init__(self, plugin)
        self.diagfile = TmpFile(self.scriptname)
        self.diagfile.touch()
        self.pngfile = File(self.diagfile.path[:-5] +
                            '.png')  # len('.diag') == 5

    def generate_image(self, text):
        if isinstance(text, basestring):
            text = text.splitlines(True)

        # Write to tmp file
        self.diagfile.writelines(text)

        # Call seqdiag
        try:
            diag = Application(diagcmd)
            diag.run((self.pngfile, self.diagfile))
        except ApplicationError:
            return None, None  # Sorry, no log
        else:
            return self.pngfile, None

    def cleanup(self):
        self.diagfile.remove()
        self.pngfile.remove()
示例#3
0
class SequenceDiagramGenerator(ImageGeneratorClass):

	uses_log_file = False

	object_type = 'seqdiagram'
	scriptname = 'seqdiagram.diag'
	imagename = 'seqdiagram.png'

	def __init__(self, plugin):
		ImageGeneratorClass.__init__(self, plugin)
		self.diagfile = TmpFile(self.scriptname)
		self.diagfile.touch()
		self.pngfile = File(self.diagfile.path[:-5] + '.png') # len('.diag') == 5

	def generate_image(self, text):
		if isinstance(text, basestring):
			text = text.splitlines(True)

		# Write to tmp file
		self.diagfile.writelines(text)

		# Call seqdiag
		try:
			diag = Application(diagcmd)
			diag.run((self.pngfile, self.diagfile))
		except ApplicationError:
			return None, None # Sorry, no log
		else:
			return self.pngfile, None

	def cleanup(self):
		self.diagfile.remove()
		self.pngfile.remove()
示例#4
0
class DitaaGenerator(ImageGeneratorClass):

    uses_log_file = False

    object_type = 'ditaa'
    scriptname = 'ditaa.dia'
    imagename = 'ditaa.png'

    def __init__(self, plugin):
        ImageGeneratorClass.__init__(self, plugin)
        self.dotfile = TmpFile(self.scriptname)
        self.dotfile.touch()
        self.pngfile = File(self.dotfile.path[:-4] +
                            '.png')  # len('.dot') == 4

    def generate_image(self, text):
        if isinstance(text, basestring):
            text = text.splitlines(True)

        # Write to tmp file
        self.dotfile.writelines(text)

        # Call GraphViz
        try:
            dot = Application(dotcmd)
            dot.run((self.dotfile, '-o', self.pngfile))
        except ApplicationError:
            return None, None  # Sorry, no log
        else:
            return self.pngfile, None

    def cleanup(self):
        self.dotfile.remove()
        self.pngfile.remove()
示例#5
0
class PlantumlGenerator(ImageGeneratorClass):
    def __init__(self, plugin, notebook, page):
        ImageGeneratorClass.__init__(self, plugin, notebook, page)
        self.dotfile = TmpFile('umldiagram.puml')
        self.dotfile.touch()
        self.pngfile = File(self.dotfile.path[:-5] +
                            '.png')  # len('.puml') == 5

    def generate_image(self, text):
        # Write to tmp file
        self.dotfile.writelines(text)

        # Call PlantUML
        try:
            dot = Application(dotcmd)
            dot.run((self.pngfile, self.dotfile))
        except ApplicationError:
            return None, None  # Sorry, no log
        else:
            if self.pngfile.exists():
                return self.pngfile, None
            else:
                # When supplying a dot file with a syntax error, the dot command
                # doesn't return an error code (so we don't raise
                # ApplicationError), but we still don't have a png file to
                # return, so return None.
                return None, None

    def cleanup(self):
        self.dotfile.remove()
        self.pngfile.remove()
示例#6
0
class DiagramGenerator(object):

	# TODO: generic base class for image generators

	type = 'diagram'
	basename = 'diagram.dot'

	def __init__(self):
		self.dotfile = TmpFile('diagram-editor.dot')
		self.dotfile.touch()
		self.pngfile = File(self.dotfile.path[:-4] + '.png') # len('.dot') == 4

	def generate_image(self, text):
		if isinstance(text, basestring):
			text = text.splitlines(True)

		# Write to tmp file
		self.dotfile.writelines(text)

		# Call GraphViz
		dot = Application(dotcmd)
		dot.run((self.pngfile, self.dotfile))

		return self.pngfile, None

	def cleanup(self):
		self.dotfile.remove()
		self.pngfile.remove()
示例#7
0
class PlantumlGenerator(ImageGeneratorClass):

	uses_log_file = False

	object_type = 'plantuml'
	scriptname = 'plantuml.pu'
	imagename = 'plantuml.png'

	def __init__(self, plugin):
		ImageGeneratorClass.__init__(self, plugin)
		self.dotfile = TmpFile(self.scriptname)
		self.dotfile.touch()
		self.pngfile = File(self.dotfile.path[:-3] + '.png') # len('.pu') == 3

	def generate_image(self, text):
		if isinstance(text, basestring):
			text = text.splitlines(True)

		# Write to tmp file
		self.dotfile.writelines(text)

		# Call PlantUML
		try:
			dot = Application(dotcmd)
			dot.run(('', self.dotfile))
		except ApplicationError:
			return None, None # Sorry, no log
		else:
			return self.pngfile, None

	def cleanup(self):
		self.dotfile.remove()
		self.pngfile.remove()
示例#8
0
class DiagramGenerator(ImageGeneratorClass):

    uses_log_file = False

    type = 'diagram'
    scriptname = 'diagram.dot'
    imagename = 'diagram.png'

    def __init__(self):
        self.dotfile = TmpFile(self.scriptname)
        self.dotfile.touch()
        self.pngfile = File(self.dotfile.path[:-4] + '.png') # len('.dot') == 4

    def generate_image(self, text):
        if isinstance(text, basestring):
            text = text.splitlines(True)

        # Write to tmp file
        self.dotfile.writelines(text)

        # Call GraphViz
        try:
            dot = Application(dotcmd)
            dot.run((self.pngfile, self.dotfile))
        except ApplicationError:
            return None, None # Sorry, no log
        else:
            return self.pngfile, None

    def cleanup(self):
        self.dotfile.remove()
        self.pngfile.remove()
示例#9
0
class DitaaGenerator(ImageGeneratorClass):

	uses_log_file = False

	object_type = 'ditaa'
	scriptname = 'ditaa.dia'
	imagename = 'ditaa.png'

	def __init__(self, plugin):
		ImageGeneratorClass.__init__(self, plugin)
		self.dotfile = TmpFile(self.scriptname)
		self.dotfile.touch()
		self.pngfile = File(self.dotfile.path[:-4] + '.png') # len('.dot') == 4

	def generate_image(self, text):

		# Write to tmp file
		self.dotfile.writelines(text)

		# Call GraphViz
		try:
			dot = Application(dotcmd)
			dot.run((self.dotfile, '-o', self.pngfile))
		except ApplicationError:
			return None, None # Sorry, no log
		else:
			return self.pngfile, None

	def cleanup(self):
		self.dotfile.remove()
		self.pngfile.remove()
class EquationGenerator(ImageGeneratorClass):
    def __init__(self, plugin, notebook, page):
        ImageGeneratorClass.__init__(self, plugin, notebook, page)
        self.preferences = plugin.preferences
        self.template = get_template('plugins', 'equationeditor.tex')
        self.texfile = TmpFile('equation.tex')

    def generate_image(self, text):

        # Filter out empty lines, not allowed in latex equation blocks
        if isinstance(text, str):
            text = text.splitlines(True)
        text = (line for line in text if line and not line.isspace())
        text = ''.join(text)
        #~ print('>>>%s<<<' % text)

        # Write to tmp file using the template for the header / footer
        lines = []
        self.template.process(
            lines, {
                'equation': text,
                'font_size': self.preferences['font_size'],
                'dark_mode': self.preferences['dark_mode']
            })
        self.texfile.writelines(lines)
        #~ print('>>>%s<<<' % self.texfile.read())

        # Call latex
        logfile = File(self.texfile.path[:-4] + '.log')  # len('.tex') == 4
        #~ print(">>>", self.texfile, logfile)
        try:
            latex = Application('%s -no-shell-escape -halt-on-error' %
                                (latexcmd))
            latex.run((self.texfile.basename, ), cwd=self.texfile.dir)
        except ApplicationError:
            # log should have details of failure
            return None, logfile

        # Call dvipng
        dvifile = File(self.texfile.path[:-4] + '.dvi')  # len('.tex') == 4
        pngfile = File(self.texfile.path[:-4] + '.png')  # len('.tex') == 4
        dvipng = Application('%s -q -bg Transparent -T tight -D %s -o' %
                             (dvipngcmd, self.preferences['output_dpi']))
        dvipng.run((pngfile, dvifile))  # output, input
        # No try .. except here - should never fail
        # TODO dvipng can start processing before latex finished - can we win speed there ?

        return pngfile, logfile

    def cleanup(self):
        path = self.texfile.path
        for path in glob.glob(path[:-4] + '.*'):
            File(path).remove()
示例#11
0
class EquationGenerator(ImageGeneratorClass):

	object_type = 'equation'
	scriptname = 'equation.tex'
	imagename = 'equation.png'

	def __init__(self, plugin):
		ImageGeneratorClass.__init__(self, plugin)
		self.template = get_template('plugins', 'equationeditor.tex')
		self.texfile = TmpFile(self.scriptname)

	def generate_image(self, text):

		# Filter out empty lines, not allowed in latex equation blocks
		if isinstance(text, basestring):
			text = text.splitlines(True)
		text = (line for line in text if line and not line.isspace())
		text = ''.join(text)
		#~ print '>>>%s<<<' % text

		# Write to tmp file using the template for the header / footer
		lines = []
		self.template.process(lines, {'equation': text})
		self.texfile.writelines(lines)
		#~ print '>>>%s<<<' % self.texfile.read()

		# Call latex
		logfile = File(self.texfile.path[:-4] + '.log') # len('.tex') == 4
		#~ print ">>>", self.texfile, logfile
		try:
			latex = Application(latexcmd)
			latex.run((self.texfile.basename,), cwd=self.texfile.dir)
		except ApplicationError:
			# log should have details of failure
			return None, logfile

		# Call dvipng
		dvifile = File(self.texfile.path[:-4] + '.dvi') # len('.tex') == 4
		pngfile = File(self.texfile.path[:-4] + '.png') # len('.tex') == 4
		dvipng = Application(dvipngcmd)
		dvipng.run((pngfile, dvifile)) # output, input
			# No try .. except here - should never fail
		# TODO dvipng can start processing before latex finished - can we win speed there ?

		return pngfile, logfile

	def cleanup(self):
		path = self.texfile.path
		for path in glob.glob(path[:-4]+'.*'):
			File(path).remove()
示例#12
0
文件: __init__.py 项目: gdw2/zim
	def show_side_by_side(self):
		file = self._get_file()
		versions = self.versionlist.get_versions()
		if not (file and versions):
			raise AssertionError

		files = map(lambda v: self._get_tmp_file(file, v), versions)
		if len(files) == 1:
			tmp = TmpFile(file.basename + '--CURRENT', persistent=True)
				# need to be persistent, else it is cleaned up before application spawned
			tmp.writelines(file.readlines())
			files.insert(0, tmp)

		self._side_by_side_app.spawn(files)
    def show_side_by_side(self):
        file = self._get_file()
        versions = self.versionlist.get_versions()
        if not (file and versions):
            raise AssertionError

        files = [self._get_tmp_file(file, v) for v in versions]
        if len(files) == 1:
            tmp = TmpFile(file.basename + '--CURRENT', persistent=True)
            # need to be persistent, else it is cleaned up before application spawned
            tmp.writelines(file.readlines())
            files.insert(0, tmp)

        self._side_by_side_app.spawn(files)
示例#14
0
class EquationGenerator(ImageGeneratorClass):

    object_type = 'equation'
    scriptname = 'equation.tex'
    imagename = 'equation.png'

    def __init__(self, plugin):
        ImageGeneratorClass.__init__(self, plugin)
        self.template = get_template('plugins', 'equationeditor.tex')
        self.texfile = TmpFile(self.scriptname)

    def generate_image(self, text):

        # Filter out empty lines, not allowed in latex equation blocks
        if isinstance(text, str):
            text = text.splitlines(True)
        text = (line for line in text if line and not line.isspace())
        text = ''.join(text)
        #~ print('>>>%s<<<' % text)

        # Write to tmp file using the template for the header / footer
        lines = []
        self.template.process(lines, {'equation': text})
        self.texfile.writelines(lines)
        #~ print('>>>%s<<<' % self.texfile.read())

        # Call latex
        logfile = File(self.texfile.path[:-4] + '.log')  # len('.tex') == 4
        #~ print(">>>", self.texfile, logfile)
        try:
            latex = Application(latexcmd)
            latex.run((self.texfile.basename, ), cwd=self.texfile.dir)
        except ApplicationError:
            # log should have details of failure
            return None, logfile

        # Call dvipng
        dvifile = File(self.texfile.path[:-4] + '.dvi')  # len('.tex') == 4
        pngfile = File(self.texfile.path[:-4] + '.png')  # len('.tex') == 4
        dvipng = Application(dvipngcmd)
        dvipng.run((pngfile, dvifile))  # output, input
        # No try .. except here - should never fail
        # TODO dvipng can start processing before latex finished - can we win speed there ?

        return pngfile, logfile

    def cleanup(self):
        path = self.texfile.path
        for path in glob.glob(path[:-4] + '.*'):
            File(path).remove()
示例#15
0
    def print_to_file(self, notebook, page):
        file = TmpFile('print-to-browser.html', persistent=True, unique=False)
        template = zim.templates.get_template('html', 'Print')

        linker_factory = partial(StaticExportLinker, notebook,
                                 template.resources_dir)
        dumper_factory = zim.formats.get_format('html').Dumper  # XXX
        context = ExportTemplateContext(notebook, linker_factory,
                                        dumper_factory, page.basename, [page])

        lines = []
        template.process(lines, context)
        file.writelines(lines)
        return file
示例#16
0
	def print_to_file(self, notebook, page):
		file = TmpFile('print-to-browser.html', persistent=True, unique=False)
		template = zim.templates.get_template('html', 'Print')

		linker_factory = partial(StaticExportLinker, notebook, template.resources_dir)
		dumper_factory = zim.formats.get_format('html').Dumper # XXX
		context = ExportTemplateContext(
			notebook, linker_factory, dumper_factory,
			page.basename, [page]
		)

		lines = []
		template.process(lines, context)
		file.writelines(lines)
		return file
示例#17
0
class DiagramGenerator(ImageGeneratorClass):

    uses_log_file = False

    object_type = 'diagram'
    scriptname = 'diagram.dot'
    imagename = 'diagram.png'

    def __init__(self, plugin):
        ImageGeneratorClass.__init__(self, plugin)
        self.dotfile = TmpFile(self.scriptname)
        self.dotfile.touch()
        self.pngfile = File(self.dotfile.path[:-4] +
                            '.png')  # len('.dot') == 4

    def generate_image(self, text):
        if isinstance(text, basestring):
            text = text.splitlines(True)

        # Write to tmp file
        self.dotfile.writelines(text)

        # Call GraphViz
        try:
            dot = Application(dotcmd)
            dot.run((self.pngfile, self.dotfile))
        except ApplicationError:
            return None, None  # Sorry, no log
        else:
            if self.pngfile.exists():
                return self.pngfile, None
            else:
                # When supplying a dot file with a syntax error, the dot command
                # doesn't return an error code (so we don't raise
                # ApplicationError), but we still don't have a png file to
                # return, so return None.
                return None, None

    def cleanup(self):
        self.dotfile.remove()
        self.pngfile.remove()
示例#18
0
    def print_to_file(self, page):
        # FIXME - HACK - dump and parse as wiki first to work
        # around glitches in pageview parsetree dumper
        # main visibility when copy pasting bullet lists
        # Same hack in gui clipboard code
        from zim.notebook import Path, Page
        from zim.formats import get_format
        parsetree = page.get_parsetree()
        dumper = get_format('wiki').Dumper()
        text = ''.join( dumper.dump(parsetree) ).encode('utf-8')
        parser = get_format('wiki').Parser()
        parsetree = parser.parse(text)
        page = Page(Path(page.name), parsetree=parsetree)
        #--

        file = TmpFile('print-to-browser.html', persistent=True, unique=False)
        template = zim.templates.get_template('html', 'Print')
        template.set_linker(StaticLinker('html', self.ui.notebook, page))
        html = template.process(self.ui.notebook, page)
        file.writelines(html)
        return file
示例#19
0
文件: diagrameditor.py 项目: gdw2/zim
class DiagramGenerator(ImageGeneratorClass):

	uses_log_file = False

	object_type = 'diagram'
	scriptname = 'diagram.dot'
	imagename = 'diagram.png'

	def __init__(self, plugin):
		ImageGeneratorClass.__init__(self, plugin)
		self.dotfile = TmpFile(self.scriptname)
		self.dotfile.touch()
		self.pngfile = File(self.dotfile.path[:-4] + '.png') # len('.dot') == 4

	def generate_image(self, text):
		if isinstance(text, basestring):
			text = text.splitlines(True)

		# Write to tmp file
		self.dotfile.writelines(text)

		# Call GraphViz
		try:
			dot = Application(dotcmd)
			dot.run((self.pngfile, self.dotfile))
		except ApplicationError:
			return None, None # Sorry, no log
		else:
			if self.pngfile.exists():
				return self.pngfile, None
			else:
				# When supplying a dot file with a syntax error, the dot command
				# doesn't return an error code (so we don't raise
				# ApplicationError), but we still don't have a png file to
				# return, so return None.
				return None, None

	def cleanup(self):
		self.dotfile.remove()
		self.pngfile.remove()
示例#20
0
class CustomToolDict(DesktopEntryDict):
	'''This is a specialized desktop entry type that is used for
	custom tools for the "Tools" menu in zim. It uses a non-standard
	Exec spec with zim specific escapes for "X-Zim-ExecTool".

	The following fields are expanded:
		- C{%f} for source file as tmp file current page
		- C{%d} for attachment directory
		- C{%s} for real source file (if any)
		- C{%n} for notebook location (file or directory)
		- C{%D} for document root
		- C{%t} for selected text or word under cursor
		- C{%T} for the selected text including wiki formatting

	Other additional keys are:
		- C{X-Zim-ReadOnly} - boolean
		- C{X-Zim-ShowInToolBar} - boolean
		- C{X-Zim-ShowInContextMenu} - 'None', 'Text' or 'Page'

	These tools should always be executed with 3 arguments: notebook,
	page & pageview.
	'''

	_definitions = DesktopEntryDict._definitions + (
			('X-Zim-ExecTool',			String(None)),
			('X-Zim-ReadOnly',			Boolean(True)),
			('X-Zim-ShowInToolBar',		Boolean(False)),
			('X-Zim-ShowInContextMenu',	Choice(None, ('Text', 'Page'))),
			('X-Zim-ReplaceSelection',	Boolean(False)),
	)

	def isvalid(self):
		'''Check if all required fields are set.
		@returns: C{True} if all required fields are set
		'''
		entry = self['Desktop Entry']
		if entry.get('Type') == 'X-Zim-CustomTool' \
		and entry.get('Version') == 1.0 \
		and entry.get('Name') \
		and entry.get('X-Zim-ExecTool') \
		and not entry.get('X-Zim-ReadOnly') is None \
		and not entry.get('X-Zim-ShowInToolBar') is None \
		and 'X-Zim-ShowInContextMenu' in entry:
			return True
		else:
			logger.error('Invalid custom tool entry: %s %s', self.key, entry)
			return False

	def get_pixbuf(self, size):
		pixbuf = DesktopEntryDict.get_pixbuf(self, size)
		if pixbuf is None:
			pixbuf = gtk.Label().render_icon(gtk.STOCK_EXECUTE, size)
			# FIXME hack to use arbitrary widget to render icon
		return pixbuf

	@property
	def icon(self):
		return self['Desktop Entry'].get('Icon') or gtk.STOCK_EXECUTE
			# get('Icon', gtk.STOCK_EXECUTE) still returns empty string if key exists but no value

	@property
	def execcmd(self):
		return self['Desktop Entry']['X-Zim-ExecTool']

	@property
	def isreadonly(self):
		return self['Desktop Entry']['X-Zim-ReadOnly']

	@property
	def showintoolbar(self):
		return self['Desktop Entry']['X-Zim-ShowInToolBar']

	@property
	def showincontextmenu(self):
		return self['Desktop Entry']['X-Zim-ShowInContextMenu']

	@property
	def replaceselection(self):
		return self['Desktop Entry']['X-Zim-ReplaceSelection']

	def parse_exec(self, args=None):
		if not (isinstance(args, tuple) and len(args) == 3):
			raise AssertionError, 'Custom commands needs 3 arguments'
			# assert statement could be optimized away
		notebook, page, pageview = args

		cmd = split_quoted_strings(self['Desktop Entry']['X-Zim-ExecTool'])
		if '%f' in cmd:
			self._tmpfile = TmpFile('tmp-page-source.txt')
			self._tmpfile.writelines(page.dump('wiki'))
			cmd[cmd.index('%f')] = self._tmpfile.path

		if '%d' in cmd:
			dir = notebook.get_attachments_dir(page)
			if dir:
				cmd[cmd.index('%d')] = dir.path
			else:
				cmd[cmd.index('%d')] = ''

		if '%s' in cmd:
			if hasattr(page, 'source') and isinstance(page.source, File):
				cmd[cmd.index('%s')] = page.source.path
			else:
				cmd[cmd.index('%s')] = ''

		if '%p' in cmd:
			cmd[cmd.index('%p')] = page.name

		if '%n' in cmd:
			cmd[cmd.index('%n')] = File(notebook.uri).path

		if '%D' in cmd:
			dir = notebook.document_root
			if dir:
				cmd[cmd.index('%D')] = dir.path
			else:
				cmd[cmd.index('%D')] = ''

		if '%t' in cmd:
			text = pageview.get_selection() or pageview.get_word()
			cmd[cmd.index('%t')] = text or ''
			# FIXME - need to substitute this in arguments + url encoding

		if '%T' in cmd:
			text = pageview.get_selection(format='wiki') or pageview.get_word(format='wiki')
			cmd[cmd.index('%T')] = text or ''
			# FIXME - need to substitute this in arguments + url encoding

		return tuple(cmd)

	_cmd = parse_exec # To hook into Application.spawn and Application.run

	def run(self, args, cwd=None):
		self._tmpfile = None
		Application.run(self, args, cwd=cwd)
		if self._tmpfile:
			notebook, page, pageview = args
			page.parse('wiki', self._tmpfile.readlines())
			self._tmpfile = None

	def update(self, E=(), **F):
		self['Desktop Entry'].update(E, **F)

		# Set sane default for X-Zim-ShowInContextMenus
		if not (E and 'X-Zim-ShowInContextMenu' in E) \
		and not 'X-Zim-ShowInContextMenu' in F:
			cmd = split_quoted_strings(self['Desktop Entry']['X-Zim-ExecTool'])
			if any(c in cmd for c in ['%f', '%d', '%s']):
				context = 'Page'
			elif '%t' in cmd:
				context = 'Text'
			else:
				context = None
			self['Desktop Entry']['X-Zim-ShowInContextMenu'] = context
示例#21
0
文件: __init__.py 项目: gdw2/zim
	def _get_tmp_file(self, file, version):
		text = self.vcs.get_version(file, version)
		tmp = TmpFile(file.basename + '--REV%s' % version, persistent=True)
			# need to be persistent, else it is cleaned up before application spawned
		tmp.writelines(text)
		return tmp
示例#22
0
class CustomToolDict(DesktopEntryDict):
    '''This is a specialized desktop entry type that is used for
    custom tools for the "Tools" menu in zim. It uses a non-standard
    Exec spec with zim specific escapes for "X-Zim-ExecTool".

    The following fields are expanded:
        - C{%f} for source file as tmp file current page
        - C{%d} for attachment directory
        - C{%s} for real source file (if any)
        - C{%n} for notebook location (file or directory)
        - C{%D} for document root
        - C{%t} for selected text or word under cursor
        - C{%T} for the selected text including wiki formatting

    Other additional keys are:
        - C{X-Zim-ReadOnly} - boolean
        - C{X-Zim-ShowInToolBar} - boolean
        - C{X-Zim-ShowInContextMenu} - 'None', 'Text' or 'Page'

    These tools should always be executed with 3 arguments: notebook,
    page & pageview.
    '''

    _key_types = {
        'X-Zim-ExecTool': 'string',
        'X-Zim-ReadOnly': 'boolean',
        'X-Zim-ShowInToolBar': 'boolean',
    }
    _key_types.update(DesktopEntryDict._key_types)

    def isvalid(self):
        '''Check if all required fields are set.
        @returns: C{True} if all required fields are set
        '''
        entry = self['Desktop Entry']
        if entry.get('Type') == 'X-Zim-CustomTool' \
        and entry.get('Version') == 1.0 \
        and entry.get('Name') \
        and entry.get('X-Zim-ExecTool') \
        and not entry.get('X-Zim-ReadOnly') is None \
        and not entry.get('X-Zim-ShowInToolBar') is None \
        and 'X-Zim-ShowInContextMenu' in entry:
            return True
        else:
            logger.error('Invalid custom tool entry: %s %s', self.key, entry)
            return False

    def get_pixbuf(self, size):
        pixbuf = DesktopEntryDict.get_pixbuf(self, size)
        if pixbuf is None:
            pixbuf = gtk.Label().render_icon(gtk.STOCK_EXECUTE, size)
            # FIXME hack to use arbitrary widget to render icon
        return pixbuf

    @property
    def icon(self):
        return self['Desktop Entry'].get('Icon') or gtk.STOCK_EXECUTE
            # get('Icon', gtk.STOCK_EXECUTE) still returns empty string if key exists but no value

    @property
    def execcmd(self):
        return self['Desktop Entry']['X-Zim-ExecTool']

    @property
    def isreadonly(self):
        return self['Desktop Entry']['X-Zim-ReadOnly']

    @property
    def showintoolbar(self):
        return self['Desktop Entry']['X-Zim-ShowInToolBar']

    @property
    def showincontextmenu(self):
        return self['Desktop Entry']['X-Zim-ShowInContextMenu']

    def parse_exec(self, args=None):
        if not (isinstance(args, tuple) and len(args) == 3):
            raise AssertionError, 'Custom commands needs 3 arguments'
            # assert statement could be optimized away
        notebook, page, pageview = args

        cmd = split_quoted_strings(self['Desktop Entry']['X-Zim-ExecTool'])
        if '%f' in cmd:
            self._tmpfile = TmpFile('tmp-page-source.txt')
            self._tmpfile.writelines(page.dump('wiki'))
            cmd[cmd.index('%f')] = self._tmpfile.path

        if '%d' in cmd:
            dir = notebook.get_attachments_dir(page)
            if dir:
                cmd[cmd.index('%d')] = dir.path
            else:
                cmd[cmd.index('%d')] = ''

        if '%s' in cmd:
            if hasattr(page, 'source') and isinstance(page.source, File):
                cmd[cmd.index('%s')] = page.source.path
            else:
                cmd[cmd.index('%s')] = ''

        if '%n' in cmd:
            cmd[cmd.index('%n')] = File(notebook.uri).path

        if '%D' in cmd:
            dir = notebook.document_root
            if dir:
                cmd[cmd.index('%D')] = dir.path
            else:
                cmd[cmd.index('%D')] = ''

        if '%t' in cmd:
            text = pageview.get_selection() or pageview.get_word()
            cmd[cmd.index('%t')] = text or ''
            # FIXME - need to substitute this in arguments + url encoding

        if '%T' in cmd:
            text = pageview.get_selection(format='wiki') or pageview.get_word(format='wiki')
            cmd[cmd.index('%T')] = text or ''
            # FIXME - need to substitute this in arguments + url encoding

        return tuple(cmd)

    _cmd = parse_exec # To hook into Application.spawn and Application.run

    def run(self, args):
        self._tmpfile = None
        Application.run(self, args)
        if self._tmpfile:
            notebook, page, pageview = args
            page.parse('wiki', self._tmpfile.readlines())
            self._tmpfile = None

    def update(self, E=None, **F):
        self['Desktop Entry'].update(E, **F)

        # Set sane default for X-Zim-ShowInContextMenus
        if not (E and 'X-Zim-ShowInContextMenu' in E) \
        and not 'X-Zim-ShowInContextMenu' in F:
            cmd = split_quoted_strings(self['Desktop Entry']['X-Zim-ExecTool'])
            if any(c in cmd for c in ['%f', '%d', '%s']):
                context = 'Page'
            elif '%t' in cmd:
                context = 'Text'
            else:
                context = None
            self['Desktop Entry']['X-Zim-ShowInContextMenu'] = context
 def _get_tmp_file(self, file, version):
     text = self.vcs.cat(file, version)
     tmp = TmpFile(file.basename + '--REV%s' % version, persistent=True)
     # need to be persistent, else it is cleaned up before application spawned
     tmp.writelines(text)
     return tmp