File: gst-xmlinspect.py

package info (click to toggle)
gnonlin 0.10.9-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,484 kB
  • ctags: 494
  • sloc: sh: 9,315; ansic: 5,503; perl: 1,401; python: 424; makefile: 155
file content (168 lines) | stat: -rw-r--r-- 4,861 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4

"""
examine all plugins and elements and output xml documentation for them
used as part of the plugin documentation build
"""

import sys
import os
import pygst
pygst.require('0.10')
import gst

INDENT_SIZE = 2

# all templates

PAD_TEMPLATE = """<caps>
  <name>%(name)s</name>
  <direction>%(direction)s</direction>
  <presence>%(presence)s</presence>
  <details>%(details)s</details>
</caps>"""

ELEMENT_TEMPLATE = """<element>
  <name>%(name)s</name>
  <longname>%(longname)s</longname>
  <class>%(class)s</class>
  <description>%(description)s</description>
  <author>%(author)s</author>
  <pads>
%(pads)s
  </pads>
</element>"""

PLUGIN_TEMPLATE = """<plugin>
  <name>%(name)s</name>
  <description>%(description)s</description>
  <filename>%(filename)s</filename>
  <basename>%(basename)s</basename>
  <version>%(version)s</version>
  <license>%(license)s</license>
  <source>%(source)s</source>
  <package>%(package)s</package>
  <origin>%(origin)s</origin>
  <elements>
%(elements)s
  </elements>
</plugin>"""

def xmlencode(line):
    """
    Replace &, <, and >
    """
    line = "&amp;".join(line.split("&"))
    line = "&lt;".join(line.split("<"))
    line = "&gt;".join(line.split(">"))
    return line

def get_offset(indent):
    return " " * INDENT_SIZE * indent

def output_pad_template(pt, indent=0):
    print  "PAD TEMPLATE", pt.name_template
    paddir = ("unknown","source","sink")
    padpres = ("always","sometimes","request")
    
    d = {
      'name':        xmlencode(pt.name_template),
      'direction':   xmlencode(paddir[pt.direction]),
      'presence':    xmlencode(padpres[pt.presence]),
      'details':     xmlencode(pt.static_caps.string),
    }
    block = PAD_TEMPLATE % d

    offset = get_offset(indent)
    return offset + ("\n" + offset).join(block.split("\n"))
    
def output_element_factory(elf, indent=0):
    print  "ELEMENT", elf.get_name()

    padsoutput = []
    padtemplates = elf.get_static_pad_templates()
    for padtemplate in padtemplates:
        padsoutput.append(output_pad_template(padtemplate, indent))

    d = {
        'name':        xmlencode(elf.get_name()),
        'longname':    xmlencode(elf.get_longname()),
        'class':       xmlencode(elf.get_klass()),
        'description': xmlencode(elf.get_description()),
        'author':      xmlencode(elf.get_author()),
        'pads': "\n".join(padsoutput),
    }
    block = ELEMENT_TEMPLATE % d

    offset = get_offset(indent)
    return offset + ("\n" + offset).join(block.split("\n"))

def output_plugin(plugin, indent=0):
    print "PLUGIN", plugin.get_name()
    version = plugin.get_version()
    
    elements = {}
    gst.debug('getting features for plugin %s' % plugin.get_name())
    registry = gst.registry_get_default()
    features = registry.get_feature_list_by_plugin(plugin.get_name())
    gst.debug('plugin %s has %d features' % (plugin.get_name(), len(features)))
    for feature in features:
        if isinstance(feature, gst.ElementFactory):
            elements[feature.get_name()] = feature
    #gst.debug("got features")
        
    elementsoutput = []
    keys = elements.keys()
    keys.sort()
    for name in keys:
        feature = elements[name]
        elementsoutput.append(output_element_factory(feature, indent + 2))

    filename = plugin.get_filename()
    basename = filename
    if basename:
        basename = os.path.basename(basename)
    d = {
        'name':        xmlencode(plugin.get_name()),
        'description': xmlencode(plugin.get_description()),
        'filename':    filename,
        'basename':    basename,
        'version':     version,
        'license':     xmlencode(plugin.get_license()),
        'source':      xmlencode(plugin.get_source()),
        'package':     xmlencode(plugin.get_package()),
        'origin':      xmlencode(plugin.get_origin()),
        'elements': "\n".join(elementsoutput),
    }
    block = PLUGIN_TEMPLATE % d
    
    offset = get_offset(indent)
    return offset + ("\n" + offset).join(block.split("\n"))

def main():
    if len(sys.argv) == 1:
        sys.stderr.write("Please specify a source module to inspect")
        sys.exit(1)
    source = sys.argv[1]

    if len(sys.argv) > 2:
        os.chdir(sys.argv[2])

    registry = gst.registry_get_default()
    all = registry.get_plugin_list()
    for plugin in all:
        gst.debug("inspecting plugin %s from source %s" % (
            plugin.get_name(), plugin.get_source()))
        # this skips gstcoreelements, with bin and pipeline
        if plugin.get_filename() is None:
            continue
        if plugin.get_source() != source:
            continue

        filename = "plugin-%s.xml" % plugin.get_name()
        handle = open(filename, "w")
        handle.write(output_plugin(plugin))
        handle.close()

main()