2
0

__init__.py 2.3 KB

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