Michael Pasternak has uploaded a new change for review.

Change subject: sdk: collection-based-options definition doesnt look like cli 
format #833788
......................................................................

sdk: collection-based-options definition doesnt look like cli format #833788

https://bugzilla.redhat.com/show_bug.cgi?id=833788

new collection based arguments syntax is:

    --<type>.<collection-obj>.<collection-member> 
"<collection-item>.<collection-item-property>=value,..."

    e.g:

    --power_management-options-option "option.name=n1,option.value=v1"
    --power_management-options-option "option.name=n2,option.value=v2"

Change-Id: Ica82f155511beb0726e1b4f32d31d7438ee56612
Signed-off-by: Michael Pasternak <[email protected]>
---
M src/cli/command/command.py
M src/cli/messages.py
M src/ovirtcli/command/action.py
M src/ovirtcli/command/command.py
M src/ovirtcli/command/create.py
M src/ovirtcli/command/delete.py
M src/ovirtcli/command/update.py
A src/ovirtcli/utils/multivaluedict.py
8 files changed, 111 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/07/7207/1

diff --git a/src/cli/command/command.py b/src/cli/command/command.py
index 04d7c89..f964ad2 100644
--- a/src/cli/command/command.py
+++ b/src/cli/command/command.py
@@ -19,6 +19,7 @@
 from cli.error import CommandError
 from ovirtcli.format.help import Help
 from ovirtcli.utils.methodhelper import MethodHelper
+from ovirtcli.utils.multivaluedict import MultiValueDict
 
 
 class Command(object):
@@ -34,7 +35,7 @@
     def __init__(self, arguments, options):
         """Constructor."""
         self.arguments = arguments
-        self.options = dict(options)
+        self.options = MultiValueDict(options)
         if '--help' in self.options:
             self.mode = 'show_help'
         else:
diff --git a/src/cli/messages.py b/src/cli/messages.py
index 51396d2..5733f86 100644
--- a/src/cli/messages.py
+++ b/src/cli/messages.py
@@ -42,6 +42,7 @@
         INVALID_OPTION = 'option %s cannot be empty.'
         INVALID_COLLECTION_BASED_OPTION_SYNTAX = 'invalid syntax at "--%s", 
see help on collection based arguments for more details.'
         INVALID_ARGUMENT_SEGMENT = '"%s" is invalid argument segment.'
+        INVALID_OPTION_SEGMENT = '"%s" is invalid segment at option "--%s".'
     class Warning():
         CANNOT_FETCH_HOST_CERT_SUBJECT = 'could not fetch host certificate 
info.'
         CANNOT_FETCH_HOST_CERT_SUBJECT_LEGACY_SDK = 'could not fetch host 
certificate info cause used backend/sdk does not support it.'
diff --git a/src/ovirtcli/command/action.py b/src/ovirtcli/command/action.py
index 3ab9bb4..8238d3a 100644
--- a/src/ovirtcli/command/action.py
+++ b/src/ovirtcli/command/action.py
@@ -103,7 +103,13 @@
 
           $actions
 
-          Note: collection based arguments syntax is: 
"{x=a1;y=b1;z=c1;...},{x=a2;y=b2;z=c2;...},..."
+          Note: collection based arguments syntax is: 
+          --<type>.<collection-obj>.<collection-member> 
"<collection-item>.<collection-item-property>=value,..."
+          
+          e.g:
+          
+          --power_management-options-option "option.name=n1,option.value=v1" 
+          --power_management-options-option "option.name=n2,option.value=v2"
 
         == Object Identifiers ==
 
diff --git a/src/ovirtcli/command/command.py b/src/ovirtcli/command/command.py
index 8d89441..fd46cb8 100644
--- a/src/ovirtcli/command/command.py
+++ b/src/ovirtcli/command/command.py
@@ -26,6 +26,7 @@
 from ovirtsdk.utils.ordereddict import OrderedDict
 import itertools
 from cli.messages import Messages
+import types
 
 
 class OvirtCommand(Command):
@@ -89,6 +90,7 @@
                 return int(param)
             elif param and param.isdigit():
                 return int(param)
+            else: return param
         except:
             return param
 
@@ -104,17 +106,40 @@
                     self.__set_property(obj, props[i], val, fq_prop)
                     return
                 if hasattr(obj, props[i]) and type(getattr(obj, props[i])) == 
list:
-                    for params_set in val.split(','):
                         params_set_cand = ParseHelper.getXmlType(props[i])
                         if params_set_cand:
                             obj_params_set_cand = params_set_cand.factory()
+                            root_obj_params_set_cand = obj_params_set_cand
                         else:
                             self.error(Messages.Error.NO_SUCH_TYPE) % props[i]
-                        for param in params_set.replace('{', '').replace('}', 
'').split(';'):
+                        for param in val.split(','):
+                            obj_params_set_cand = root_obj_params_set_cand
                             param_data = param.replace(props[i] + '.', 
'').split('=')
                             if len(param_data) == 2:
