Skip to content

Commit

Permalink
PyLint and PEP8 clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
xingwu1 committed Jan 27, 2017
1 parent ec4d65f commit 824eefb
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 128 deletions.
Expand Up @@ -266,7 +266,7 @@ def deserialize_json(self, client, kwargs, json_obj):
"""
message = "Failed to deserialized JSON file into object {}"
try:
kwargs[self._request_param['name']] = client._deserialize( # pylint:disable=W0212
kwargs[self._request_param['name']] = client._deserialize( # pylint: disable=protected-access
self._request_param['model'], json_obj)
except DeserializationError as error:
message += ": {}".format(error)
Expand All @@ -276,7 +276,7 @@ def deserialize_json(self, client, kwargs, json_obj):
raise ValueError(message.format(self._request_param['model']))

def queue_argument(self, name=None, path=None, root=None, # pylint:disable=too-many-arguments
options=None, type=None, dependencies=None): # pylint:disable=W0622
options=None, type=None, dependencies=None): # pylint: disable=redefined-builtin
"""Add pending command line argument
:param str name: The name of the command line argument.
:param str path: The complex object path to the parameter.
Expand Down Expand Up @@ -562,10 +562,10 @@ def _get_attrs(self, model, path):
:param class model: The parameter model class.
:param str path: Request parameter namespace.
"""
for attr, details in model._attribute_map.items(): # pylint: disable=W0212
for attr, details in model._attribute_map.items(): # pylint: disable=protected-access
conditions = []
conditions.append(model._validation.get(attr, {}).get('readonly')) # pylint: disable=W0212
conditions.append(model._validation.get(attr, {}).get('constant')) # pylint: disable=W0212
conditions.append(model._validation.get(attr, {}).get('readonly')) # pylint: disable=protected-access
conditions.append(model._validation.get(attr, {}).get('constant')) # pylint: disable=protected-access
conditions.append('.'.join([path, attr]) in self.ignore)
conditions.append(details['type'][0] in ['{'])
if not any(conditions):
Expand Down Expand Up @@ -622,15 +622,15 @@ def _resolve_conflict(self, arg, param, path, options, typestr, dependencies, co
else:
self.parser.queue_argument(arg, path, param, options, typestr, dependencies)

def _flatten_object(self, path, param_model, conflict_names=[]): # pylint: disable=W0102
def _flatten_object(self, path, param_model, conflict_names=[]): # pylint: disable=dangerous-default-value
"""Flatten a complex parameter object into command line arguments.
:param str path: The complex parameter namespace.
:param class param_model: The complex parameter class.
:param list conflict_name: List of argument names that conflict.
"""
if self._should_flatten(path):
required_attrs = [key for key,
val in param_model._validation.items() if val.get('required')] # pylint: disable=W0212
val in param_model._validation.items() if val.get('required')] # pylint: disable=protected-access

for param_attr, details in self._get_attrs(param_model, path):
options = {}
Expand Down Expand Up @@ -726,11 +726,12 @@ def _load_transformed_arguments(self, handler):


def cli_batch_data_plane_command(name, operation, client_factory, transform=None, # pylint:disable=too-many-arguments
table_transformer=None, flatten=FLATTEN, ignore=None, validator=None):
table_transformer=None, flatten=FLATTEN,
ignore=None, validator=None):
""" Registers an Azure CLI Batch Data Plane command. These commands must respond to a
challenge from the service when they make requests. """
command = AzureBatchDataPlaneCommand(__name__, name, operation, client_factory,
transform, table_transformer, flatten, ignore, validator)
transform, table_transformer, flatten, ignore, validator)

# add parameters required to create a batch client
group_name = 'Batch Account'
Expand Down
Expand Up @@ -40,10 +40,10 @@
for command in ['list', 'show', 'create', 'set', 'delete', 'package']:
register_cli_argument('batch application {}'.format(command), 'account_name', batch_name_type, options_list=('--name', '-n'), validator=application_enabled)

register_cli_argument('batch pool resize', 'if_modified_since', help='Specify this header to perform the operation only if the resource has been modified since the specified date/time.', type=datetime_format, arg_group='Pre-condition')
register_cli_argument('batch pool resize', 'if_unmodified_since', help='Specify this header to perform the operation only if the resource has not been modified since the specified date/time.', type=datetime_format, arg_group='Pre-condition')
register_cli_argument('batch pool resize', 'if_match', help='An ETag is specified. Specify this header to perform the operation only if the resource\'s ETag is an exact match as specified', arg_group='Pre-condition')
register_cli_argument('batch pool resize', 'if_none_match', help='An ETag is specified. Specify this header to perform the operation only if the resource\'s ETag does not match the specified ETag.', arg_group='Pre-condition')
register_cli_argument('batch pool resize', 'if_modified_since', help='The operation will be performed only if the resource has been modified since the specified timestamp.', type=datetime_format, arg_group='Pre-condition')
register_cli_argument('batch pool resize', 'if_unmodified_since', help='The operation will not be performed only if the resource has been modified since the specified timestamp.', type=datetime_format, arg_group='Pre-condition')
register_cli_argument('batch pool resize', 'if_match', help='The operation will be performed only if the resource\'s current ETag exactly matches the specified value.', arg_group='Pre-condition')
register_cli_argument('batch pool resize', 'if_none_match', help='The operation will not be performed only if the resource\'s current ETag exactly matches the specified value.', arg_group='Pre-condition')
register_cli_argument('batch pool resize', 'pool_id', help='The ID of the pool.')
register_cli_argument('batch pool resize', 'abort', action='store_true', help='Stop the pool resize operation.', validator=validate_pool_resize_parameters)

Expand Down Expand Up @@ -72,8 +72,8 @@
for item in ['batch certificate delete', 'batch certificate create', 'batch pool resize', 'batch pool reset', 'batch job list', 'batch task create']:
register_extra_cli_argument(item, 'account_name', arg_group='Batch Account',
validator=validate_client_parameters,
help='Batch account name. Environment variable: AZURE_BATCH_ACCOUNT')
help='The Batch account name. Or specify at environment variable: AZURE_BATCH_ACCOUNT')
register_extra_cli_argument(item, 'account_key', arg_group='Batch Account',
help='Batch account key. Must be used in conjunction with Batch account name and endpoint. Environment variable: AZURE_BATCH_ACCESS_KEY')
help='The Batch account key. Or specify at environment variable: AZURE_BATCH_ACCESS_KEY')
register_extra_cli_argument(item, 'account_endpoint', arg_group='Batch Account',
help='Batch service endpoint. Environment variable: AZURE_BATCH_ENDPOINT')
help='Batch service endpoint. Or specify at environment variable: AZURE_BATCH_ENDPOINT')
Expand Up @@ -6,7 +6,7 @@
import os
import json

from six.moves.urllib.parse import urlsplit
from six.moves.urllib.parse import urlsplit # pylint: disable=import-error

from msrest.serialization import Deserializer
from msrest.exceptions import DeserializationError
Expand Down
Expand Up @@ -3,12 +3,9 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

try:
from urllib.parse import urlsplit
except ImportError:
from urlparse import urlsplit # pylint: disable=import-error
import json
import base64
from six.moves.urllib.parse import urlsplit # pylint: disable=import-error

from msrest.exceptions import DeserializationError, ValidationError, ClientRequestError
from azure.mgmt.batch import BatchManagementClient
Expand Down Expand Up @@ -123,7 +120,7 @@ def create_application_package(client, resource_group_name, account_name, # pyl
mgmt_client = get_mgmt_service_client(BatchManagementClient)
try:
mgmt_client.application.get(resource_group_name, account_name, application_id)
except: # pylint:disable=W0702
except Exception: # pylint:disable=broad-except
mgmt_client.application.create(resource_group_name, account_name, application_id)

result = client.create(resource_group_name, account_name, application_id, version)
Expand All @@ -139,21 +136,9 @@ def create_application_package(client, resource_group_name, account_name, # pyl

# Data plane custom commands

@transfer_doc(CertificateAddParameter)
def create_certificate(client, cert_file, thumbprint, thumbprint_algorithm, password=None):
if password:
certificate_format = 'pfx'
else:
certificate_format = 'cer'
with open(cert_file, "rb") as f:
data_bytes = f.read()
data = base64.b64encode(data_bytes).decode('utf-8')
cert = CertificateAddParameter(thumbprint, thumbprint_algorithm, data,
certificate_format=certificate_format,
password=password)
def _handle_batch_exception(action):
try:
client.add(cert)
return client.get(thumbprint_algorithm, thumbprint)
return action()
except BatchErrorException as ex:
try:
message = ex.error.message.value
Expand All @@ -167,69 +152,70 @@ def create_certificate(client, cert_file, thumbprint, thumbprint_algorithm, pass
raise CLIError(ex)


def delete_certificate(client, thumbprint, thumbprint_algorithm, abort=None):
try:
@transfer_doc(CertificateAddParameter)
def create_certificate(client, cert_file, thumbprint, thumbprint_algorithm, password=None):
def action():
client.add(cert)
return client.get(thumbprint_algorithm, thumbprint)

certificate_format = 'pfx' if password else 'cer'
with open(cert_file, "rb") as f:
data_bytes = f.read()
data = base64.b64encode(data_bytes).decode('utf-8')
cert = CertificateAddParameter(thumbprint, thumbprint_algorithm, data,
certificate_format=certificate_format,
password=password)
return _handle_batch_exception(action)


def delete_certificate(client, thumbprint, thumbprint_algorithm, abort=False):
def action():
if abort:
client.cancel_deletion(thumbprint_algorithm, thumbprint)
else:
client.delete(thumbprint_algorithm, thumbprint)
except BatchErrorException as ex:
try:
message = ex.error.message.value
if ex.error.values:
for detail in ex.error.values:
message += "\n{}: {}".format(detail.key, detail.value)
raise CLIError(message)
except AttributeError:
raise CLIError(ex)
except (ValidationError, ClientRequestError) as ex:
raise CLIError(ex)

return _handle_batch_exception(action)


@transfer_doc(PoolResizeParameter)
def resize_pool(client, pool_id, target_dedicated=None, # pylint:disable=too-many-arguments
resize_timeout=None, node_deallocation_option=None,
if_match=None, if_none_match=None, if_modified_since=None,
if_unmodified_since=None, abort=None):
if abort:
stop_resize_option = PoolStopResizeOptions(if_match=if_match,
if_none_match=if_none_match,
if_modified_since=if_modified_since,
if_unmodified_since=if_unmodified_since)
return client.stop_resize(pool_id, pool_stop_resize_options=stop_resize_option)
else:
param = PoolResizeParameter(target_dedicated,
resize_timeout=resize_timeout,
node_deallocation_option=node_deallocation_option)
resize_option = PoolResizeOptions(if_match=if_match,
if_none_match=if_none_match,
if_modified_since=if_modified_since,
if_unmodified_since=if_unmodified_since)
if_unmodified_since=None, abort=False):
def action():
if abort:
stop_resize_option = PoolStopResizeOptions(if_match=if_match,
if_none_match=if_none_match,
if_modified_since=if_modified_since,
if_unmodified_since=if_unmodified_since)
return client.stop_resize(pool_id, pool_stop_resize_options=stop_resize_option)
else:
param = PoolResizeParameter(target_dedicated,
resize_timeout=resize_timeout,
node_deallocation_option=node_deallocation_option)
resize_option = PoolResizeOptions(if_match=if_match,
if_none_match=if_none_match,
if_modified_since=if_modified_since,
if_unmodified_since=if_unmodified_since)
return client.resize(pool_id, param, pool_resize_options=resize_option)

try:
return client.resize(pool_id, param, pool_resize_options=resize_option)
except BatchErrorException as ex:
try:
message = ex.error.message.value
if ex.error.values:
for detail in ex.error.values:
message += "\n{}: {}".format(detail.key, detail.value)
raise CLIError(message)
except AttributeError:
raise CLIError(ex)
except (ValidationError, ClientRequestError) as ex:
raise CLIError(ex)
return _handle_batch_exception(action)


@transfer_doc(PoolUpdatePropertiesParameter, StartTask)
def update_pool(client, pool_id, json_file=None, command_line=None, # pylint:disable=too-many-arguments, W0613
def update_pool(client, pool_id, json_file=None, command_line=None, # pylint:disable=too-many-arguments
certificate_references=None, application_package_references=None, metadata=None):
def action():
client.update_properties(pool_id=pool_id, pool_update_properties_parameter=param)
return client.get(pool_id)

if json_file:
with open(json_file) as f:
json_obj = json.load(f)
param = None
try:
param = client._deserialize('PoolUpdatePropertiesParameter', json_obj) # pylint:disable=W0212
param = client._deserialize('PoolUpdatePropertiesParameter', json_obj) # pylint: disable=protected-access
except DeserializationError:
pass
if not param:
Expand All @@ -254,25 +240,11 @@ def update_pool(client, pool_id, json_file=None, command_line=None, # pylint:di

if command_line:
param.start_task = StartTask(command_line)
return _handle_batch_exception(action)

try:
client.update_properties(pool_id=pool_id, pool_update_properties_parameter=param)
return client.get(pool_id)
except BatchErrorException as ex:
try:
message = ex.error.message.value
if ex.error.values:
for detail in ex.error.values:
message += "\n{}: {}".format(detail.key, detail.value)
raise CLIError(message)
except AttributeError:
raise CLIError(ex)
except (ValidationError, ClientRequestError) as ex:
raise CLIError(ex)


def list_job(client, job_schedule_id=None, filter=None, select=None, expand=None): # pylint:disable=W0622
try:
def list_job(client, job_schedule_id=None, filter=None, select=None, expand=None): # pylint: disable=redefined-builtin
def action():
if job_schedule_id:
option1 = JobListFromJobScheduleOptions(filter=filter,
select=select,
Expand All @@ -284,33 +256,32 @@ def list_job(client, job_schedule_id=None, filter=None, select=None, expand=None
select=select,
expand=expand)
return list(client.list(job_list_options=option2))
except BatchErrorException as ex:
try:
message = ex.error.message.value
if ex.error.values:
for detail in ex.error.values:
message += "\n{}: {}".format(detail.key, detail.value)
raise CLIError(message)
except AttributeError:
raise CLIError(ex)
except (ValidationError, ClientRequestError) as ex:
raise CLIError(ex)

return _handle_batch_exception(action)


@transfer_doc(TaskAddParameter, TaskConstraints)
def create_task(client, job_id, json_file=None, task_id=None, command_line=None, # pylint:disable=too-many-arguments
resource_files=None, environment_settings=None, affinity_info=None,
max_wall_clock_time=None, retention_time=None, max_task_retry_count=None,
run_elevated=None, application_package_references=None):
def action():
if task is not None:
client.add(job_id=job_id, task=task)
return client.get(job_id=job_id, task_id=task.id)
else:
result = client.add_collection(job_id=job_id, value=tasks)
return result.value

task = None
if json_file:
with open(json_file) as f:
json_obj = json.load(f)
try:
task = client._deserialize('TaskAddParameter', json_obj) # pylint:disable=W0212
task = client._deserialize('TaskAddParameter', json_obj) # pylint: disable=protected-access
except DeserializationError:
try:
tasks = client._deserialize('[TaskAddParameter]', json_obj) # pylint:disable=W0212
tasks = client._deserialize('[TaskAddParameter]', json_obj) # pylint: disable=protected-access
except DeserializationError:
raise ValueError("JSON file '{}' is not in reqired format.".format(json_file))
else:
Expand All @@ -325,22 +296,4 @@ def create_task(client, job_id, json_file=None, task_id=None, command_line=None,
task.constraints = TaskConstraints(max_wall_clock_time=max_wall_clock_time,
retention_time=retention_time,
max_task_retry_count=max_task_retry_count)

try:
if task is not None:
client.add(job_id=job_id, task=task)
return client.get(job_id=job_id, task_id=task.id)
else:
result = client.add_collection(job_id=job_id, value=tasks)
return result.value
except BatchErrorException as ex:
try:
message = ex.error.message.value
if ex.error.values:
for detail in ex.error.values:
message += "\n{}: {}".format(detail.key, detail.value)
raise CLIError(message)
except AttributeError:
raise CLIError(ex)
except (ValidationError, ClientRequestError) as ex:
raise CLIError(ex)
return _handle_batch_exception(action)

0 comments on commit 824eefb

Please sign in to comment.