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>
<para>
<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
url="http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format">D-Bus
Introspection XML</ulink> files and generates output files. The
tool currently supports generating C code (via
Introspection XML</ulink> from files passed as additional
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-docbook</option>).
</para>
@ -152,6 +156,7 @@
<term><option>--xml-files</option> <replaceable>FILE</replaceable></term>
<listitem>
<para>
This option is deprecated; use positional arguments instead.
The D-Bus introspection XML file.
</para>
</listitem>

View File

@ -19,8 +19,8 @@
#
# Author: David Zeuthen <davidz@redhat.com>
import argparse
import sys
import optparse
from os import path
from . import config
@ -28,7 +28,7 @@ from . import dbustypes
from . import parser
from . import codegen
from . import codegen_docbook
from .utils import print_error
from .utils import print_error, print_warning
def find_arg(arg_list, arg_name):
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)
def codegen_main():
arg_parser = optparse.OptionParser('%prog [options]')
arg_parser.add_option('', '--xml-files', metavar='FILE', action='append',
help='D-Bus introspection XML file')
arg_parser.add_option('', '--interface-prefix', metavar='PREFIX', default='',
arg_parser = argparse.ArgumentParser(description='D-Bus code and documentation generator')
arg_parser.add_argument('files', metavar='FILE', nargs='*',
help='D-Bus introspection XML file')
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')
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')
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')
arg_parser.add_option('', '--generate-c-code', metavar='OUTFILES',
help='Generate C code in OUTFILES.[ch]')
arg_parser.add_option('', '--c-generate-autocleanup', type='choice', choices=['none', 'objects', 'all'], default='objects',
help='Generate autocleanup support')
arg_parser.add_option('', '--generate-docbook', metavar='OUTFILES',
help='Generate Docbook in OUTFILES-org.Project.IFace.xml')
arg_parser.add_option('', '--annotate', nargs=3, action='append', metavar='WHAT KEY VALUE',
help='Add annotation (may be used several times)')
arg_parser.add_option('', '--output-directory', metavar='OUTDIR', default='',
help='Location to output generated files')
(opts, args) = arg_parser.parse_args();
arg_parser.add_argument('--generate-c-code', metavar='OUTFILES',
help='Generate C code in OUTFILES.[ch]')
arg_parser.add_argument('--c-generate-autocleanup', choices=['none', 'objects', 'all'], default='objects',
help='Generate autocleanup support')
arg_parser.add_argument('--generate-docbook', metavar='OUTFILES',
help='Generate Docbook in OUTFILES-org.Project.IFace.xml')
arg_parser.add_argument('--annotate', nargs=3, action='append', metavar='WHAT KEY VALUE',
help='Add annotation (may be used several times)')
arg_parser.add_argument('--output-directory', metavar='OUTDIR', default='',
help='Location to output generated files')
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 = []
for fname in args:
f = open(fname, 'rb')
xml_data = f.read()
f.close()
for fname in args.files + args.xml_files:
with open(fname, 'rb') as f:
xml_data = f.read()
parsed_ifaces = parser.parse_dbus_xml(xml_data)
all_ifaces.extend(parsed_ifaces)
if opts.annotate != None:
apply_annotations(all_ifaces, opts.annotate)
if args.annotate != None:
apply_annotations(all_ifaces, args.annotate)
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);
if docbook:
ret = docbook_gen.generate()
c_code = opts.generate_c_code
c_code = args.generate_c_code
if c_code:
header_name = c_code + '.h'
h = open(path.join(outdir, header_name), 'w')
c = open(path.join(outdir, c_code + '.c'), 'w')
gen = codegen.CodeGenerator(all_ifaces,
opts.c_namespace,
opts.interface_prefix,
opts.c_generate_object_manager,
opts.c_generate_autocleanup,
args.c_namespace,
args.interface_prefix,
args.c_generate_object_manager,
args.c_generate_autocleanup,
docbook_gen,
h, c,
header_name)