Smush ds removal

This commit is contained in:
Michael DeHaan 2014-07-24 21:16:24 -04:00 committed by James Cammarata
parent b8a4ba26f0
commit 8d42f5cbfa
3 changed files with 10 additions and 45 deletions

View file

@ -29,7 +29,7 @@ from ansible import __version__
from ansible.utils.display_functions import *
from ansible.utils.plugins import *
from ansible.callbacks import display
from ansible.utils.splitter import split_args
from ansible.utils.splitter import split_args, unquote
import ansible.constants as C
import ast
import time
@ -446,28 +446,6 @@ def merge_module_args(current_args, new_args):
module_args = "%s=%s %s" % (k, pipes.quote(v), module_args)
return module_args.strip()
def smush_braces(data):
''' smush Jinaj2 braces so unresolved templates like {{ foo }} don't get parsed weird by key=value code '''
while '{{ ' in data:
data = data.replace('{{ ', '{{')
while ' }}' in data:
data = data.replace(' }}', '}}')
return data
def smush_ds(data):
# things like key={{ foo }} are not handled by shlex.split well, so preprocess any YAML we load
# so we do not have to call smush elsewhere
if type(data) == list:
return [ smush_ds(x) for x in data ]
elif type(data) == dict:
for (k,v) in data.items():
data[k] = smush_ds(v)
return data
elif isinstance(data, basestring):
return smush_braces(data)
else:
return data
def parse_yaml(data, path_hint=None):
''' convert a yaml string to a data structure. Also supports JSON, ssssssh!!!'''
@ -486,7 +464,7 @@ def parse_yaml(data, path_hint=None):
# else this is pretty sure to be a YAML document
loaded = yaml.safe_load(data)
return smush_ds(loaded)
return loaded
def process_common_errors(msg, probline, column):
replaced = probline.replace(" ","")
@ -666,7 +644,7 @@ def parse_kv(args):
# attempting to split a unicode here does bad things
args = args.encode('utf-8')
try:
vargs = shlex.split(args, posix=True)
vargs = split_args(args)
except ValueError, ve:
if 'no closing quotation' in str(ve).lower():
raise errors.AnsibleError("error parsing argument string, try quoting the entire line.")
@ -676,7 +654,7 @@ def parse_kv(args):
for x in vargs:
if "=" in x:
k, v = x.split("=",1)
options[k] = v
options[k] = unquote(v)
return options
def merge_hash(a, b):

View file

@ -149,3 +149,9 @@ def split_args(args):
params = [x.decode('utf-8') for x in params]
return params
def unquote(data):
''' removes first and last quotes from a string, if the string starts and ends with the same quotes '''
if len(data) > 0 and (data[0] == '"' and data[-1] == '"' or data[0] == "'" and data[-1] == "'"):
return data[1:-1]
return data

View file

@ -246,24 +246,6 @@ class TestUtils(unittest.TestCase):
# Just a string
self.assertEqual(ansible.utils.parse_json('foo'), dict(failed=True, parsed=False, msg='foo'))
def test_smush_braces(self):
self.assertEqual(ansible.utils.smush_braces('{{ foo}}'), '{{foo}}')
self.assertEqual(ansible.utils.smush_braces('{{foo }}'), '{{foo}}')
self.assertEqual(ansible.utils.smush_braces('{{ foo }}'), '{{foo}}')
def test_smush_ds(self):
# list
self.assertEqual(ansible.utils.smush_ds(['foo={{ foo }}']), ['foo={{foo}}'])
# dict
self.assertEqual(ansible.utils.smush_ds(dict(foo='{{ foo }}')), dict(foo='{{foo}}'))
# string
self.assertEqual(ansible.utils.smush_ds('foo={{ foo }}'), 'foo={{foo}}')
# int
self.assertEqual(ansible.utils.smush_ds(0), 0)
def test_parse_yaml(self):
#json
self.assertEqual(ansible.utils.parse_yaml('{"foo": "bar"}'), dict(foo='bar'))
@ -681,7 +663,6 @@ class TestUtils(unittest.TestCase):
def test_split_args(self):
# split_args is a smarter shlex.split for the needs of the way ansible uses it
# TODO: FIXME: should this survive, retire smush_ds
def _split_info(input, desired, actual):
print "SENT: ", input