-                                if hasattr(obj_params_set_cand, param_data[0]):
-                                    setattr(obj_params_set_cand, 
param_data[0], self.__try_parse(param_data[1].strip()))
+                                spplited_param_data = param_data[0].split('.')
+                                for param_period in spplited_param_data:
+                                    if spplited_param_data[-1] == param_period:
+                                        if hasattr(obj_params_set_cand, 
param_period):
+                                            setattr(obj_params_set_cand, 
param_period,
+                                                    
self.__try_parse(param_data[1].strip()))
+                                        else:
+                                            
self.error(Messages.Error.INVALID_OPTION_SEGMENT % \
+                                                       (param_period, 
param_data[0]))
+                                    elif hasattr(obj_params_set_cand, 
param_period) and \
+                                         getattr(obj_params_set_cand, 
param_period) == None:
+                                        param_period_cand = 
ParseHelper.getXmlType(param_period)
+                                        if param_period_cand:
+                                            param_period_cand_obj = 
param_period_cand.factory()
+                                            setattr(obj_params_set_cand, 
param_period, param_period_cand_obj)
+                                            obj_params_set_cand = 
param_period_cand_obj
+                                    elif hasattr(obj_params_set_cand, 
param_period):
+                                        param_period_cand = 
ParseHelper.getXmlType(param_period)
+                                        if param_period_cand:
+                                            param_period_cand_obj = 
getattr(obj_params_set_cand, param_period)
+                                            obj_params_set_cand = 
param_period_cand_obj
+                                    else:
+                                        
self.error(Messages.Error.INVALID_OPTION_SEGMENT % \
+                                                   (param_period, 
param_data[0]))
                             else:
                                 
self.error(Messages.Error.INVALID_COLLECTION_BASED_OPTION_SYNTAX % prop)
                         getattr(obj, props[i]).append(obj_params_set_cand)
@@ -161,7 +186,11 @@
             prop = key.replace('--', '')
             val = options[key]
             if not prop.endswith('-id') and prop.endswith('-identifier'): 
continue
-            self.__do_set_data(obj=obj, prop=prop, fq_prop=key, val=val)
+            if type(val) == types.ListType:
+                for item in val:
+                    self.__do_set_data(obj=obj, prop=prop, fq_prop=key, 
val=item)
+            else:
+                self.__do_set_data(obj=obj, prop=prop, fq_prop=key, val=val)
 
         return obj
 
diff --git a/src/ovirtcli/command/create.py b/src/ovirtcli/command/create.py
index 2fdf64a..ab5aa6f 100644
--- a/src/ovirtcli/command/create.py
+++ b/src/ovirtcli/command/create.py
@@ -109,7 +109,13 @@
 
           $options
 
-          Note: collection based arguments syntax is: 
"{x=a1;y=b1;z=c1;...},{x=a2;y=b2;z=c2;...},..."
+          Note: collection based arguments syntax is: 
+          --<type>.<collection-obj>.<collection-member> 
"<collection-item>.<collection-item-property>=value,..."
+          
+          e.g:
+          
+          --power_management-options-option "option.name=n1,option.value=v1" 
+          --power_management-options-option "option.name=n2,option.value=v2"
 
         == Return Values ==
 
diff --git a/src/ovirtcli/command/delete.py b/src/ovirtcli/command/delete.py
index ead5e00..d6b4a50 100644
--- a/src/ovirtcli/command/delete.py
+++ b/src/ovirtcli/command/delete.py
@@ -109,7 +109,13 @@
 
           $options
 
-          Note: collection based arguments syntax is: 
"{x=a1;y=b1;z=c1;...},{x=a2;y=b2;z=c2;...},..."
+          Note: collection based arguments syntax is: 
+          --<type>.<collection-obj>.<collection-member> 
"<collection-item>.<collection-item-property>=value,..."
+          
+          e.g:
+          
+          --power_management-options-option "option.name=n1,option.value=v1" 
+          --power_management-options-option "option.name=n2,option.value=v2"
 
         == Return Values ==
 
diff --git a/src/ovirtcli/command/update.py b/src/ovirtcli/command/update.py
index 5c0148a..0284b61 100644
--- a/src/ovirtcli/command/update.py
+++ b/src/ovirtcli/command/update.py
@@ -112,7 +112,13 @@
 
           $options
 
-          Note: collection based arguments syntax is: 
"{x=a1;y=b1;z=c1;...},{x=a2;y=b2;z=c2;...},..."
+          Note: collection based arguments syntax is: 
+          --<type>.<collection-obj>.<collection-member> 
"<collection-item>.<collection-item-property>=value,..."
+          
+          e.g:
+          
+          --power_management-options-option "option.name=n1,option.value=v1" 
+          --power_management-options-option "option.name=n2,option.value=v2"
 
         == Return Values ==
 
diff --git a/src/ovirtcli/utils/multivaluedict.py 
b/src/ovirtcli/utils/multivaluedict.py
new file mode 100644
index 0000000..8413bc1
--- /dev/null
+++ b/src/ovirtcli/utils/multivaluedict.py
@@ -0,0 +1,46 @@
+#
+# Copyright (c) 2010 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.
+#
+
+import types
+
+class MultiValueDict(dict):
+    '''
+    Dictionary that supports multi-value based keys
+    '''
+
+    def __init__(self, *args, **kwargs):
+        if args and args[0] and args[0][0] and type(args[0][0]) == 
types.TupleType:
+            for item in args[0]:
+                if len(item) >= 2:
+                    self.__setitem__(item[0], item[1])
+                elif len(item) == 1:
+                    self.__setitem__(item[0], None)
+        else:
+            dict.__init__(self, *args, **kwargs)
+
+    def __setitem__(self, key, value):
+        if self.has_key(key):
+            if self[key] <> None and type(self[key]) != types.ListType:
+                tmp = self[key]
+                super(MultiValueDict, self).__setitem__(key, [])
+                self[key].append(tmp)
+                self[key].append(value)
+            elif self[key] == None:
+                super(MultiValueDict, self).__setitem__(key, value)
+            else:
+                self[key].append(value)
+        else:
+            super(MultiValueDict, self).__setitem__(key, value)


--
To view, visit http://gerrit.ovirt.org/7207
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ica82f155511beb0726e1b4f32d31d7438ee56612
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine-cli
Gerrit-Branch: master
Gerrit-Owner: Michael Pasternak <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to