Juan Hernandez has uploaded a new change for review. Change subject: codegen, sdk: Use Nothing instead of None ......................................................................
codegen, sdk: Use Nothing instead of None Currently whe use None to indicate as the default value for optional parameters. This means that it isn't possible to distinguish a parameter that hasn't been passed from a parameter that has been passed with None as the value, and in turn that means that it isn't possible to decide if parameters shouln't be sent to the server or if they should be sent but without a value. This patch introduces a new Nothing constant that will be used as the default value for parameters, instead of None, in places where we need to check if the parameter was actually passed or not. Change-Id: I2798a49da133a3c378dcaecd8275702e05c82acb Bug-Url: https://bugzilla.redhat.com/1058396 Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com> --- M src/codegen/main.py M src/codegen/templates/importstemplate M src/codegen/utils/paramutils.py A src/ovirtsdk/utils/nothing.py M src/ovirtsdk/utils/searchhelper.py 5 files changed, 60 insertions(+), 4 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-sdk refs/changes/05/24305/1 diff --git a/src/codegen/main.py b/src/codegen/main.py old mode 100644 new mode 100755 diff --git a/src/codegen/templates/importstemplate b/src/codegen/templates/importstemplate index 6c390a5..08b4af5 100644 --- a/src/codegen/templates/importstemplate +++ b/src/codegen/templates/importstemplate @@ -4,6 +4,7 @@ from ovirtsdk.utils.filterhelper import FilterHelper from ovirtsdk.utils.parsehelper import ParseHelper from ovirtsdk.utils.searchhelper import SearchHelper +from ovirtsdk.utils.nothing import Nothing from ovirtsdk.infrastructure.common import Base from ovirtsdk.infrastructure.context import context from ovirtsdk.infrastructure.errors import MissingParametersError diff --git a/src/codegen/utils/paramutils.py b/src/codegen/utils/paramutils.py index 31b6458..ed11212 100644 --- a/src/codegen/utils/paramutils.py +++ b/src/codegen/utils/paramutils.py @@ -36,13 +36,13 @@ else: name_candidate = param.name if name_candidate == 'search': - method_parameters += param.value + '=None, ' + method_parameters += param.value + '=Nothing, ' method_params[param.value] = name_candidate else: if param.name == 'case_sensitive': method_parameters += name_candidate + '=True, ' else: - method_parameters += name_candidate + '=None, ' + method_parameters += name_candidate + '=Nothing, ' if hasattr(param, 'type_'): method_params[name_candidate] = param.type_.replace('xs:', '') + \ ' (' + param.value + ')' diff --git a/src/ovirtsdk/utils/nothing.py b/src/ovirtsdk/utils/nothing.py new file mode 100644 index 0000000..c93e8d3 --- /dev/null +++ b/src/ovirtsdk/utils/nothing.py @@ -0,0 +1,51 @@ +# +# Copyright (c) 2014 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +class Nothing: + """A singleton to indicate that parameters have default value. + + This class will have only one instance, intended to indicate that + parameters to functions have been assigned their default value. + + For example, consider a function like this: + + def f(x=None): + if x is None: + print("Parameter wasn't given, or hasn't a value.") + else: + print("Parameter was given, and has a value.") + + In this function there is no way to know if the parameter wasn't given or + if it was given with None as the value. + + To solve this problem this class can be used as follows: + + def f(x=Nothing): + if x is Nothing: + print("Parameter x wasn't given.") + else if x is None: + print("Parameter x was given, but it hasn't a value.") + else: + print("Parameter x was given, and has a value.") + """ + + def __str__(self): + return "Nothing" + + def __repr__(delf): + return "Nothing" + +Nothing = Nothing() diff --git a/src/ovirtsdk/utils/searchhelper.py b/src/ovirtsdk/utils/searchhelper.py index 0a40523..e44af6e 100644 --- a/src/ovirtsdk/utils/searchhelper.py +++ b/src/ovirtsdk/utils/searchhelper.py @@ -14,9 +14,11 @@ # limitations under the License. # +from ovirtsdk.utils.nothing import Nothing from urllib import urlencode -import re import fnmatch +import re + class SearchHelper(): @@ -29,7 +31,9 @@ if (qargs and len(qargs) > 0): for k, v in qargs.items(): - if v != None: + if not v is Nothing: + if v is None: + v = '' prms = k.split(':') if len(prms) == 2 and prms[1] == 'matrix': matrix_params += ';' + urlencode({prms[0] : v}) -- To view, visit http://gerrit.ovirt.org/24305 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I2798a49da133a3c378dcaecd8275702e05c82acb Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine-sdk Gerrit-Branch: master Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches