Imported Upstream version 1.39.3
[platform/upstream/gobject-introspection.git] / giscanner / docmain.py
1 # -*- Mode: Python -*-
2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008-2011 Johan Dahlin
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
19 #
20
21 import os
22 import optparse
23
24 from .docwriter import DocWriter
25 from .sectionparser import generate_sections_file, write_sections_file
26 from .transformer import Transformer
27
28
29 def doc_main(args):
30     parser = optparse.OptionParser('%prog [options] GIR-file')
31
32     parser.add_option("-o", "--output",
33                       action="store", dest="output",
34                       help="Directory to write output to")
35     parser.add_option("-l", "--language",
36                       action="store", dest="language",
37                       default="c",
38                       help="Output language")
39     parser.add_option("", "--add-include-path",
40                       action="append", dest="include_paths", default=[],
41                       help="include paths for other GIR files")
42     parser.add_option("", "--write-sections-file",
43                       action="store_true", dest="write_sections",
44                       help="Generate and write out a sections file")
45
46     options, args = parser.parse_args(args)
47     if not options.output:
48         raise SystemExit("missing output parameter")
49
50     if len(args) < 2:
51         raise SystemExit("Need an input GIR filename")
52
53     if 'UNINSTALLED_INTROSPECTION_SRCDIR' in os.environ:
54         top_srcdir = os.environ['UNINSTALLED_INTROSPECTION_SRCDIR']
55         top_builddir = os.environ['UNINSTALLED_INTROSPECTION_BUILDDIR']
56         extra_include_dirs = [os.path.join(top_srcdir, 'gir'), top_builddir]
57     else:
58         extra_include_dirs = []
59     extra_include_dirs.extend(options.include_paths)
60     transformer = Transformer.parse_from_gir(args[1], extra_include_dirs)
61
62     if options.write_sections:
63         sections_file = generate_sections_file(transformer)
64
65         fp = open(options.output, 'w')
66         write_sections_file(fp, sections_file)
67         fp.close()
68     else:
69         writer = DocWriter(transformer, options.language)
70         writer.write(options.output)
71
72     return 0