1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

ini_file: PEP8 compliancy, pylint and docs (#30851)

This PR includes;
- PEP8 compliancy fixes
- pylint fixes
- Documentation updates
This commit is contained in:
Dag Wieers 2017-09-25 17:39:46 +02:00 committed by ansibot
parent 5646d9960f
commit 04b5c17578
2 changed files with 38 additions and 52 deletions

View file

@ -14,7 +14,6 @@ ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'], 'status': ['preview'],
'supported_by': 'community'} 'supported_by': 'community'}
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
module: ini_file module: ini_file
@ -33,9 +32,8 @@ options:
description: description:
- Path to the INI-style file; this file is created if required. - Path to the INI-style file; this file is created if required.
- Before 2.3 this option was only usable as I(dest). - Before 2.3 this option was only usable as I(dest).
aliases: [ dest ]
required: true required: true
default: null
aliases: ['dest']
section: section:
description: description:
- Section name in INI file. This is added if C(state=present) automatically when - Section name in INI file. This is added if C(state=present) automatically when
@ -43,48 +41,39 @@ options:
- If left empty or set to `null`, the I(option) will be placed before the first I(section). - If left empty or set to `null`, the I(option) will be placed before the first I(section).
Using `null` is also required if the config format does not support sections. Using `null` is also required if the config format does not support sections.
required: true required: true
default: null
option: option:
description: description:
- If set (required for changing a I(value)), this is the name of the option. - If set (required for changing a I(value)), this is the name of the option.
- May be omitted if adding/removing a whole I(section). - May be omitted if adding/removing a whole I(section).
required: false
default: null
value: value:
description: description:
- The string value to be associated with an I(option). May be omitted when removing an I(option). - The string value to be associated with an I(option). May be omitted when removing an I(option).
required: false
default: null
backup: backup:
description: description:
- Create a backup file including the timestamp information so you can get - Create a backup file including the timestamp information so you can get
the original file back if you somehow clobbered it incorrectly. the original file back if you somehow clobbered it incorrectly.
required: false type: bool
default: "no" default: 'no'
choices: [ "yes", "no" ]
others: others:
description: description:
- All arguments accepted by the M(file) module also work here - All arguments accepted by the M(file) module also work here
required: false
state: state:
description: description:
- If set to C(absent) the option or section will be removed if present instead of created. - If set to C(absent) the option or section will be removed if present instead of created.
required: false choices: [ absent, present ]
default: "present" default: present
choices: [ "present", "absent" ]
no_extra_spaces: no_extra_spaces:
description: description:
- Do not insert spaces before and after '=' symbol - Do not insert spaces before and after '=' symbol
required: false type: bool
default: false default: 'no'
version_added: "2.1" version_added: "2.1"
create: create:
required: false
choices: [ "yes", "no" ]
default: "yes"
description: description:
- If set to 'no', the module will fail if the file does not already exist. - If set to 'no', the module will fail if the file does not already exist.
By default it will create the file if it is missing. By default it will create the file if it is missing.
type: bool
default: 'yes'
version_added: "2.2" version_added: "2.2"
notes: notes:
- While it is possible to add an I(option) without specifying a I(value), this makes - While it is possible to add an I(option) without specifying a I(value), this makes
@ -92,8 +81,8 @@ notes:
- As of Ansible 2.3, the I(dest) option has been changed to I(path) as default, but - As of Ansible 2.3, the I(dest) option has been changed to I(path) as default, but
I(dest) still works as well. I(dest) still works as well.
author: author:
- "Jan-Piet Mens (@jpmens)" - Jan-Piet Mens (@jpmens)
- "Ales Nosek (@noseka1)" - Ales Nosek (@noseka1)
''' '''
EXAMPLES = ''' EXAMPLES = '''
@ -118,35 +107,30 @@ EXAMPLES = '''
import os import os
import re import re
# import module snippets
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
# ==============================================================
# match_opt
def match_opt(option, line): def match_opt(option, line):
option = re.escape(option) option = re.escape(option)
return re.match('( |\t)*%s( |\t)*=' % option, line) \ return re.match('( |\t)*%s( |\t)*=' % option, line) \
or re.match('#( |\t)*%s( |\t)*=' % option, line) \ or re.match('#( |\t)*%s( |\t)*=' % option, line) \
or re.match(';( |\t)*%s( |\t)*=' % option, line) or re.match(';( |\t)*%s( |\t)*=' % option, line)
# ==============================================================
# match_active_opt
def match_active_opt(option, line): def match_active_opt(option, line):
option = re.escape(option) option = re.escape(option)
return re.match('( |\t)*%s( |\t)*=' % option, line) return re.match('( |\t)*%s( |\t)*=' % option, line)
# ==============================================================
# do_ini
def do_ini(module, filename, section=None, option=None, value=None, def do_ini(module, filename, section=None, option=None, value=None,
state='present', backup=False, no_extra_spaces=False, create=True): state='present', backup=False, no_extra_spaces=False, create=True):
diff = {'before': '', diff = dict(
'after': '', before='',
'before_header': '%s (content)' % filename, after='',
'after_header': '%s (content)' % filename} before_header='%s (content)' % filename,
after_header='%s (content)' % filename,
)
if not os.path.exists(filename): if not os.path.exists(filename):
if not create: if not create:
@ -263,24 +247,22 @@ def do_ini(module, filename, section=None, option=None, value=None,
return (changed, backup_file, diff, msg) return (changed, backup_file, diff, msg)
# ==============================================================
# main
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec=dict(
path = dict(required=True, aliases=['dest'], type='path'), path=dict(type='path', required=True, aliases=['dest']),
section = dict(required=True), section=dict(type='str', required=True),
option = dict(required=False), option=dict(type='str'),
value = dict(required=False), value=dict(type='str'),
backup = dict(default='no', type='bool'), backup=dict(type='bool', default=False),
state = dict(default='present', choices=['present', 'absent']), state=dict(type='str', default='present', choices=['absent', 'present']),
no_extra_spaces = dict(required=False, default=False, type='bool'), no_extra_spaces=dict(type='bool', default=False),
create=dict(default=True, type='bool') create=dict(type='bool', default=True)
), ),
add_file_common_args = True, add_file_common_args=True,
supports_check_mode = True supports_check_mode=True,
) )
path = module.params['path'] path = module.params['path']
@ -292,13 +274,18 @@ def main():
no_extra_spaces = module.params['no_extra_spaces'] no_extra_spaces = module.params['no_extra_spaces']
create = module.params['create'] create = module.params['create']
(changed,backup_file,diff,msg) = do_ini(module, path, section, option, value, state, backup, no_extra_spaces, create) (changed, backup_file, diff, msg) = do_ini(module, path, section, option, value, state, backup, no_extra_spaces, create)
if not module.check_mode and os.path.exists(path): if not module.check_mode and os.path.exists(path):
file_args = module.load_file_common_arguments(module.params) file_args = module.load_file_common_arguments(module.params)
changed = module.set_fs_attributes_if_different(file_args, changed) changed = module.set_fs_attributes_if_different(file_args, changed)
results = { 'changed': changed, 'msg': msg, 'path': path, 'diff': diff } results = dict(
changed=changed,
diff=diff,
msg=msg,
path=path,
)
if backup_file is not None: if backup_file is not None:
results['backup_file'] = backup_file results['backup_file'] = backup_file

View file

@ -200,7 +200,6 @@ lib/ansible/modules/database/vertica/vertica_user.py
lib/ansible/modules/files/archive.py lib/ansible/modules/files/archive.py
lib/ansible/modules/files/assemble.py lib/ansible/modules/files/assemble.py
lib/ansible/modules/files/blockinfile.py lib/ansible/modules/files/blockinfile.py
lib/ansible/modules/files/ini_file.py
lib/ansible/modules/files/replace.py lib/ansible/modules/files/replace.py
lib/ansible/modules/files/synchronize.py lib/ansible/modules/files/synchronize.py
lib/ansible/modules/files/tempfile.py lib/ansible/modules/files/tempfile.py