Change license name
[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 argparse
23
24 import giscanner
25 from .docwriter import DocWriter
26 from .sectionparser import generate_sections_file, write_sections_file
27 from .transformer import Transformer
28
29 FORMATS = ('devdocs', 'mallard', 'sections')
30
31
32 def doc_main(args):
33     parser = argparse.ArgumentParser()
34     parser.add_argument('--version', action='version',
35                       version='%(prog)s ' + giscanner.__version__)
36     parser.add_argument("girfile")
37     parser.add_argument("-o", "--output",
38                       action="store", dest="output",
39                       help="Directory to write output to")
40     parser.add_argument("-l", "--language",
41                       action="store", dest="language",
42                       default="c",
43                       help="Output language")
44     parser.add_argument("-f", "--format",
45                         action="store", dest="format",
46                         choices=FORMATS, default=FORMATS[1],
47                         help="Output format")
48     parser.add_argument("-I", "--add-include-path",
49                       action="append", dest="include_paths", default=[],
50                       help="include paths for other GIR files")
51     parser.add_argument("-s", "--write-sections-file",
52                         action="store_const", dest="format", const="sections",
53                         help="Backwards-compatible equivalent to -f sections")
54
55     args = parser.parse_args(args[1:])
56     if not args.output:
57         raise SystemExit("missing output parameter")
58     if args.format not in FORMATS:
59         raise SystemExit("Unknown output format %s (supported: %s)" %
60             (args.format, ", ".join(FORMATS)))
61
62     if 'UNINSTALLED_INTROSPECTION_SRCDIR' in os.environ:
63         top_srcdir = os.environ['UNINSTALLED_INTROSPECTION_SRCDIR']
64         top_builddir = os.environ['UNINSTALLED_INTROSPECTION_BUILDDIR']
65         extra_include_dirs = [os.path.join(top_srcdir, 'gir'), top_builddir]
66     else:
67         extra_include_dirs = []
68     extra_include_dirs.extend(args.include_paths)
69     transformer = Transformer.parse_from_gir(args.girfile, extra_include_dirs)
70
71     if args.format == 'sections':
72         sections_file = generate_sections_file(transformer)
73
74         with open(args.output, 'w', encoding='utf-8') as fp:
75             write_sections_file(fp, sections_file)
76     else:
77         writer = DocWriter(transformer, args.language, args.format)
78         writer.write(args.output)
79
80     return 0