Juan Hernandez has uploaded a new change for review. Change subject: cli: Calculate singulars and plurals correctly ......................................................................
cli: Calculate singulars and plurals correctly Currently when we need to calculate singulars and plurals we just remove or add a final "s". This doesn't work for names like "SchedulingPolicy" and "SchedulingPolicies". This patch improves the calculations so that they will work correctly for this type of names. Change-Id: I4c7849dadd1161bfd435c7ae79d196c08b3e7e69 Bug-Url: https://bugzilla.redhat.com/1144110 Signed-off-by: Juan Hernandez <[email protected]> --- M src/ovirtcli/command/add.py M src/ovirtcli/command/command.py M src/ovirtcli/utils/typehelper.py 3 files changed, 28 insertions(+), 23 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/70/33070/1 diff --git a/src/ovirtcli/command/add.py b/src/ovirtcli/command/add.py index 01d922f..dc0df39 100644 --- a/src/ovirtcli/command/add.py +++ b/src/ovirtcli/command/add.py @@ -15,8 +15,9 @@ # -from ovirtcli.command.command import OvirtCommand from cli.messages import Messages +from ovirtcli.command.command import OvirtCommand +from ovirtcli.utils.typehelper import TypeHelper class AddCommand(OvirtCommand): @@ -136,7 +137,7 @@ args = self.arguments opts = self.options base = self.resolve_base(opts) - typ = args[0] + 's' + typ = TypeHelper.to_plural(args[0]) collection = None typs = self.get_singular_types(method='add', typ=args[0]) diff --git a/src/ovirtcli/command/command.py b/src/ovirtcli/command/command.py index bd8b386..5c972a9 100644 --- a/src/ovirtcli/command/command.py +++ b/src/ovirtcli/command/command.py @@ -58,7 +58,7 @@ parnet_candidate_locator += 1 typename = key[2:-11] - coll = typename + 's' + coll = TypeHelper.to_plural(typename) if not (TypeHelper.isKnownType(typename) or TypeHelper.isKnownType(coll)): self.error(Messages.Error.NO_SUCH_TYPE % typename) @@ -336,8 +336,8 @@ candidate = typ if typ is not None and isinstance(typ, type('')) \ else type(typ).__name__.lower() - if hasattr(base, candidate + 's'): - coll = getattr(base, candidate + 's') + if hasattr(base, TypeHelper.to_plural(candidate)): + coll = getattr(base, TypeHelper.to_plural(candidate)) else: err_str = Messages.Error.NO_SUCH_TYPE_OR_ARS_NOT_VALID if context_variants: @@ -449,26 +449,26 @@ if isinstance(resource, type('')): if not sub_resource: - if resource and hasattr(connection, resource + 's') and \ - type(getattr(connection, resource + 's')).__dict__.has_key(method): + if resource and hasattr(connection, TypeHelper.to_plural(resource)) and \ + type(getattr(connection, TypeHelper.to_plural(resource))).__dict__.has_key(method): method_ref = getattr(getattr(connection, - resource + 's'), + TypeHelper.to_plural(resource)), method) else: - if hasattr(sub_resource, resource + 's') and \ - type(getattr(sub_resource, resource + 's')).__dict__.has_key(method): + if hasattr(sub_resource, TypeHelper.to_plural(resource)) and \ + type(getattr(sub_resource, TypeHelper.to_plural(resource))).__dict__.has_key(method): method_ref = getattr(getattr(sub_resource, - resource + 's'), + TypeHelper.to_plural(resource)), method) - elif hasattr(sub_resource, resource + 's') and \ + elif hasattr(sub_resource, TypeHelper.to_plural(resource)) and \ hasattr(brokers, TypeHelper.to_singular(type(getattr(sub_resource, - resource + 's')).__name__)) and \ + TypeHelper.to_plural(resource))).__name__)) and \ hasattr(getattr(brokers, TypeHelper.to_singular(type(getattr(sub_resource, - resource + 's')).__name__)), + TypeHelper.to_plural(resource))).__name__)), method): method_ref = getattr(getattr(brokers, TypeHelper.to_singular(type(getattr(sub_resource, - resource + 's')).__name__)), + TypeHelper.to_plural(resource))).__name__)), method) if not method_ref: @@ -481,9 +481,9 @@ elif isinstance(resource, brokers.Base): if hasattr(resource, method): method_ref = getattr(resource, method) - elif hasattr(brokers, type(resource).__name__ + 's') and \ - hasattr(getattr(brokers, type(resource).__name__ + 's'), method): - method_ref = getattr(getattr(brokers, type(resource).__name__ + 's'), method) + elif hasattr(brokers, TypeHelper.to_plural(type(resource).__name__)) and \ + hasattr(getattr(brokers, TypeHelper.to_plural(type(resource).__name__)), method): + method_ref = getattr(getattr(brokers, TypeHelper.to_plural(type(resource).__name__)), method) return MethodHelper.get_arguments_documentation(method_ref, as_params_collection) diff --git a/src/ovirtcli/utils/typehelper.py b/src/ovirtcli/utils/typehelper.py index 6eba8bd..a2dcf14 100644 --- a/src/ovirtcli/utils/typehelper.py +++ b/src/ovirtcli/utils/typehelper.py @@ -120,7 +120,7 @@ dct = getattr(brokers, decorator).__dict__ if dct.has_key(method): if decorator.endswith('s'): - cls_name = TypeHelper.getDecoratorType(decorator[:len(decorator) - 1]) + cls_name = TypeHelper.getDecoratorType(TypeHelper.to_singular(decorator)) if cls_name: MethodHelper.get_method_params(brokers, cls_name, @@ -138,12 +138,16 @@ @staticmethod def to_singular(string): - if string.endswith('s'): - return string[:len(string) - 1] + if string.endswith("ies"): + return string[:-3] + "y" + if string.endswith("s"): + return string[:-1] return string @staticmethod def to_plural(string): - if not string.endswith('s'): - return string + 's' + if string.endswith("y"): + return string[:-1] + "ies" + if not string.endswith("s"): + return string + "s" return string -- To view, visit http://gerrit.ovirt.org/33070 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I4c7849dadd1161bfd435c7ae79d196c08b3e7e69 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine-cli Gerrit-Branch: master Gerrit-Owner: Juan Hernandez <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
