Make configparser imports python3 ready

the ConfigParser module was renamed to configparser in Python3.  Use
six.moves to import it so that the modules will function on Python3.
This commit is contained in:
Toshio Kuratomi 2017-02-03 10:01:53 -08:00
parent 272ff10fa1
commit f75ffe46db
8 changed files with 41 additions and 45 deletions

View file

@ -18,7 +18,6 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# #
import ConfigParser
import json import json
import os import os
import re import re
@ -29,7 +28,9 @@ import inspect
from distutils.version import LooseVersion from distutils.version import LooseVersion
from os.path import expanduser from os.path import expanduser
from ansible.module_utils.basic import *
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six.moves import configparser
AZURE_COMMON_ARGS = dict( AZURE_COMMON_ARGS = dict(
profile=dict(type='str'), profile=dict(type='str'),
@ -299,7 +300,7 @@ class AzureRMModuleBase(object):
def _get_profile(self, profile="default"): def _get_profile(self, profile="default"):
path = expanduser("~/.azure/credentials") path = expanduser("~/.azure/credentials")
try: try:
config = ConfigParser.ConfigParser() config = configparser.ConfigParser()
config.read(path) config.read(path)
except Exception as exc: except Exception as exc:
self.fail("Failed to access {0}. Check that the file exists and you have read " self.fail("Failed to access {0}. Check that the file exists and you have read "

View file

@ -36,12 +36,12 @@ except ImportError:
# Let snippet from module_utils/basic.py return a proper error in this case # Let snippet from module_utils/basic.py return a proper error in this case
pass pass
import urllib import os
from ConfigParser import ConfigParser, NoSectionError
# import module snippets # import module snippets
from ansible.module_utils.urls import * from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.basic import * from ansible.module_utils.six.moves import configparser
from ansible.module_utils.urls import fetch_url
EXO_DNS_BASEURL="https://api.exoscale.ch/dns/v1" EXO_DNS_BASEURL="https://api.exoscale.ch/dns/v1"
@ -115,7 +115,7 @@ class ExoDns(object):
raise SystemExit("Config file not found. Tried {0}".format( raise SystemExit("Config file not found. Tried {0}".format(
", ".join(paths))) ", ".join(paths)))
conf = ConfigParser() conf = configparser.ConfigParser()
conf.read(paths) conf.read(paths)
return dict(conf.items(ini_group)) return dict(conf.items(ini_group))

View file

@ -29,7 +29,8 @@
import os import os
import re import re
import types import types
import ConfigParser
from ansible.module_utils.six.moves import configparser
class RegistrationBase(object): class RegistrationBase(object):
@ -59,7 +60,7 @@ class RegistrationBase(object):
def update_plugin_conf(self, plugin, enabled=True): def update_plugin_conf(self, plugin, enabled=True):
plugin_conf = '/etc/yum/pluginconf.d/%s.conf' % plugin plugin_conf = '/etc/yum/pluginconf.d/%s.conf' % plugin
if os.path.isfile(plugin_conf): if os.path.isfile(plugin_conf):
cfg = ConfigParser.ConfigParser() cfg = configparser.ConfigParser()
cfg.read([plugin_conf]) cfg.read([plugin_conf])
if enabled: if enabled:
cfg.set('main', 'enabled', 1) cfg.set('main', 'enabled', 1)
@ -87,7 +88,7 @@ class Rhsm(RegistrationBase):
''' '''
# Read RHSM defaults ... # Read RHSM defaults ...
cp = ConfigParser.ConfigParser() cp = configparser.ConfigParser()
cp.read(rhsm_conf) cp.read(rhsm_conf)
# Add support for specifying a default value w/o having to standup some configuration # Add support for specifying a default value w/o having to standup some configuration
@ -99,7 +100,7 @@ class Rhsm(RegistrationBase):
else: else:
return default return default
cp.get_option = types.MethodType(get_option_default, cp, ConfigParser.ConfigParser) cp.get_option = types.MethodType(get_option_default, cp, configparser.ConfigParser)
return cp return cp

View file

@ -113,7 +113,7 @@ after:
type: string type: string
''' '''
import ConfigParser import os
try: try:
from pymongo.errors import ConnectionFailure from pymongo.errors import ConnectionFailure
@ -130,13 +130,17 @@ except ImportError:
else: else:
pymongo_found = True pymongo_found = True
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.six.moves import configparser
# ========================================= # =========================================
# MongoDB module specific support methods. # MongoDB module specific support methods.
# #
def load_mongocnf(): def load_mongocnf():
config = ConfigParser.RawConfigParser() config = configparser.RawConfigParser()
mongocnf = os.path.expanduser('~/.mongodb.cnf') mongocnf = os.path.expanduser('~/.mongodb.cnf')
try: try:
@ -145,7 +149,7 @@ def load_mongocnf():
user=config.get('client', 'user'), user=config.get('client', 'user'),
password=config.get('client', 'pass') password=config.get('client', 'pass')
) )
except (ConfigParser.NoOptionError, IOError): except (configparser.NoOptionError, IOError):
return False return False
return creds return creds
@ -231,9 +235,5 @@ def main():
after=value) after=value)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.pycompat24 import get_exception
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -189,9 +189,10 @@ EXAMPLES = '''
''' '''
import os
import ssl as ssl_lib import ssl as ssl_lib
import ConfigParser
from distutils.version import LooseVersion from distutils.version import LooseVersion
try: try:
from pymongo.errors import ConnectionFailure from pymongo.errors import ConnectionFailure
from pymongo.errors import OperationFailure from pymongo.errors import OperationFailure
@ -207,6 +208,11 @@ except ImportError:
else: else:
pymongo_found = True pymongo_found = True
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.six.moves import configparser
# ========================================= # =========================================
# MongoDB module specific support methods. # MongoDB module specific support methods.
# #
@ -279,7 +285,7 @@ def user_remove(module, client, db_name, user):
module.exit_json(changed=False, user=user) module.exit_json(changed=False, user=user)
def load_mongocnf(): def load_mongocnf():
config = ConfigParser.RawConfigParser() config = configparser.RawConfigParser()
mongocnf = os.path.expanduser('~/.mongodb.cnf') mongocnf = os.path.expanduser('~/.mongodb.cnf')
try: try:
@ -288,7 +294,7 @@ def load_mongocnf():
user=config.get('client', 'user'), user=config.get('client', 'user'),
password=config.get('client', 'pass') password=config.get('client', 'pass')
) )
except (ConfigParser.NoOptionError, IOError): except (configparser.NoOptionError, IOError):
return False return False
return creds return creds
@ -446,9 +452,6 @@ def main():
module.exit_json(changed=True, user=user) module.exit_json(changed=True, user=user)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.pycompat24 import get_exception
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -196,14 +196,15 @@ EXAMPLES = '''
command: DISABLE_FAILURE_PREDICTION command: DISABLE_FAILURE_PREDICTION
''' '''
import ConfigParser
import types import types
import time import time
import os.path import os.path
###################################################################### from ansible.module_utils.basic import AnsibleModule
######################################################################
def which_cmdfile(): def which_cmdfile():
locations = [ locations = [
# rhel # rhel
@ -1079,9 +1080,6 @@ class Nagios(object):
self.module.exit_json(nagios_commands=self.command_results, self.module.exit_json(nagios_commands=self.command_results,
changed=True) changed=True)
######################################################################
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -160,8 +160,10 @@ EXAMPLES = '''
import os import os
import re import re
import types import types
import ConfigParser
import shlex from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.six.moves import configparser
class RegistrationBase(object): class RegistrationBase(object):
@ -191,7 +193,7 @@ class RegistrationBase(object):
def update_plugin_conf(self, plugin, enabled=True): def update_plugin_conf(self, plugin, enabled=True):
plugin_conf = '/etc/yum/pluginconf.d/%s.conf' % plugin plugin_conf = '/etc/yum/pluginconf.d/%s.conf' % plugin
if os.path.isfile(plugin_conf): if os.path.isfile(plugin_conf):
cfg = ConfigParser.ConfigParser() cfg = configparser.ConfigParser()
cfg.read([plugin_conf]) cfg.read([plugin_conf])
if enabled: if enabled:
cfg.set('main', 'enabled', 1) cfg.set('main', 'enabled', 1)
@ -219,7 +221,7 @@ class Rhsm(RegistrationBase):
''' '''
# Read RHSM defaults ... # Read RHSM defaults ...
cp = ConfigParser.ConfigParser() cp = configparser.ConfigParser()
cp.read(rhsm_conf) cp.read(rhsm_conf)
# Add support for specifying a default value w/o having to standup some configuration # Add support for specifying a default value w/o having to standup some configuration
@ -231,7 +233,7 @@ class Rhsm(RegistrationBase):
else: else:
return default return default
cp.get_option = types.MethodType(get_option_default, cp, ConfigParser.ConfigParser) cp.get_option = types.MethodType(get_option_default, cp, configparser.ConfigParser)
return cp return cp
@ -559,8 +561,5 @@ def main():
module.exit_json(changed=True, msg="System successfully unregistered from %s." % server_hostname) module.exit_json(changed=True, msg="System successfully unregistered from %s." % server_hostname)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -21,13 +21,7 @@ from io import StringIO
import os import os
import re import re
try: from ansible.compat.six.moves import configparser
# python2
import ConfigParser as configparser
except ImportError:
# python3
import configparser
from ansible.errors import AnsibleError from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase from ansible.plugins.lookup import LookupBase
from ansible.module_utils._text import to_bytes, to_text from ansible.module_utils._text import to_bytes, to_text