xref: /openbmc/qemu/scripts/tracetool/format/__init__.py (revision beafab86a3062489cffd58d6608e64df98e256f1)
1650ab98dSLluís Vilanova# -*- coding: utf-8 -*-
2650ab98dSLluís Vilanova
3650ab98dSLluís Vilanova"""
4650ab98dSLluís VilanovaFormat management.
5650ab98dSLluís Vilanova
6650ab98dSLluís Vilanova
7650ab98dSLluís VilanovaCreating new formats
8650ab98dSLluís Vilanova--------------------
9650ab98dSLluís Vilanova
10650ab98dSLluís VilanovaA new format named 'foo-bar' corresponds to Python module
11650ab98dSLluís Vilanova'tracetool/format/foo_bar.py'.
12650ab98dSLluís Vilanova
13650ab98dSLluís VilanovaA format module should provide a docstring, whose first non-empty line will be
14650ab98dSLluís Vilanovaconsidered its short description.
15650ab98dSLluís Vilanova
16650ab98dSLluís VilanovaAll formats must generate their contents through the 'tracetool.out' routine.
17650ab98dSLluís Vilanova
18650ab98dSLluís Vilanova
19650ab98dSLluís VilanovaFormat functions
20650ab98dSLluís Vilanova----------------
21650ab98dSLluís Vilanova
221dad2ce9SLluís Vilanova======== ==================================================================
23650ab98dSLluís VilanovaFunction Description
241dad2ce9SLluís Vilanova======== ==================================================================
251dad2ce9SLluís Vilanovagenerate Called to generate a format-specific file.
261dad2ce9SLluís Vilanova======== ==================================================================
271dad2ce9SLluís Vilanova
28650ab98dSLluís Vilanova"""
29650ab98dSLluís Vilanova
30650ab98dSLluís Vilanova__author__     = "Lluís Vilanova <vilanova@ac.upc.edu>"
31b160d7f8SLluís Vilanova__copyright__  = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>"
32650ab98dSLluís Vilanova__license__    = "GPL version 2 or (at your option) any later version"
33650ab98dSLluís Vilanova
34650ab98dSLluís Vilanova__maintainer__ = "Stefan Hajnoczi"
35*f892b494SPhilippe Mathieu-Daudé__email__      = "stefanha@redhat.com"
36650ab98dSLluís Vilanova
37650ab98dSLluís Vilanova
389943e0ecSStefan Hajnocziimport os
39650ab98dSLluís Vilanova
40650ab98dSLluís Vilanovaimport tracetool
41650ab98dSLluís Vilanova
42650ab98dSLluís Vilanova
43650ab98dSLluís Vilanovadef get_list():
44650ab98dSLluís Vilanova    """Get a list of (name, description) pairs."""
45650ab98dSLluís Vilanova    res = []
469943e0ecSStefan Hajnoczi    modnames = []
479943e0ecSStefan Hajnoczi    for filename in os.listdir(tracetool.format.__path__[0]):
489943e0ecSStefan Hajnoczi        if filename.endswith('.py') and filename != '__init__.py':
499943e0ecSStefan Hajnoczi            modnames.append(filename.rsplit('.', 1)[0])
50b160d7f8SLluís Vilanova    for modname in sorted(modnames):
51650ab98dSLluís Vilanova        module = tracetool.try_import("tracetool.format." + modname)
52650ab98dSLluís Vilanova
53650ab98dSLluís Vilanova        # just in case; should never fail unless non-module files are put there
54650ab98dSLluís Vilanova        if not module[0]:
55650ab98dSLluís Vilanova            continue
56650ab98dSLluís Vilanova        module = module[1]
57650ab98dSLluís Vilanova
58650ab98dSLluís Vilanova        doc = module.__doc__
59650ab98dSLluís Vilanova        if doc is None:
60650ab98dSLluís Vilanova            doc = ""
61650ab98dSLluís Vilanova        doc = doc.strip().split("\n")[0]
62650ab98dSLluís Vilanova
63650ab98dSLluís Vilanova        name = modname.replace("_", "-")
64650ab98dSLluís Vilanova        res.append((name, doc))
65650ab98dSLluís Vilanova    return res
66650ab98dSLluís Vilanova
67650ab98dSLluís Vilanova
68650ab98dSLluís Vilanovadef exists(name):
69650ab98dSLluís Vilanova    """Return whether the given format exists."""
70650ab98dSLluís Vilanova    if len(name) == 0:
71650ab98dSLluís Vilanova        return False
72650ab98dSLluís Vilanova    name = name.replace("-", "_")
73650ab98dSLluís Vilanova    return tracetool.try_import("tracetool.format." + name)[1]
74650ab98dSLluís Vilanova
75650ab98dSLluís Vilanova
7680dd5c49SDaniel P. Berrangedef generate(events, format, backend, group):
771dad2ce9SLluís Vilanova    if not exists(format):
781dad2ce9SLluís Vilanova        raise ValueError("unknown format: %s" % format)
791dad2ce9SLluís Vilanova    format = format.replace("-", "_")
801dad2ce9SLluís Vilanova    func = tracetool.try_import("tracetool.format." + format,
811dad2ce9SLluís Vilanova                                "generate")[1]
821dad2ce9SLluís Vilanova    if func is None:
831dad2ce9SLluís Vilanova        raise AttributeError("format has no 'generate': %s" % format)
8480dd5c49SDaniel P. Berrange    func(events, backend, group)
85