Merge remote-tracking branch 'remotes/kraxel/tags/ui-20210127-pull-request' into...
[qemu/ar7.git] / scripts / tracetool / format / __init__.py
blob2dc46f3dd93e4d9fdd58a3cfa77efe6100a9adbe
1 # -*- coding: utf-8 -*-
3 """
4 Format management.
7 Creating new formats
8 --------------------
10 A new format named 'foo-bar' corresponds to Python module
11 'tracetool/format/foo_bar.py'.
13 A format module should provide a docstring, whose first non-empty line will be
14 considered its short description.
16 All formats must generate their contents through the 'tracetool.out' routine.
19 Format functions
20 ----------------
22 ======== ==================================================================
23 Function Description
24 ======== ==================================================================
25 generate Called to generate a format-specific file.
26 ======== ==================================================================
28 """
30 __author__ = "Lluís Vilanova <vilanova@ac.upc.edu>"
31 __copyright__ = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>"
32 __license__ = "GPL version 2 or (at your option) any later version"
34 __maintainer__ = "Stefan Hajnoczi"
35 __email__ = "stefanha@redhat.com"
38 import os
40 import tracetool
43 def get_list():
44 """Get a list of (name, description) pairs."""
45 res = []
46 modnames = []
47 for filename in os.listdir(tracetool.format.__path__[0]):
48 if filename.endswith('.py') and filename != '__init__.py':
49 modnames.append(filename.rsplit('.', 1)[0])
50 for modname in sorted(modnames):
51 module = tracetool.try_import("tracetool.format." + modname)
53 # just in case; should never fail unless non-module files are put there
54 if not module[0]:
55 continue
56 module = module[1]
58 doc = module.__doc__
59 if doc is None:
60 doc = ""
61 doc = doc.strip().split("\n")[0]
63 name = modname.replace("_", "-")
64 res.append((name, doc))
65 return res
68 def exists(name):
69 """Return whether the given format exists."""
70 if len(name) == 0:
71 return False
72 name = name.replace("-", "_")
73 return tracetool.try_import("tracetool.format." + name)[1]
76 def generate(events, format, backend, group):
77 if not exists(format):
78 raise ValueError("unknown format: %s" % format)
79 format = format.replace("-", "_")
80 func = tracetool.try_import("tracetool.format." + format,
81 "generate")[1]
82 if func is None:
83 raise AttributeError("format has no 'generate': %s" % format)
84 func(events, backend, group)