def _to_paths(*args, **kwargs): """Flatten a complex object into a dictionary of paths and values.""" keys = ["var", "prepend", "wantlist"] data = dict(zip(keys, args)) data.update(kwargs) aav = AnsibleArgSpecValidator( data=data, schema=DOCUMENTATION, name="to_paths" ) valid, errors, updated_data = aav.validate() if not valid: raise AnsibleFilterError(errors) return to_paths(**updated_data)
def run(self, terms, variables, **kwargs): if isinstance(terms, list): keys = ["var", "prepend"] terms = dict(zip(keys, terms)) terms.update(kwargs) aav = AnsibleArgSpecValidator(data=terms, schema=DOCUMENTATION, name="to_paths") valid, errors, updated_data = aav.validate() if not valid: raise AnsibleLookupError(errors) updated_data["wantlist"] = True res = to_paths(**updated_data) return res
def test_roundtrip_large(self): """Test the 1000 longest keys, otherwise this takes a _really_ long time""" big_json_path = os.path.join(os.path.dirname(__file__), "fixtures", "large.json") with open(big_json_path) as fhand: big_json = fhand.read() var = json.loads(big_json) paths = to_paths(var, prepend=None, wantlist=None) to_tests = heapq.nlargest(1000, list(paths.keys()), key=len) for to_test in to_tests: gotten = get_path(var, to_test, environment=self._environment, wantlist=False) self.assertEqual(gotten, paths[to_test])
def test_to_paths_prepend(self): var = {"a": {"b": {"c": {"d": [0, 1]}}}} expected = [{"var.a.b.c.d[0]": 0, "var.a.b.c.d[1]": 1}] result = to_paths(var, wantlist=True, prepend="var") self.assertEqual(result, expected)
def test_to_paths_special_char(self): var = {"a": {"b": {"c": {"Eth1/1": True}}}} expected = [{"a.b.c['Eth1/1']": True}] result = to_paths(var, prepend=None, wantlist=True) self.assertEqual(result, expected)
def test_to_paths_wantlist(self): var = {"a": {"b": {"c": {"d": [0, 1]}}}} expected = [{"a.b.c.d[0]": 0, "a.b.c.d[1]": 1}] result = to_paths(var, prepend=None, wantlist=True) self.assertEqual(result, expected)
def test_to_paths_only_empty_mapping(self): var = {} expected = {} result = to_paths(var, prepend=None, wantlist=None) self.assertEqual(result, expected)
def test_to_paths_only_empty_list(self): var = [] expected = [] result = to_paths(var, prepend=None, wantlist=None) self.assertEqual(result, expected)
def test_to_paths_list_of_empty_mapping(self): var = [{}, {}] expected = {"[0]": {}, "[1]": {}} result = to_paths(var, prepend=None, wantlist=None) self.assertEqual(result, expected)
def test_to_paths_list_of_empty_list(self): var = {"a": [[], []]} expected = {"a[0]": [], "a[1]": []} result = to_paths(var, prepend=None, wantlist=None) self.assertEqual(result, expected)