batch.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # This file is part of pybootchartgui.
  2. # pybootchartgui is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # pybootchartgui is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with pybootchartgui. If not, see <http://www.gnu.org/licenses/>.
  12. import cairo
  13. from . import draw
  14. from .draw import RenderOptions
  15. def render(writer, trace, app_options, filename):
  16. handlers = {
  17. "png": (lambda w, h: cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h), \
  18. lambda sfc: sfc.write_to_png(filename)),
  19. "pdf": (lambda w, h: cairo.PDFSurface(filename, w, h), lambda sfc: 0),
  20. "svg": (lambda w, h: cairo.SVGSurface(filename, w, h), lambda sfc: 0)
  21. }
  22. if app_options.format is None:
  23. fmt = filename.rsplit('.', 1)[1]
  24. else:
  25. fmt = app_options.format
  26. if not (fmt in handlers):
  27. writer.error ("Unknown format '%s'." % fmt)
  28. return 10
  29. make_surface, write_surface = handlers[fmt]
  30. options = RenderOptions (app_options)
  31. (w, h) = draw.extents (options, 1.0, trace)
  32. w = max (w, draw.MIN_IMG_W)
  33. surface = make_surface (w, h)
  34. ctx = cairo.Context (surface)
  35. draw.render (ctx, options, 1.0, trace)
  36. write_surface (surface)
  37. writer.status ("bootchart written to '%s'" % filename)