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.write(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 DitaaGenerator(ImageGeneratorClass): uses_log_file = False object_type = "shaape" scriptname = "shaape.dia" imagename = "shaape.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.write(text) # Call GraphViz try: dot = Application(dotcmd) dot.run(("-o", 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()
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()
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): # Write to tmp file self.diagfile.write(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()
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()
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): # Write to tmp file self.diagfile.write(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()
class LogContext(object): '''Context to log errors and warnings to a log file''' def __init__(self): names = ['zim.export', 'zim.templates', 'zim.formats'] level = logging.INFO self.logger = logging.getLogger('zim') self.level = level self.file = TmpFile(basename='export-log.txt', unique=False, persistent=True) self.file.remove() # clean up previous run self.handler = LogHandler(self.file.path) self.handler.setLevel(self.level) self.handler.addFilter(LogFilter(names)) self.handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s') ) def __enter__(self): #~ self._old_level = self.logger.getEffectiveLevel() #~ if self._old_level > self.level: #~ self.logger.setLevel(self.level) self.logger.addHandler(self.handler) def __exit__(self, exc_type, exc_val, exc_tb): self.logger.removeHandler(self.handler) #~ self.logger.setLevel(self._old_level) self.handler.close() return False # re-raises error
class LogContext(object): '''Context to log errors and warnings to a log file''' def __init__(self): names = ['zim.export', 'zim.templates', 'zim.formats'] level = logging.INFO self.logger = logging.getLogger('zim') self.level = level self.file = TmpFile(basename='export-log.txt', unique=False, persistent=True) self.file.remove() # clean up previous run self.handler = LogHandler(self.file.path) self.handler.setLevel(self.level) self.handler.addFilter(LogFilter(names)) self.handler.setFormatter( logging.Formatter('%(levelname)s: %(message)s')) def __enter__(self): #~ self._old_level = self.logger.getEffectiveLevel() #~ if self._old_level > self.level: #~ self.logger.setLevel(self.level) self.logger.addHandler(self.handler) def __exit__(self, exc_type, exc_val, exc_tb): self.logger.removeHandler(self.handler) #~ self.logger.setLevel(self._old_level) self.handler.close() return False # re-raises error
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()
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()
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()
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()
class PlantumlGenerator(ImageGeneratorClass): def __init__(self, plugin, notebook, page): ImageGeneratorClass.__init__(self, plugin, notebook, page) self.dotfile = TmpFile('plantuml.pu') self.dotfile.touch() self.pngfile = File(self.dotfile.path[:-3] + '.png') # len('.pu') == 3 def generate_image(self, text): # Write to tmp file self.dotfile.write(text) # Call GraphViz try: dot = Application(dotcmd) dot.run((self.dotfile, )) except ApplicationError as e: print(e) return None, None # Sorry, no log else: return self.pngfile, None def cleanup(self): self.dotfile.remove() self.pngfile.remove()