__init__.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. All the following functions are optional, and no output will be generated if
  15. they do not exist.
  16. ======== =======================================================================
  17. Function Description
  18. ======== =======================================================================
  19. begin Called to generate the format-specific file header.
  20. end Called to generate the format-specific file footer.
  21. nop Called to generate the per-event contents when the event is disabled or
  22. the selected backend is 'nop'.
  23. ======== =======================================================================
  24. """
  25. __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
  26. __copyright__ = "Copyright 2012, Lluís Vilanova <vilanova@ac.upc.edu>"
  27. __license__ = "GPL version 2 or (at your option) any later version"
  28. __maintainer__ = "Stefan Hajnoczi"
  29. __email__ = "stefanha@linux.vnet.ibm.com"
  30. import os
  31. import tracetool
  32. def get_list():
  33. """Get a list of (name, description) pairs."""
  34. res = []
  35. modnames = []
  36. for filename in os.listdir(tracetool.format.__path__[0]):
  37. if filename.endswith('.py') and filename != '__init__.py':
  38. modnames.append(filename.rsplit('.', 1)[0])
  39. for modname in modnames:
  40. module = tracetool.try_import("tracetool.format." + modname)
  41. # just in case; should never fail unless non-module files are put there
  42. if not module[0]:
  43. continue
  44. module = module[1]
  45. doc = module.__doc__
  46. if doc is None:
  47. doc = ""
  48. doc = doc.strip().split("\n")[0]
  49. name = modname.replace("_", "-")
  50. res.append((name, doc))
  51. return res
  52. def exists(name):
  53. """Return whether the given format exists."""
  54. if len(name) == 0:
  55. return False
  56. name = name.replace("-", "_")
  57. return tracetool.try_import("tracetool.format." + name)[1]
  58. def _empty(events):
  59. pass
  60. def generate_begin(name, events):
  61. """Generate the header of the format-specific file."""
  62. if not exists(name):
  63. raise ValueError("unknown format: %s" % name)
  64. name = name.replace("-", "_")
  65. func = tracetool.try_import("tracetool.format." + name,
  66. "begin", _empty)[1]
  67. func(events)
  68. def generate_end(name, events):
  69. """Generate the footer of the format-specific file."""
  70. if not exists(name):
  71. raise ValueError("unknown format: %s" % name)
  72. name = name.replace("-", "_")
  73. func = tracetool.try_import("tracetool.format." + name,
  74. "end", _empty)[1]
  75. func(events)