From 8d42f5cbfa6890867c5459be3c5c4042f1ed3b54 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Thu, 24 Jul 2014 21:16:24 -0400 Subject: [PATCH] Smush ds removal --- lib/ansible/utils/__init__.py | 30 ++++-------------------------- lib/ansible/utils/splitter.py | 6 ++++++ test/units/TestUtils.py | 19 ------------------- 3 files changed, 10 insertions(+), 45 deletions(-) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index a8e3687f36..16eb872726 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -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): diff --git a/lib/ansible/utils/splitter.py b/lib/ansible/utils/splitter.py index ca2c37cd00..d4ae773b2b 100644 --- a/lib/ansible/utils/splitter.py +++ b/lib/ansible/utils/splitter.py @@ -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 + diff --git a/test/units/TestUtils.py b/test/units/TestUtils.py index 0cabb93b58..dff76c2664 100644 --- a/test/units/TestUtils.py +++ b/test/units/TestUtils.py @@ -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