2
0

__init__.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Format management.
  5. Creating new formats
  6. --------------------
  7. A new format named 'foo-bar' corresponds to Python module
  8. 'tracetool/format/foo_bar.py'.
  9. A format module should provide a docstring, whose first non-empty line will be
  10. considered its short description.
  11. All formats must generate their contents through the 'tracetool.out' routine.
  12. Format functions
  13. ----------------
  14. ======== ==================================================================
  15. Function Description
  16. ======== ==================================================================
  17. generate Called to generate a format-specific file.
  18. ======== ==================================================================
  19. """
  20. __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
  21. __copyright__ = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>"
  22. __license__ = "GPL version 2 or (at your option) any later version"
  23. __maintainer__ = "Stefan Hajnoczi"
  24. __email__ = "stefanha@linux.vnet.ibm.com"
  25. import os
  26. import tracetool
  27. def get_list():
  28. """Get a list of (name, description) pairs."""
  29. res = []
  30. modnames = []
  31. for filename in os.listdir(tracetool.format.__path__[0]):
  32. if filename.endswith('.py') and filename != '__init__.py':
  33. modnames.append(filename.rsplit('.', 1)[0])
  34. for modname in sorted(modnames):
  35. module = tracetool.try_import("tracetool.format." + modname)
  36. # just in case; should never fail unless non-module files are put there
  37. if not module[0]:
  38. continue
  39. module = module[1]
  40. doc = module.__doc__
  41. if doc is None:
  42. doc = ""
  43. doc = doc.strip().split("\n")[0]
  44. name = modname.replace("_", "-")
  45. res.append((name, doc))
  46. return res
  47. def exists(name):
  48. """Return whether the given format exists."""
  49. if len(name) == 0:
  50. return False
  51. name = name.replace("-", "_")
  52. return tracetool.try_import("tracetool.format." + name)[1]
  53. def generate(events, format, backend):
  54. if not exists(format):
  55. raise ValueError("unknown format: %s" % format)
  56. format = format.replace("-", "_")
  57. func = tracetool.try_import("tracetool.format." + format,
  58. "generate")[1]
  59. if func is None:
  60. raise AttributeError("format has no 'generate': %s" % format)
  61. func(events, backend)