pep8 the ansible modules

By ignoring the appropriate tests that pep8 does we can properly run
*most* of the pep8 tests on all of our modules allowing for a more
consistent coding style.

Closes-Bug: #1528431
Change-Id: I33f27a250d06d4f044267aa3ad189e092789b8df
This commit is contained in:
SamYaple 2015-12-21 17:04:09 +00:00
parent ed82afa8e9
commit 4199634e98
8 changed files with 93 additions and 78 deletions

View File

@ -18,9 +18,9 @@ from ConfigParser import ConfigParser
from cStringIO import StringIO
import os
from ansible.runner.return_data import ReturnData
from ansible import utils
from ansible.utils import template
from ansible.runner.return_data import ReturnData
class ActionModule(object):
@ -30,7 +30,20 @@ class ActionModule(object):
def __init__(self, runner):
self.runner = runner
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
def read_config(self, source, inject, config):
# Only use config if present
if os.access(source, os.R_OK):
# template the source data locally & get ready to transfer
resultant = template.template_from_file(self.runner.basedir,
source, inject)
# Read in new results and merge this with the existing config
fakefile = StringIO(resultant)
config.readfp(fakefile)
fakefile.close()
def run(self, conn, tmp, module_name, module_args, inject,
complex_args=None, **kwargs):
args = {}
if complex_args:
args.update(complex_args)
@ -47,7 +60,6 @@ class ActionModule(object):
else:
inject.update(utils.parse_kv(extra_vars))
# Catch the case where sources is a str()
if not isinstance(sources, list):
sources = [sources]
@ -58,24 +70,18 @@ class ActionModule(object):
# template the source string
source = template.template(self.runner.basedir, source, inject)
# Only use config if present
if os.access(source, os.R_OK):
# template the source data locally & get ready to transfer
try:
resultant = template.template_from_file(self.runner.basedir, source, inject)
self.read_config(source, inject, config)
except Exception as e:
return ReturnData(conn=conn, comm_ok=False, result={'failed': True, 'msg': str(e)})
# Read in new results and merge this with the existing config
fakefile = StringIO(resultant)
config.readfp(fakefile)
fakefile.close()
return ReturnData(conn=conn, comm_ok=False,
result={'failed': True, 'msg': str(e)})
# Dump configparser to string via an emulated file
fakefile = StringIO()
config.write(fakefile)
# Template the file to fill out any variables
content = template.template(self.runner.basedir, fakefile.getvalue(), inject)
content = template.template(self.runner.basedir, fakefile.getvalue(),
inject)
fakefile.close()
# Ship this content over to a new file for use with the copy module
@ -87,4 +93,6 @@ class ActionModule(object):
original_basename=os.path.basename(source),
follow=True,
)
return self.runner._execute_module(conn, tmp, 'copy', '', inject=inject, complex_args=copy_module_args)
return self.runner._execute_module(conn, tmp, 'copy', '',
inject=inject,
complex_args=copy_module_args)

View File

@ -109,9 +109,9 @@ Distribute a to file many host without compression; Change permissions on dest:
import base64
import hashlib
import os
import sys
import zlib
def copy_from_host(module):
compress = module.params.get('compress')
src = module.params.get('src')
@ -121,7 +121,7 @@ def copy_from_host(module):
if not os.access(src, os.R_OK):
module.fail_json(msg="file is not readable: {}".format(src))
mode = oct(os.stat(src).st_mode & 0777)
mode = oct(os.stat(src).st_mode & 0o777)
with open(src, 'rb') as f:
raw_data = f.read()
@ -162,16 +162,16 @@ def copy_to_host(module):
module.exit_json(changed=True)
def main():
module = AnsibleModule(
argument_spec = dict(
compress = dict(default=True, type='bool'),
dest = dict(type='str'),
mode = dict(default='0644', type='str'),
sha1 = dict(default=None, type='str'),
src = dict(required=True, type='str')
)
compress=dict(default=True, type='bool'),
dest=dict(type='str'),
mode=dict(default='0644', type='str'),
sha1=dict(default=None, type='str'),
src=dict(required=True, type='str')
)
module = AnsibleModule(argument_spec)
dest = module.params.get('dest')
@ -183,7 +183,8 @@ def main():
except Exception as e:
module.exit_json(failed=True, changed=True, msg=repr(e))
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.basic import * # noqa
if __name__ == '__main__':
main()

View File

