[libvirt] [PATCH] python: set default value to optional arguments

When prefixing with string (optional) or optional in the description of arguments to libvirt C APIs, in python, these arguments will be set as optional arugments, for example: * virDomainSaveFlags: * @domain: a domain object * @to: path for the output file * @dxml: (optional) XML config for adjusting guest xml used on restore * @flags: bitwise-OR of virDomainSaveRestoreFlags the corresponding python APIs is restoreFlags(self, frm, dxml=None, flags=0) The following python APIs are changed to: blockCommit(self, disk, base, top, bandwidth=0, flags=0) blockPull(self, disk, bandwidth=0, flags=0) blockRebase(self, disk, base, bandwidth=0, flags=0) migrate(self, dconn, flags=0, dname=None, uri=None, bandwidth=0) migrate2(self, dconn, dxml=None, flags=0, dname=None, uri=None, bandwidth=0) migrateToURI(self, duri, flags=0, dname=None, bandwidth=0) migrateToURI2(self, dconnuri=None, miguri=None, dxml=None, flags=0, \ dname=None, bandwidth=0) saveFlags(self, to, dxml=None, flags=0) migrate(self, domain, flags=0, dname=None, uri=None, bandwidth=0) migrate2(self, domain, dxml=None, flags=0, dname=None, uri=None, bandwidth=0) restoreFlags(self, frm, dxml=None, flags=0) --- python/generator.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/python/generator.py b/python/generator.py index 0237374..7586ffc 100755 --- a/python/generator.py +++ b/python/generator.py @@ -1005,6 +1005,8 @@ functions_int_default_test = "%s == -1" def is_integral_type (name): return not re.search ("^(unsigned)? ?(int|long)$", name) is None +def is_optional_arg(info): + return not re.search("^\(?\optional\)?", info) is None # Functions returning lists which need special rules to check for errors # and raise exceptions. functions_list_exception_test = { @@ -1488,9 +1490,12 @@ def buildWrappers(module): for arg in args: if n != index: classes.write(", %s" % arg[0]) + if arg[0] == "flags" or is_optional_arg(arg[2]): + if is_integral_type(arg[1]): + classes.write("=0") + else: + classes.write("=None") n = n + 1 - if arg[0] == "flags": - classes.write("=0") classes.write("):\n") writeDoc(module, name, args, ' ', classes) n = 0 -- 1.7.11.2

On 03/25/2013 10:18 AM, Guannan Ren wrote:
When prefixing with string (optional) or optional in the description of arguments to libvirt C APIs, in python, these arguments will be set as optional arugments, for example:
* virDomainSaveFlags: * @domain: a domain object * @to: path for the output file * @dxml: (optional) XML config for adjusting guest xml used on restore * @flags: bitwise-OR of virDomainSaveRestoreFlags
the corresponding python APIs is restoreFlags(self, frm, dxml=None, flags=0)
The following python APIs are changed to: blockCommit(self, disk, base, top, bandwidth=0, flags=0) blockPull(self, disk, bandwidth=0, flags=0) blockRebase(self, disk, base, bandwidth=0, flags=0) migrate(self, dconn, flags=0, dname=None, uri=None, bandwidth=0) migrate2(self, dconn, dxml=None, flags=0, dname=None, uri=None, bandwidth=0) migrateToURI(self, duri, flags=0, dname=None, bandwidth=0) migrateToURI2(self, dconnuri=None, miguri=None, dxml=None, flags=0, \ dname=None, bandwidth=0) saveFlags(self, to, dxml=None, flags=0) migrate(self, domain, flags=0, dname=None, uri=None, bandwidth=0) migrate2(self, domain, dxml=None, flags=0, dname=None, uri=None, bandwidth=0) restoreFlags(self, frm, dxml=None, flags=0) --- python/generator.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/python/generator.py b/python/generator.py index 0237374..7586ffc 100755 --- a/python/generator.py +++ b/python/generator.py @@ -1005,6 +1005,8 @@ functions_int_default_test = "%s == -1" def is_integral_type (name): return not re.search ("^(unsigned)? ?(int|long)$", name) is None
+def is_optional_arg(info): + return not re.search("^\(?\optional\)?", info) is None
Use "is not None" in here, I believe it is described in best practices.
# Functions returning lists which need special rules to check for errors # and raise exceptions. functions_list_exception_test = { @@ -1488,9 +1490,12 @@ def buildWrappers(module): for arg in args: if n != index: classes.write(", %s" % arg[0]) + if arg[0] == "flags" or is_optional_arg(arg[2]): + if is_integral_type(arg[1]): + classes.write("=0") + else: + classes.write("=None") n = n + 1 - if arg[0] == "flags": - classes.write("=0") classes.write("):\n") writeDoc(module, name, args, ' ', classes) n = 0
We have a check for flags being always unsigned long, so I see no place this could make any problems. ACK, Martin

On 03/25/2013 04:28 PM, Martin Kletzander wrote:
On 03/25/2013 10:18 AM, Guannan Ren wrote:
When prefixing with string (optional) or optional in the description of arguments to libvirt C APIs, in python, these arguments will be set as optional arugments, for example:
[...] We have a check for flags being always unsigned long, so I see no place this could make any problems.
ACK,
If you didn't push this yet, I suggest squashing this in as I failed to see this the first time: diff --git a/python/generator.py b/python/generator.py index 7586ffc..5adf3e0 100755 --- a/python/generator.py +++ b/python/generator.py @@ -1334,6 +1334,11 @@ def buildWrappers(module): if n != 0: classes.write(", ") classes.write("%s" % arg[0]) + if arg[0] == "flags" or is_optional_arg(arg[2]): + if is_integral_type(arg[1]): + classes.write("=0") + else: + classes.write("=None") n = n + 1 classes.write("):\n") writeDoc(module, name, args, ' ', classes) -- Thanks, Martin

On 03/26/2013 12:48 AM, Martin Kletzander wrote:
On 03/25/2013 04:28 PM, Martin Kletzander wrote:
On 03/25/2013 10:18 AM, Guannan Ren wrote:
When prefixing with string (optional) or optional in the description of arguments to libvirt C APIs, in python, these arguments will be set as optional arugments, for example:
[...] We have a check for flags being always unsigned long, so I see no place this could make any problems.
ACK,
If you didn't push this yet, I suggest squashing this in as I failed to see this the first time:
diff --git a/python/generator.py b/python/generator.py index 7586ffc..5adf3e0 100755 --- a/python/generator.py +++ b/python/generator.py @@ -1334,6 +1334,11 @@ def buildWrappers(module): if n != 0: classes.write(", ") classes.write("%s" % arg[0]) + if arg[0] == "flags" or is_optional_arg(arg[2]): + if is_integral_type(arg[1]): + classes.write("=0") + else: + classes.write("=None") n = n + 1 classes.write("):\n") writeDoc(module, name, args, ' ', classes) --
Thanks, Martin
Yes, this code will set optional arguments for APIs which don't belong to any classes. Okay, I pushed it with these codes. Thanks. Guannan
participants (2)
-
Guannan Ren
-
Martin Kletzander