diff options
author | Anthon van der Neut <anthon@mnt.org> | 2021-03-09 09:02:50 +0100 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2021-03-09 09:02:50 +0100 |
commit | e73562c6f14d1d71a9fea174d58465e1b13f68af (patch) | |
tree | 309851cca7d411b31c27753555871d493282c7f0 /util.py | |
parent | 96839d9f64f4698bdc519cbfbd48d51178460714 (diff) | |
download | ruamel.yaml-e73562c6f14d1d71a9fea174d58465e1b13f68af.tar.gz |
remove python 2 specific code
add future deprecation warning to old style functions
Diffstat (limited to 'util.py')
-rw-r--r-- | util.py | 32 |
1 files changed, 15 insertions, 17 deletions
@@ -4,12 +4,9 @@ some helper functions that might be generally useful """ -from __future__ import absolute_import, print_function - from functools import partial import re -from .compat import text_type, binary_type if False: # MYPY from typing import Any, Dict, Optional, List, Text # NOQA @@ -66,9 +63,9 @@ def load_yaml_guess_indent(stream, **kw): - if there are no block sequences, indent is taken from nested mappings, block sequence indent is unset (None) in that case """ - from .main import round_trip_load + from .main import YAML - # load a YAML document, guess the indentation, if you use TABs you're on your own + # load a YAML document, guess the indentation, if you use TABs you are on your own def leading_spaces(line): # type: (Any) -> int idx = 0 @@ -76,9 +73,9 @@ def load_yaml_guess_indent(stream, **kw): idx += 1 return idx - if isinstance(stream, text_type): + if isinstance(stream, str): yaml_str = stream # type: Any - elif isinstance(stream, binary_type): + elif isinstance(stream, bytes): # most likely, but the Reader checks BOM for this yaml_str = stream.decode('utf-8') else: @@ -117,7 +114,8 @@ def load_yaml_guess_indent(stream, **kw): prev_line_key_only = None if indent is None and map_indent is not None: indent = map_indent - return round_trip_load(yaml_str, **kw), indent, block_seq_indent + yaml = YAML() + return yaml.load(yaml_str, **kw), indent, block_seq_indent def configobj_walker(cfg): @@ -145,28 +143,28 @@ def _walk_section(s, level=0): from configobj import Section assert isinstance(s, Section) - indent = u' ' * level + indent = ' ' * level for name in s.scalars: for c in s.comments[name]: yield indent + c.strip() x = s[name] - if u'\n' in x: - i = indent + u' ' - x = u'|\n' + i + x.strip().replace(u'\n', u'\n' + i) + if '\n' in x: + i = indent + ' ' + x = '|\n' + i + x.strip().replace('\n', '\n' + i) elif ':' in x: - x = u"'" + x.replace(u"'", u"''") + u"'" - line = u'{0}{1}: {2}'.format(indent, name, x) + x = "'" + x.replace("'", "''") + "'" + line = '{0}{1}: {2}'.format(indent, name, x) c = s.inline_comments[name] if c: - line += u' ' + c + line += ' ' + c yield line for name in s.sections: for c in s.comments[name]: yield indent + c.strip() - line = u'{0}{1}:'.format(indent, name) + line = '{0}{1}:'.format(indent, name) c = s.inline_comments[name] if c: - line += u' ' + c + line += ' ' + c yield line for val in _walk_section(s[name], level=level + 1): yield val |