@ -45,12 +45,12 @@ EXAMPLES = '''
import json
import pyudev
def main():
module = AnsibleModule(
argument_spec = dict(
partition_name = dict(required=True, type='str')
)
partition_name=dict(required=True, type='str')
)
module = AnsibleModule(argument_spec)
partition_name = module.params.get('partition_name')
try:
@ -68,6 +68,6 @@ def main():
module.exit_json(failed=True, msg=repr(e))
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.basic import * # noqa
if __name__ == '__main__':
main()

View File

@ -14,8 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# This file is a barebones file needed to file a gap until Ansible 2.0. No error
# checking, no deletions, no updates. Idempotent creation only.
# This file is a barebones file needed to file a gap until Ansible 2.0. No
# error checking, no deletions, no updates. Idempotent creation only.
# If you look closely, you will see we arent _really_ using the shade module
# we just use it to slightly abstract the authentication model. As patches land
@ -24,18 +24,18 @@
import shade
def main():
module = AnsibleModule(
argument_spec = openstack_full_argument_spec(
description = dict(required=True, type='str'),
service_name = dict(required=True, type='str'),
service_type = dict(required=True, type='str'),
admin_url = dict(required=True, type='str'),
internal_url = dict(required=True, type='str'),
public_url = dict(required=True, type='str'),
endpoint_region = dict(required=True, type='str')
)
description=dict(required=True, type='str'),
service_name=dict(required=True, type='str'),
service_type=dict(required=True, type='str'),
admin_url=dict(required=True, type='str'),
internal_url=dict(required=True, type='str'),
public_url=dict(required=True, type='str'),
endpoint_region=dict(required=True, type='str')
)
module = AnsibleModule(argument_spec)
try:
description = module.params.pop('description')
@ -61,13 +61,15 @@ def main():
if _endpoint.service_id == service.id:
endpoint = _endpoint
else:
service = cloud.keystone_client.services.create(name=service_name,
service = cloud.keystone_client.services.create(
name=service_name,
service_type=service_type,
description=description)
if endpoint is None:
changed = True
cloud.keystone_client.endpoints.create(service_id=service.id,
cloud.keystone_client.endpoints.create(
service_id=service.id,
adminurl=admin_url,
internalurl=internal_url,
publicurl=public_url,
@ -78,7 +80,7 @@ def main():
module.exit_json(failed=True, changed=True, msg=e)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.openstack import *
from ansible.module_utils.basic import * # noqa
from ansible.module_utils.openstack import * # noqa
if __name__ == '__main__':
main()

View File

@ -14,8 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# This file is a barebones file needed to file a gap until Ansible 2.0. No error
# checking, no deletions, no updates. Idempotent creation only.
# This file is a barebones file needed to file a gap until Ansible 2.0. No
# error checking, no deletions, no updates. Idempotent creation only.
# If you look closely, you will see we arent _really_ using the shade module
# we just use it to slightly abstract the authentication model. As patches land
@ -24,15 +24,15 @@
import shade
def main():
module = AnsibleModule(
argument_spec = openstack_full_argument_spec(
password = dict(required=True, type='str'),
project = dict(required=True, type='str'),
role = dict(required=True, type='str'),
user = dict(required=True, type='str')
)
password=dict(required=True, type='str'),
project=dict(required=True, type='str'),
role=dict(required=True, type='str'),
user=dict(required=True, type='str')
)
module = AnsibleModule(argument_spec)
try:
password = module.params.pop('password')
@ -61,7 +61,8 @@ def main():
if not project:
changed = True
project = cloud.keystone_client.tenants.create(tenant_name=project_name)
project = cloud.keystone_client.tenants.create(
tenant_name=project_name)
if not role:
changed = True
@ -69,15 +70,19 @@ def main():
if not user:
changed = True
user = cloud.keystone_client.users.create(name=user_name, password=password, tenant_id=project.id)
cloud.keystone_client.roles.add_user_role(role=role.id, user=user.id, tenant=project.id)
user = cloud.keystone_client.users.create(name=user_name,
password=password,
tenant_id=project.id)
cloud.keystone_client.roles.add_user_role(role=role.id,
user=user.id,
tenant=project.id)
module.exit_json(changed=changed)
except Exception as e:
module.exit_json(failed=True, changed=True, msg=e)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.openstack import *
from ansible.module_utils.basic import * # noqa
from ansible.module_utils.openstack import * # noqa
if __name__ == '__main__':
main()

View File

@ -57,7 +57,7 @@ def main():
module.exit_json(failed=True, changed=True, msg=e)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.openstack import *
from ansible.module_utils.basic import * # noqa
from ansible.module_utils.openstack import * # noqa
if __name__ == '__main__':
main()

View File

@ -61,7 +61,6 @@ def main():
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.basic import * # noqa
if __name__ == '__main__':
main()

View File

@ -104,4 +104,4 @@ commands =
[flake8]
show-source = True
exclude=.eggs,.git,.tox,doc,ansible/library,ansible/action_plugins,docker/kolla-ansible
exclude=.eggs,.git,.tox,doc