From 382a2d95658a57b47720e032fc2ee83a89f47869 Mon Sep 17 00:00:00 2001 From: chenchaozhe1988 Date: Wed, 18 Nov 2015 15:51:34 +0800 Subject: [PATCH] Add common arguments Merge same arguments in common arguments to make it concise and convenient. Change-Id: I75e246d36ed7d38858e9dfdedcc77dd19ea587d5 --- cloudkittyclient/v1/rating/hashmap/shell.py | 94 ++++++++++----------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/cloudkittyclient/v1/rating/hashmap/shell.py b/cloudkittyclient/v1/rating/hashmap/shell.py index 87b03f9..d7eb6c3 100644 --- a/cloudkittyclient/v1/rating/hashmap/shell.py +++ b/cloudkittyclient/v1/rating/hashmap/shell.py @@ -112,24 +112,34 @@ def do_hashmap_field_delete(cc, args={}): raise exc.CommandError('Field not found: %s' % args.counter_name) -@utils.arg('-c', '--cost', - help='Mapping cost', - required=True) -@utils.arg('-v', '--value', - help='Mapping value', - required=False) -@utils.arg('-t', '--type', - help='Mapping type (flat, rate)', - required=False) +def common_hashmap_mapping_arguments(create=False): + def _wrapper(func): + @utils.arg('-c', '--cost', + help='Mapping cost', + required=create) + @utils.arg('-v', '--value', + help='Mapping value', + required=False) + @utils.arg('-t', '--type', + help='Mapping type (flat, rate)', + required=False) + @utils.arg('-g', '--group-id', + help='Group id', + required=False) + @functools.wraps(func) + def _wrapped(*args, **kwargs): + return func(*args, **kwargs) + return _wrapped + return _wrapper + + @utils.arg('-s', '--service-id', help='Service id', required=False) @utils.arg('-f', '--field-id', help='Field id', required=False) -@utils.arg('-g', '--group-id', - help='Group id', - required=False) +@common_hashmap_mapping_arguments(create=True) def do_hashmap_mapping_create(cc, args={}): """Create a mapping.""" arg_to_field_mapping = { @@ -152,18 +162,7 @@ def do_hashmap_mapping_create(cc, args={}): @utils.arg('-m', '--mapping-id', help='Mapping id', required=True) -@utils.arg('-c', '--cost', - help='Mapping cost', - required=False) -@utils.arg('-v', '--value', - help='Mapping value', - required=False) -@utils.arg('-t', '--type', - help='Mapping type (flat, rate)', - required=False) -@utils.arg('-g', '--group-id', - help='Group id', - required=False) +@common_hashmap_mapping_arguments() def do_hashmap_mapping_update(cc, args={}): """Update a mapping.""" arg_to_field_mapping = { @@ -272,24 +271,34 @@ def do_hashmap_group_delete(cc, args={}): raise exc.CommandError('Group not found: %s' % args.group_id) -@utils.arg('-l', '--level', - help='Threshold level', - required=True) -@utils.arg('-c', '--cost', - help='Threshold cost', - required=True) -@utils.arg('-m', '--map-type', - help='Threshold type (flat, rate)', - required=False) +def common_hashmap_threshold_arguments(create=False): + def _wrapper(func): + @utils.arg('-l', '--level', + help='Threshold level', + required=create) + @utils.arg('-c', '--cost', + help='Threshold cost', + required=create) + @utils.arg('-m', '--map-type', + help='Threshold type (flat, rate)', + required=False) + @utils.arg('-g', '--group-id', + help='Group id', + required=False) + @functools.wraps(func) + def _wrapped(*args, **kwargs): + return func(*args, **kwargs) + return _wrapped + return _wrapper + + @utils.arg('-s', '--service-id', help='Service id', required=False) @utils.arg('-f', '--field-id', help='Field id', required=False) -@utils.arg('-g', '--group-id', - help='Group id', - required=False) +@common_hashmap_threshold_arguments(create=True) def do_hashmap_threshold_create(cc, args={}): """Create a mapping.""" arg_to_field_mapping = { @@ -312,18 +321,7 @@ def do_hashmap_threshold_create(cc, args={}): @utils.arg('-t', '--threshold-id', help='Threshold id', required=True) -@utils.arg('-l', '--level', - help='Threshold level', - required=False) -@utils.arg('-c', '--cost', - help='Threshold cost', - required=False) -@utils.arg('-m', '--map-type', - help='Threshold type (flat, rate)', - required=False) -@utils.arg('-g', '--group-id', - help='Group id', - required=False) +@common_hashmap_threshold_arguments() def do_hashmap_threshold_update(cc, args={}): """Update a threshold.""" arg_to_field_mapping = {