switch from preamble to argparse action

This commit is contained in:
Burt Bielicki 2016-05-03 09:12:35 -07:00
Родитель aae9b90a1a
Коммит 1369fb1642
2 изменённых файлов: 24 добавлений и 24 удалений

Просмотреть файл

@ -33,14 +33,12 @@ def _get_member(obj, path):
pass
return obj
def _make_func(client_factory, member_path, return_type_or_func, unbound_func, preamble=None):
def _make_func(client_factory, member_path, return_type_or_func, unbound_func):
def call_client(args):
client = client_factory(args)
ops_instance = _get_member(client, member_path)
try:
if preamble:
preamble(ops_instance, args)
result = unbound_func(ops_instance, **args)
if not return_type_or_func:
return {}
@ -71,12 +69,11 @@ def build_operation(command_name,
operations,
command_table,
param_aliases=None,
extra_parameters=None,
preamble=None):
extra_parameters=None):
for op in operations:
func = _make_func(client_type, member_path, op.return_type, op.operation, preamble)
func = _make_func(client_type, member_path, op.return_type, op.operation)
args = []
try:

Просмотреть файл

@ -195,28 +195,32 @@ vm_param_aliases = {
},
}
class VMImageFieldAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string = None):
image = values
match = re.match('([^:]*):([^:]*):([^:]*):([^:]*)', image)
if image.lower().endswith('.vhd'):
namespace.os_disk_uri = image
elif match:
namespace.os_type = 'Custom'
namespace.os_publisher = match.group(1)
namespace.os_offer = match.group(2)
namespace.os_sku = match.group(3)
namespace.os_version = match.group(4)
else:
namespace.os_type = image
#return super(VMImageFieldAction, self).__call__(parser, namespace, values, option_string)
extra_parameters = [
{
'name': '--image',
'help': 'The OS image. Supported values: Common OS (e.g. Win2012R2Datacenter), URN (e.g. "publisher:offer:sku:version"), or existing VHD URI.'
'help': 'The OS image. Supported values: Common OS (e.g. Win2012R2Datacenter), URN (e.g. "publisher:offer:sku:version"), or existing VHD URI.',
'action': VMImageFieldAction
}
]
def preamble(_, args):
image = args.get('image')
match = re.match('([^:]*):([^:]*):([^:]*):([^:]*)', image)
if image.lower().endswith('.vhd'):
args['os_disk_uri'] = image
elif match:
args['os_type'] = 'Custom'
args['os_publisher'] = match.group(1)
args['os_offer'] = match.group(2)
args['os_sku'] = match.group(3)
args['os_version'] = match.group(4)
else:
args['os_type'] = image
build_operation('vm',
'vm',
lambda _: get_mgmt_service_client(VMClient, VMClientConfig),
@ -227,5 +231,4 @@ build_operation('vm',
],
command_table,
vm_param_aliases,
extra_parameters,
preamble)
extra_parameters)