refactor(modules): fix lots of pylint warnings

This commit is contained in:
Jan Christian Grünhage 2020-10-02 19:50:02 +02:00
parent 12f01fc2a7
commit e6bd06524d
7 changed files with 34 additions and 47 deletions

View file

@ -66,9 +66,9 @@ try:
from nio import AsyncClient, LoginResponse
except ImportError:
MATRIX_IMP_ERR = traceback.format_exc()
matrix_found = False
MATRIX_FOUND = False
else:
matrix_found = True
MATRIX_FOUND = True
async def run_module():
module_args = dict(
@ -86,7 +86,7 @@ async def run_module():
supports_check_mode=True
)
if not matrix_found:
if not MATRIX_FOUND:
module.fail_json(msg=missing_required_lib('matrix-nio'), exception=MATRIX_IMP_ERR)
if module.check_mode:

View file

@ -53,9 +53,9 @@ try:
from nio import AsyncClient
except ImportError:
MATRIX_IMP_ERR = traceback.format_exc()
matrix_found = False
MATRIX_FOUND = False
else:
matrix_found = True
MATRIX_FOUND = True
async def run_module():
module_args = dict(
@ -72,7 +72,7 @@ async def run_module():
supports_check_mode=True
)
if not matrix_found:
if not MATRIX_FOUND:
module.fail_json(msg=missing_required_lib('matrix-nio'), exception=MATRIX_IMP_ERR)
if module.check_mode:

View file

@ -78,9 +78,9 @@ try:
from nio import AsyncClient, RoomBanError, RoomUnbanError, RoomKickError, RoomInviteError, RoomGetStateError
except ImportError:
MATRIX_IMP_ERR = traceback.format_exc()
matrix_found = False
MATRIX_FOUND = False
else:
matrix_found = True
MATRIX_FOUND = True
async def get_room_members(client, room_id, res):
member_resp = await client.room_get_state(room_id)
@ -97,40 +97,32 @@ async def ban_from_room(client, room_id, user_id, res):
if isinstance(ban_resp, RoomBanError):
res['msg'] = "Could not ban user={0} from roomId={1}".format(user_id, room_id)
raise Exception()
else:
res['changed'] = True
res['banned'].append(user_id)
return
res['changed'] = True
res['banned'].append(user_id)
async def unban_from_room(client, room_id, user_id, res):
ban_resp = await client.room_unban(room_id, user_id)
if isinstance(ban_resp, RoomUnbanError):
res['msg'] = "Could not unban user={0} from roomId={1}".format(user_id, room_id)
raise Exception()
else:
res['changed'] = True
res['unbanned'].append(user_id)
return
res['changed'] = True
res['unbanned'].append(user_id)
async def kick_from_room(client, room_id, user_id, res):
kick_resp = await client.room_kick(room_id, user_id)
if isinstance(kick_resp, RoomKickError):
res['msg'] = "Could not kick user={0} from roomId={1}".format(user_id, room_id)
raise Exception()
else:
res['changed'] = True
res['kicked'].append(user_id)
return
res['changed'] = True
res['kicked'].append(user_id)
async def invite_to_room(client, room_id, user_id, res):
invite_resp = await client.room_invite(room_id, user_id)
if isinstance(invite_resp, RoomInviteError):
res['msg'] = "Could not invite user={0} to roomId={1}".format(user_id, room_id)
raise Exception()
else:
res['changed'] = True
res['invited'].append(user_id)
return
res['changed'] = True
res['invited'].append(user_id)
async def run_module():
@ -157,7 +149,7 @@ async def run_module():
argument_spec=module_args,
supports_check_mode=True
)
if not matrix_found:
if not MATRIX_FOUND:
module.fail_json(msg=missing_required_lib('matrix-nio'), exception=MATRIX_IMP_ERR)
if module.check_mode:

View file

@ -83,9 +83,9 @@ try:
from nio import AsyncClient
except ImportError:
MATRIX_IMP_ERR = traceback.format_exc()
matrix_found = False
MATRIX_FOUND = False
else:
matrix_found = True
MATRIX_FOUND = True
async def get_client_with_token(hs_url, token):
client = AsyncClient(hs_url)
@ -121,7 +121,7 @@ async def run_module():
supports_check_mode=True
)
if not matrix_found:
if not MATRIX_FOUND:
module.fail_json(msg=missing_required_lib('matrix-nio'), exception=MATRIX_IMP_ERR)
if module.check_mode:

View file

@ -54,7 +54,6 @@ room_id:
import traceback
import asyncio
import re
import sys
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
@ -63,9 +62,9 @@ try:
from nio import (AsyncClient, RoomResolveAliasResponse, JoinedRoomsError, RoomCreateResponse, JoinResponse)
except ImportError:
MATRIX_IMP_ERR = traceback.format_exc()
matrix_found = False
MATRIX_FOUND = False
else:
matrix_found = True
MATRIX_FOUND = True
async def run_module():
module_args = dict(
@ -84,7 +83,7 @@ async def run_module():
supports_check_mode=True
)
if not matrix_found:
if not MATRIX_FOUND:
module.fail_json(msg=missing_required_lib('matrix-nio'), exception=MATRIX_IMP_ERR)
if module.check_mode:
@ -111,7 +110,7 @@ async def run_module():
else:
# Try to join room
join_resp = await client.join(module.params['alias'])
# If successful, return, changed=true
if isinstance(join_resp, JoinResponse):
result = {"room_id": join_resp.room_id, "changed": True}
@ -130,7 +129,6 @@ async def run_module():
if isinstance(create_room_resp, RoomCreateResponse):
result = {"room_id": create_room_resp.room_id, "changed": True}
else:
json_resp = await create_room_resp.transport_response.json()
failed = True
result = {"msg": "Room does not exist but couldn't be created either: {0}".format(create_room_resp)}

View file

@ -2,14 +2,14 @@
# Copyright: (c) 2018, Emmanouil Kampitakis <info@kampitakis.de>
# Apache 2.0
import os
from ansible.module_utils.basic import AnsibleModule
from signedjson import key
import os
def write_signing_key(path):
with open(path,'w') as f:
with open(path,'w') as file:
key.write_signing_keys(
f,
file,
[key.generate_signing_key('first')]
)
@ -35,9 +35,8 @@ def run_module():
if not signing_key_exists:
result['changed'] = True
if module.check_mode:
return result
if module.check_mode:
module.exit_json(**result)
write_signing_key(signing_key_path)
module.exit_json(**result)
@ -47,5 +46,3 @@ def main():
if __name__ == '__main__':
main()

View file

@ -56,7 +56,7 @@ EXAMPLES = '''
event_type: m.room.server_acl
state_key: ""
content:
allow:
allow:
- "*"
deny:
- "bad-server.one"
@ -84,9 +84,9 @@ try:
from nio import AsyncClient, RoomGetStateEventResponse, RoomPutStateResponse, JoinedRoomsError
except ImportError:
MATRIX_IMP_ERR = traceback.format_exc()
matrix_found = False
MATRIX_FOUND = False
else:
matrix_found = True
MATRIX_FOUND = True
async def run_module():
@ -109,7 +109,7 @@ async def run_module():
supports_check_mode=True
)
if not matrix_found:
if not MATRIX_FOUND:
module.fail_json(msg=missing_required_lib('matrix-nio'), exception=MATRIX_IMP_ERR)
if module.check_mode:
@ -162,7 +162,7 @@ async def run_module():
if failed:
module.fail_json(**result)
else:
module.exit_json(**result)
module.exit_json(**result)
def main():
asyncio.run(run_module())