mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 13:20:23 -07:00
Refactor the parameter splitting in ini lookup to handle more path formats (#16477)
Refactor the parameter splitting in ini lookup to handle more path formats. Fixes #16468
This commit is contained in:
parent
3afe50dfe2
commit
4ba60d00c8
2 changed files with 96 additions and 4 deletions
|
@ -19,12 +19,41 @@ __metaclass__ = type
|
|||
|
||||
from io import StringIO
|
||||
import os
|
||||
import ConfigParser
|
||||
import re
|
||||
|
||||
try:
|
||||
# python2
|
||||
import ConfigParser as configparser
|
||||
except ImportError:
|
||||
# python3
|
||||
import configparser
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
|
||||
|
||||
def _parse_params(term):
|
||||
'''Safely split parameter term to preserve spaces'''
|
||||
|
||||
keys = ['key', 'section', 'file', 're']
|
||||
params = {}
|
||||
for k in keys:
|
||||
params[k] = ''
|
||||
|
||||
thiskey = 'key'
|
||||
for idp,phrase in enumerate(term.split()):
|
||||
for k in keys:
|
||||
if ('%s=' % k) in phrase:
|
||||
thiskey = k
|
||||
if idp == 0 or not params[thiskey]:
|
||||
params[thiskey] = phrase
|
||||
else:
|
||||
params[thiskey] += ' ' + phrase
|
||||
|
||||
rparams = [params[x] for x in keys if params[x]]
|
||||
return rparams
|
||||
|
||||
|
||||
class LookupModule(LookupBase):
|
||||
|
||||
def read_properties(self, filename, key, dflt, is_regexp):
|
||||
|
@ -46,7 +75,7 @@ class LookupModule(LookupBase):
|
|||
# Retrieve a single value
|
||||
try:
|
||||
value = self.cp.get(section, key)
|
||||
except ConfigParser.NoOptionError:
|
||||
except configparser.NoOptionError:
|
||||
return dflt
|
||||
return value
|
||||
|
||||
|
@ -54,11 +83,11 @@ class LookupModule(LookupBase):
|
|||
|
||||
basedir = self.get_basedir(variables)
|
||||
self.basedir = basedir
|
||||
self.cp = ConfigParser.ConfigParser()
|
||||
self.cp = configparser.ConfigParser()
|
||||
|
||||
ret = []
|
||||
for term in terms:
|
||||
params = term.split()
|
||||
params = _parse_params(term)
|
||||
key = params[0]
|
||||
|
||||
paramvals = {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue