def convert_dict(template, start_mark=(0, 0), end_mark=(0, 0)): """Convert dict to template""" if isinstance(template, dict): if not isinstance(template, dict_node): template = dict_node(template, start_mark, end_mark) for k, v in template.copy().items(): k_start_mark = start_mark k_end_mark = end_mark if isinstance(k, str_node): k_start_mark = k.start_mark k_end_mark = k.end_mark new_k = str_node(k, k_start_mark, k_end_mark) del template[k] template[new_k] = convert_dict(v, k_start_mark, k_end_mark) elif isinstance(template, list): if not isinstance(template, list_node): template = list_node(template, start_mark, end_mark) for i, v in enumerate(template): template[i] = convert_dict(v, start_mark, end_mark) return template
def construct_yaml_str(self, node): obj = SafeConstructor.construct_yaml_str(self, node) assert isinstance(obj, (six.string_types)) return str_node(obj, node.start_mark, node.end_mark)
def CfnJSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook, memo=None, _w=WHITESPACE.match, _ws=WHITESPACE_STR): """ Custom Cfn JSON Object to store keys with start and end times """ s, end = s_and_end orginal_end = end pairs = [] pairs_append = pairs.append # Backwards compatibility if memo is None: memo = {} memo_get = memo.setdefault # Use a slice to prevent IndexError from being raised, the following # check will raise a more specific ValueError if the string is empty nextchar = s[end:end + 1] # Normally we expect nextchar == '"' if nextchar != '"': if nextchar in _ws: end = _w(s, end).end() nextchar = s[end:end + 1] # Trivial empty object if nextchar == '}': if object_pairs_hook is not None: try: beg_mark, end_mark = get_beg_end_mark( s, orginal_end, end + 1) result = object_pairs_hook(pairs, beg_mark, end_mark) return result, end + 1 except DuplicateError as err: raise JSONDecodeError('Duplicate found {}'.format(err), s, end) except NullError as err: raise JSONDecodeError('Null Error {}'.format(err), s, end) pairs = {} if object_hook is not None: beg_mark, end_mark = get_beg_end_mark(s, orginal_end, end + 1) pairs = object_hook(pairs, beg_mark, end_mark) return pairs, end + 1 if nextchar != '"': raise JSONDecodeError( 'Expecting property name enclosed in double quotes', s, end) end += 1 while True: begin = end - 1 key, end = py_scanstring(s, end, strict) # print(lineno, colno, obj) # print(key, lineno, colno) key = memo_get(key, key) # To skip some function call overhead we optimize the fast paths where # the JSON key separator is ": " or just ":". if s[end:end + 1] != ':': end = _w(s, end).end() if s[end:end + 1] != ':': raise JSONDecodeError('Expecting \':\' delimiter', s, end) end += 1 try: if s[end] in _ws: end += 1 if s[end] in _ws: end = _w(s, end + 1).end() except IndexError: pass beg_mark, end_mark = get_beg_end_mark(s, begin, begin + len(key)) try: value, end = scan_once(s, end) except StopIteration as err: raise JSONDecodeError('Expecting value', s, str(err)) key_str = str_node(key, beg_mark, end_mark) pairs_append((key_str, value)) try: nextchar = s[end] if nextchar in _ws: end = _w(s, end + 1).end() nextchar = s[end] except IndexError: nextchar = '' end += 1 if nextchar == '}': break if nextchar != ',': raise JSONDecodeError('Expecting \',\' delimiter', s, end - 1) end = _w(s, end).end() nextchar = s[end:end + 1] end += 1 if nextchar != '"': raise JSONDecodeError( 'Expecting property name enclosed in double quotes', s, end - 1) if object_pairs_hook is not None: try: beg_mark, end_mark = get_beg_end_mark(s, orginal_end, end) result = object_pairs_hook(pairs, beg_mark, end_mark) except DuplicateError as err: raise JSONDecodeError('Duplicate found {}'.format(err), s, begin, key) except NullError as err: raise JSONDecodeError('Null Error {}'.format(err), s, begin, key) return result, end pairs = dict(pairs) if object_hook is not None: beg_mark, end_mark = get_beg_end_mark(s, orginal_end, end) pairs = object_hook(pairs, beg_mark, end_mark) return pairs, end