gdbus-codegen: Move from optparse to argparse

The optparse module is deprecated since version 2.7 and the
development continues with the argparse.

The code has been moved from optparse to argparse when parsing
command-line options. This has also led to the deprecation of the
`--xml-files`, and positional arguments should be used instead.

https://bugzilla.gnome.org/show_bug.cgi?id=791015
This commit is contained in:
Iñigo Martínez 2018-01-02 20:24:56 +01:00
parent dcc1fe09d0
commit e59bce3c74
2 changed files with 45 additions and 36 deletions

View File

@ -55,11 +55,15 @@
<title>Description</title> <title>Description</title>
<para> <para>
<command>gdbus-codegen</command> is used to generate code and/or <command>gdbus-codegen</command> is used to generate code and/or
documentation for one or more D-Bus interfaces. The tool reads documentation for one or more D-Bus interfaces.
</para>
<para>
<command>gdbus-codegen</command> reads
<ulink <ulink
url="http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format">D-Bus url="http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format">D-Bus
Introspection XML</ulink> files and generates output files. The Introspection XML</ulink> from files passed as additional
tool currently supports generating C code (via arguments on the command line and generates output files.
It currently supports generating C code (via
<option>--generate-c-code</option>) and Docbook XML (via <option>--generate-c-code</option>) and Docbook XML (via
<option>--generate-docbook</option>). <option>--generate-docbook</option>).
</para> </para>
@ -152,6 +156,7 @@
<term><option>--xml-files</option> <replaceable>FILE</replaceable></term> <term><option>--xml-files</option> <replaceable>FILE</replaceable></term>
<listitem> <listitem>
<para> <para>
This option is deprecated; use positional arguments instead.
The D-Bus introspection XML file. The D-Bus introspection XML file.
</para> </para>
</listitem> </listitem>

View File

@ -19,8 +19,8 @@
# #
# Author: David Zeuthen <davidz@redhat.com> # Author: David Zeuthen <davidz@redhat.com>
import argparse
import sys import sys
import optparse
from os import path from os import path
from . import config from . import config
@ -28,7 +28,7 @@ from . import dbustypes
from . import parser from . import parser
from . import codegen from . import codegen
from . import codegen_docbook from . import codegen_docbook
from .utils import print_error from .utils import print_error, print_warning
def find_arg(arg_list, arg_name): def find_arg(arg_list, arg_name):
for a in arg_list: for a in arg_list:
@ -146,58 +146,62 @@ def apply_annotations(iface_list, annotation_list):
apply_annotation(iface_list, iface, None, None, None, None, key, value) apply_annotation(iface_list, iface, None, None, None, None, key, value)
def codegen_main(): def codegen_main():
arg_parser = optparse.OptionParser('%prog [options]') arg_parser = argparse.ArgumentParser(description='D-Bus code and documentation generator')
arg_parser.add_option('', '--xml-files', metavar='FILE', action='append', arg_parser.add_argument('files', metavar='FILE', nargs='*',
help='D-Bus introspection XML file') help='D-Bus introspection XML file')
arg_parser.add_option('', '--interface-prefix', metavar='PREFIX', default='', arg_parser.add_argument('--xml-files', metavar='FILE', action='append', default=[],
help='D-Bus introspection XML file')
arg_parser.add_argument('--interface-prefix', metavar='PREFIX', default='',
help='String to strip from D-Bus interface names for code and docs') help='String to strip from D-Bus interface names for code and docs')
arg_parser.add_option('', '--c-namespace', metavar='NAMESPACE', default='', arg_parser.add_argument('--c-namespace', metavar='NAMESPACE', default='',
help='The namespace to use for generated C code') help='The namespace to use for generated C code')
arg_parser.add_option('', '--c-generate-object-manager', action='store_true', arg_parser.add_argument('--c-generate-object-manager', action='store_true',
help='Generate a GDBusObjectManagerClient subclass when generating C code') help='Generate a GDBusObjectManagerClient subclass when generating C code')
arg_parser.add_option('', '--generate-c-code', metavar='OUTFILES', arg_parser.add_argument('--generate-c-code', metavar='OUTFILES',
help='Generate C code in OUTFILES.[ch]') help='Generate C code in OUTFILES.[ch]')
arg_parser.add_option('', '--c-generate-autocleanup', type='choice', choices=['none', 'objects', 'all'], default='objects', arg_parser.add_argument('--c-generate-autocleanup', choices=['none', 'objects', 'all'], default='objects',
help='Generate autocleanup support') help='Generate autocleanup support')
arg_parser.add_option('', '--generate-docbook', metavar='OUTFILES', arg_parser.add_argument('--generate-docbook', metavar='OUTFILES',
help='Generate Docbook in OUTFILES-org.Project.IFace.xml') help='Generate Docbook in OUTFILES-org.Project.IFace.xml')
arg_parser.add_option('', '--annotate', nargs=3, action='append', metavar='WHAT KEY VALUE', arg_parser.add_argument('--annotate', nargs=3, action='append', metavar='WHAT KEY VALUE',
help='Add annotation (may be used several times)') help='Add annotation (may be used several times)')
arg_parser.add_option('', '--output-directory', metavar='OUTDIR', default='', arg_parser.add_argument('--output-directory', metavar='OUTDIR', default='',
help='Location to output generated files') help='Location to output generated files')
(opts, args) = arg_parser.parse_args(); args = arg_parser.parse_args();
if len(args.xml_files) > 0:
print_warning('The "--xml-files" option is deprecated; use positional arguments instead')
all_ifaces = [] all_ifaces = []
for fname in args: for fname in args.files + args.xml_files:
f = open(fname, 'rb') with open(fname, 'rb') as f:
xml_data = f.read() xml_data = f.read()
f.close()
parsed_ifaces = parser.parse_dbus_xml(xml_data) parsed_ifaces = parser.parse_dbus_xml(xml_data)
all_ifaces.extend(parsed_ifaces) all_ifaces.extend(parsed_ifaces)
if opts.annotate != None: if args.annotate != None:
apply_annotations(all_ifaces, opts.annotate) apply_annotations(all_ifaces, args.annotate)
for i in all_ifaces: for i in all_ifaces:
i.post_process(opts.interface_prefix, opts.c_namespace) i.post_process(args.interface_prefix, args.c_namespace)
outdir = opts.output_directory outdir = args.output_directory
docbook = opts.generate_docbook docbook = args.generate_docbook
docbook_gen = codegen_docbook.DocbookCodeGenerator(all_ifaces, docbook, outdir); docbook_gen = codegen_docbook.DocbookCodeGenerator(all_ifaces, docbook, outdir);
if docbook: if docbook:
ret = docbook_gen.generate() ret = docbook_gen.generate()
c_code = opts.generate_c_code c_code = args.generate_c_code
if c_code: if c_code:
header_name = c_code + '.h' header_name = c_code + '.h'
h = open(path.join(outdir, header_name), 'w') h = open(path.join(outdir, header_name), 'w')
c = open(path.join(outdir, c_code + '.c'), 'w') c = open(path.join(outdir, c_code + '.c'), 'w')
gen = codegen.CodeGenerator(all_ifaces, gen = codegen.CodeGenerator(all_ifaces,
opts.c_namespace, args.c_namespace,
opts.interface_prefix, args.interface_prefix,
opts.c_generate_object_manager, args.c_generate_object_manager,
opts.c_generate_autocleanup, args.c_generate_autocleanup,
docbook_gen, docbook_gen,
h, c, h, c,
header_name) header_name)