trace: [tracetool] Show list of frontends and backends sorted by name
[qemu/cris-port.git] / scripts / tracetool / format / __init__.py
blob2bbbba7011c61d4e6c6fed1660d17eca7d65e882
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 """
5 Format management.
8 Creating new formats
9 --------------------
11 A new format named 'foo-bar' corresponds to Python module
12 'tracetool/format/foo_bar.py'.
14 A format module should provide a docstring, whose first non-empty line will be
15 considered its short description.
17 All formats must generate their contents through the 'tracetool.out' routine.
20 Format functions
21 ----------------
23 All the following functions are optional, and no output will be generated if
24 they do not exist.
26 ======== =======================================================================
27 Function Description
28 ======== =======================================================================
29 begin Called to generate the format-specific file header.
30 end Called to generate the format-specific file footer.
31 nop Called to generate the per-event contents when the event is disabled or
32 the selected backend is 'nop'.
33 ======== =======================================================================
34 """
36 __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
37 __copyright__ = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>"
38 __license__ = "GPL version 2 or (at your option) any later version"
40 __maintainer__ = "Stefan Hajnoczi"
41 __email__ = "stefanha@linux.vnet.ibm.com"
44 import os
46 import tracetool
49 def get_list():
50 """Get a list of (name, description) pairs."""
51 res = []
52 modnames = []
53 for filename in os.listdir(tracetool.format.__path__[0]):
54 if filename.endswith('.py') and filename != '__init__.py':
55 modnames.append(filename.rsplit('.', 1)[0])
56 for modname in sorted(modnames):
57 module = tracetool.try_import("tracetool.format." + modname)
59 # just in case; should never fail unless non-module files are put there
60 if not module[0]:
61 continue
62 module = module[1]
64 doc = module.__doc__
65 if doc is None:
66 doc = ""
67 doc = doc.strip().split("\n")[0]
69 name = modname.replace("_", "-")
70 res.append((name, doc))
71 return res
74 def exists(name):
75 """Return whether the given format exists."""
76 if len(name) == 0:
77 return False
78 name = name.replace("-", "_")
79 return tracetool.try_import("tracetool.format." + name)[1]
82 def _empty(events):
83 pass
85 def generate_begin(name, events):
86 """Generate the header of the format-specific file."""
87 if not exists(name):
88 raise ValueError("unknown format: %s" % name)
90 name = name.replace("-", "_")
91 func = tracetool.try_import("tracetool.format." + name,
92 "begin", _empty)[1]
93 func(events)
95 def generate_end(name, events):
96 """Generate the footer of the format-specific file."""
97 if not exists(name):
98 raise ValueError("unknown format: %s" % name)
100 name = name.replace("-", "_")
101 func = tracetool.try_import("tracetool.format." + name,
102 "end", _empty)[1]
103 func(events)