summaryrefslogtreecommitdiff
path: root/giscanner/docmain.py
blob: e6ae8b604351dd245235e4a12a906563c9145de1 (plain)
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
# -*- Mode: Python -*-
# GObject-Introspection - a framework for introspecting GObject libraries
# Copyright (C) 2008-2011 Johan Dahlin
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

import os
import optparse

from .docbookwriter import DocBookWriter
from .docbookwriter import DocBookFormatterC
from .docbookwriter import DocBookFormatterPython
from .mallardwriter import MallardWriter
from .mallardwriter import MallardFormatterC
from .mallardwriter import MallardFormatterPython
from .transformer import Transformer

class GIDocGenerator(object):

    def parse(self, filename):
        if 'UNINSTALLED_INTROSPECTION_SRCDIR' in os.environ:
            top_srcdir = os.environ['UNINSTALLED_INTROSPECTION_SRCDIR']
            top_builddir = os.environ['UNINSTALLED_INTROSPECTION_BUILDDIR']
            extra_include_dirs = [os.path.join(top_srcdir, 'gir'), top_builddir]
        else:
            extra_include_dirs = []
        self.transformer = Transformer.parse_from_gir(filename, extra_include_dirs)

    def generate(self, writer, output):
        writer.add_transformer(self.transformer)
        writer.write(output)

def doc_main(args):
    parser = optparse.OptionParser('%prog [options] GIR-file')

    parser.add_option("-o", "--output",
                      action="store", dest="output",
                      help="Filename to write output")
    parser.add_option("-f", "--format",
                      action="store", dest="format",
                      default="docbook",
                      help="Output format")
    parser.add_option("-l", "--language",
                      action="store", dest="language",
                      default="Python",
                      help="Output language")

    options, args = parser.parse_args(args)
    if not options.output:
        raise SystemExit("missing output parameter")

    if len(args) < 2:
        raise SystemExit("Need an input GIR filename")

    if options.format == "docbook":
        if options.language == "Python":
            formatter = DocBookFormatterPython()
        elif options.language == "C":
            formatter = DocBookFormatterC()
        else:
            raise SystemExit("Unsupported language: %s" % (options.language, ))
        writer = DocBookWriter(formatter)
    elif options.format == "mallard":
        if options.language == "Python":
            formatter = MallardFormatterPython()
        elif options.language == "C":
            formatter = MallardFormatterC()
        else:
            raise SystemExit("Unsupported language: %s" % (options.language, ))
        writer = MallardWriter(formatter)
    else:
        raise SystemExit("Unsupported output format: %s" % (options.format, ))

    generator = GIDocGenerator()
    generator.parse(args[1])

    generator.generate(writer, options.output)

    return 0