diff options
Diffstat (limited to '_test/lib/canonical.py')
-rw-r--r-- | _test/lib/canonical.py | 112 |
1 files changed, 55 insertions, 57 deletions
diff --git a/_test/lib/canonical.py b/_test/lib/canonical.py index af2c3cf..1cab1ff 100644 --- a/_test/lib/canonical.py +++ b/_test/lib/canonical.py @@ -3,7 +3,6 @@ import ruamel.yaml from ruamel.yaml.composer import Composer from ruamel.yaml.constructor import Constructor from ruamel.yaml.resolver import Resolver -from ruamel.yaml.compat import unichr, PY3 class CanonicalError(ruamel.yaml.YAMLError): @@ -13,14 +12,11 @@ class CanonicalError(ruamel.yaml.YAMLError): class CanonicalScanner: def __init__(self, data): try: - if PY3: - if isinstance(data, bytes): - data = data.decode('utf-8') - else: - data = unicode(data, 'utf-8') # NOQA + if isinstance(data, bytes): + data = data.decode('utf-8') except UnicodeDecodeError: raise CanonicalError('utf-8 stream is expected') - self.data = data + u'\0' + self.data = data + '\0' self.index = 0 self.tokens = [] self.scanned = False @@ -59,51 +55,51 @@ class CanonicalScanner: while True: self.find_token() ch = self.data[self.index] - if ch == u'\0': + if ch == '\0': self.tokens.append(ruamel.yaml.StreamEndToken(None, None)) break - elif ch == u'%': + elif ch == '%': self.tokens.append(self.scan_directive()) - elif ch == u'-' and self.data[self.index : self.index + 3] == u'---': + elif ch == '-' and self.data[self.index : self.index + 3] == '---': self.index += 3 self.tokens.append(ruamel.yaml.DocumentStartToken(None, None)) - elif ch == u'[': + elif ch == '[': self.index += 1 self.tokens.append(ruamel.yaml.FlowSequenceStartToken(None, None)) - elif ch == u'{': + elif ch == '{': self.index += 1 self.tokens.append(ruamel.yaml.FlowMappingStartToken(None, None)) - elif ch == u']': + elif ch == ']': self.index += 1 self.tokens.append(ruamel.yaml.FlowSequenceEndToken(None, None)) - elif ch == u'}': + elif ch == '}': self.index += 1 self.tokens.append(ruamel.yaml.FlowMappingEndToken(None, None)) - elif ch == u'?': + elif ch == '?': self.index += 1 self.tokens.append(ruamel.yaml.KeyToken(None, None)) - elif ch == u':': + elif ch == ':': self.index += 1 self.tokens.append(ruamel.yaml.ValueToken(None, None)) - elif ch == u',': + elif ch == ',': self.index += 1 self.tokens.append(ruamel.yaml.FlowEntryToken(None, None)) - elif ch == u'*' or ch == u'&': + elif ch == '*' or ch == '&': self.tokens.append(self.scan_alias()) - elif ch == u'!': + elif ch == '!': self.tokens.append(self.scan_tag()) - elif ch == u'"': + elif ch == '"': self.tokens.append(self.scan_scalar()) else: raise CanonicalError('invalid token') self.scanned = True - DIRECTIVE = u'%YAML 1.1' + DIRECTIVE = '%YAML 1.1' def scan_directive(self): if ( self.data[self.index : self.index + len(self.DIRECTIVE)] == self.DIRECTIVE - and self.data[self.index + len(self.DIRECTIVE)] in u' \n\0' + and self.data[self.index + len(self.DIRECTIVE)] in ' \n\0' ): self.index += len(self.DIRECTIVE) return ruamel.yaml.DirectiveToken('YAML', (1, 1), None, None) @@ -111,13 +107,13 @@ class CanonicalScanner: raise CanonicalError('invalid directive') def scan_alias(self): - if self.data[self.index] == u'*': + if self.data[self.index] == '*': TokenClass = ruamel.yaml.AliasToken else: TokenClass = ruamel.yaml.AnchorToken self.index += 1 start = self.index - while self.data[self.index] not in u', \n\0': + while self.data[self.index] not in ', \n\0': self.index += 1 value = self.data[start : self.index] return TokenClass(value, None, None) @@ -125,38 +121,38 @@ class CanonicalScanner: def scan_tag(self): self.index += 1 start = self.index - while self.data[self.index] not in u' \n\0': + while self.data[self.index] not in ' \n\0': self.index += 1 value = self.data[start : self.index] if not value: - value = u'!' - elif value[0] == u'!': + value = '!' + elif value[0] == '!': value = 'tag:yaml.org,2002:' + value[1:] - elif value[0] == u'<' and value[-1] == u'>': + elif value[0] == '<' and value[-1] == '>': value = value[1:-1] else: - value = u'!' + value + value = '!' + value return ruamel.yaml.TagToken(value, None, None) QUOTE_CODES = {'x': 2, 'u': 4, 'U': 8} QUOTE_REPLACES = { - u'\\': u'\\', - u'"': u'"', - u' ': u' ', - u'a': u'\x07', - u'b': u'\x08', - u'e': u'\x1B', - u'f': u'\x0C', - u'n': u'\x0A', - u'r': u'\x0D', - u't': u'\x09', - u'v': u'\x0B', - u'N': u'\u0085', - u'L': u'\u2028', - u'P': u'\u2029', - u'_': u'_', - u'0': u'\x00', + '\\': '\\', + '"': '"', + ' ': ' ', + 'a': '\x07', + 'b': '\x08', + 'e': '\x1B', + 'f': '\x0C', + 'n': '\x0A', + 'r': '\x0D', + 't': '\x09', + 'v': '\x0B', + 'N': '\u0085', + 'L': '\u2028', + 'P': '\u2029', + '_': '_', + '0': '\x00', } def scan_scalar(self): @@ -164,32 +160,32 @@ class CanonicalScanner: chunks = [] start = self.index ignore_spaces = False - while self.data[self.index] != u'"': - if self.data[self.index] == u'\\': + while self.data[self.index] != '"': + if self.data[self.index] == '\\': ignore_spaces = False chunks.append(self.data[start : self.index]) self.index += 1 ch = self.data[self.index] self.index += 1 - if ch == u'\n': + if ch == '\n': ignore_spaces = True elif ch in self.QUOTE_CODES: length = self.QUOTE_CODES[ch] code = int(self.data[self.index : self.index + length], 16) - chunks.append(unichr(code)) + chunks.append(chr(code)) self.index += length else: if ch not in self.QUOTE_REPLACES: raise CanonicalError('invalid escape code') chunks.append(self.QUOTE_REPLACES[ch]) start = self.index - elif self.data[self.index] == u'\n': + elif self.data[self.index] == '\n': chunks.append(self.data[start : self.index]) - chunks.append(u' ') + chunks.append(' ') self.index += 1 start = self.index ignore_spaces = True - elif ignore_spaces and self.data[self.index] == u' ': + elif ignore_spaces and self.data[self.index] == ' ': self.index += 1 start = self.index else: @@ -202,12 +198,12 @@ class CanonicalScanner: def find_token(self): found = False while not found: - while self.data[self.index] in u' \t': + while self.data[self.index] in ' \t': self.index += 1 - if self.data[self.index] == u'#': - while self.data[self.index] != u'\n': + if self.data[self.index] == '#': + while self.data[self.index] != '\n': self.index += 1 - if self.data[self.index] == u'\n': + if self.data[self.index] == '\n': self.index += 1 else: found = True @@ -378,7 +374,9 @@ ruamel.yaml.canonical_load = canonical_load def canonical_load_all(stream): - return ruamel.yaml.load_all(stream, Loader=CanonicalLoader) + yaml = ruamel.yaml.YAML(typ='safe', pure=True) + yaml.Loader = CanonicalLoader + return yaml.load_all(stream) ruamel.yaml.canonical_load_all = canonical_load_all |