mirror of
https://github.com/powerline/powerline.git
synced 2025-07-29 00:34:49 +02:00
Some fixes for flake8
This commit is contained in:
parent
3fd1215091
commit
5da848fa4a
@ -108,7 +108,7 @@ class Spec(object):
|
||||
else:
|
||||
proceed, echo, fhadproblem = item_func(item, data, context, echoerr)
|
||||
if echo and fhadproblem:
|
||||
echoerr(context=self.cmsg.format(key=context_key(context)+'/list item '+unicode(i)),
|
||||
echoerr(context=self.cmsg.format(key=context_key(context) + '/list item ' + unicode(i)),
|
||||
context_mark=value.mark,
|
||||
problem=msg_func(item),
|
||||
problem_mark=item.mark)
|
||||
@ -141,7 +141,7 @@ class Spec(object):
|
||||
def check_tuple(self, value, context_mark, data, context, echoerr, start, end):
|
||||
hadproblem = False
|
||||
for (i, item, spec) in zip(itertools.count(), value, self.specs[start:end]):
|
||||
proceed, ihadproblem = spec.match(item, value.mark, data, context + (('tuple item '+unicode(i), item),), echoerr)
|
||||
proceed, ihadproblem = spec.match(item, value.mark, data, context + (('tuple item ' + unicode(i), item),), echoerr)
|
||||
if ihadproblem:
|
||||
hadproblem = True
|
||||
if not proceed:
|
||||
@ -424,8 +424,8 @@ main_spec = (Spec(
|
||||
.context_message('Error while loading extensions configuration (key {key})'),
|
||||
).context_message('Error while loading main configuration'))
|
||||
|
||||
term_color_spec=Spec().unsigned().cmp('le', 255).copy
|
||||
true_color_spec=Spec().re('^[0-9a-fA-F]{6}$',
|
||||
term_color_spec = Spec().unsigned().cmp('le', 255).copy
|
||||
true_color_spec = Spec().re('^[0-9a-fA-F]{6}$',
|
||||
lambda value: '"{0}" is not a six-digit hexadecimal unsigned integer written as a string'.format(value)).copy
|
||||
colors_spec = (Spec(
|
||||
colors=Spec().unknown_spec(
|
||||
@ -568,7 +568,7 @@ def check_full_segment_data(segment, data, context, echoerr):
|
||||
|
||||
names = [segment['name']]
|
||||
if segment.get('type', 'function') == 'function':
|
||||
module = segment.get('module', context[0][1].get('default_module', 'powerline.segments.'+ext))
|
||||
module = segment.get('module', context[0][1].get('default_module', 'powerline.segments.' + ext))
|
||||
names.insert(0, unicode(module) + '.' + unicode(names[0]))
|
||||
|
||||
segment_copy = segment.copy()
|
||||
@ -592,7 +592,7 @@ def check_full_segment_data(segment, data, context, echoerr):
|
||||
def check_segment_name(name, data, context, echoerr):
|
||||
ext = data['ext']
|
||||
if context[-2][1].get('type', 'function') == 'function':
|
||||
module = context[-2][1].get('module', context[0][1].get('default_module', 'powerline.segments.'+ext))
|
||||
module = context[-2][1].get('module', context[0][1].get('default_module', 'powerline.segments.' + ext))
|
||||
with WithPath(data['import_paths']):
|
||||
try:
|
||||
func = getattr(__import__(unicode(module), fromlist=[unicode(name)]), unicode(name))
|
||||
@ -616,9 +616,9 @@ def check_segment_name(name, data, context, echoerr):
|
||||
D_H_G_USED_STR = 'Divider highlight group used: '
|
||||
for line in func.__doc__.split('\n'):
|
||||
if H_G_USED_STR in line:
|
||||
hl_groups.append(line[line.index(H_G_USED_STR)+len(H_G_USED_STR):])
|
||||
hl_groups.append(line[line.index(H_G_USED_STR) + len(H_G_USED_STR):])
|
||||
elif D_H_G_USED_STR in line:
|
||||
divider_hl_group = line[line.index(D_H_G_USED_STR)+len(D_H_G_USED_STR)+2:-3]
|
||||
divider_hl_group = line[line.index(D_H_G_USED_STR) + len(D_H_G_USED_STR) + 2:-3]
|
||||
|
||||
hadproblem = False
|
||||
|
||||
@ -756,7 +756,7 @@ def check_segment_data_key(key, data, context, echoerr):
|
||||
if 'name' in segment:
|
||||
if key == segment['name']:
|
||||
found = True
|
||||
module = segment.get('module', theme.get('default_module', 'powerline.segments.'+ext))
|
||||
module = segment.get('module', theme.get('default_module', 'powerline.segments.' + ext))
|
||||
if key == unicode(module) + '.' + unicode(segment['name']):
|
||||
found = True
|
||||
if found:
|
||||
@ -861,6 +861,7 @@ def check(path=None):
|
||||
))
|
||||
|
||||
lhadproblem = [False]
|
||||
|
||||
def load_config(stream):
|
||||
r, hadproblem = load(stream)
|
||||
if hadproblem:
|
||||
|
@ -1,14 +1,9 @@
|
||||
|
||||
from .error import *
|
||||
|
||||
from .tokens import *
|
||||
from .events import *
|
||||
from .nodes import *
|
||||
|
||||
from .loader import *
|
||||
|
||||
__version__ = '3.10'
|
||||
|
||||
|
||||
from .loader import Loader
|
||||
|
||||
|
||||
def load(stream, Loader=Loader):
|
||||
"""
|
||||
Parse the first YAML document in a stream
|
||||
@ -20,4 +15,3 @@ def load(stream, Loader=Loader):
|
||||
return r, loader.haserrors
|
||||
finally:
|
||||
loader.dispose()
|
||||
|
||||
|
@ -1,15 +1,15 @@
|
||||
|
||||
__all__ = ['Composer', 'ComposerError']
|
||||
|
||||
from .error import MarkedYAMLError
|
||||
from .events import *
|
||||
from .nodes import *
|
||||
from .events import * # NOQA
|
||||
from .nodes import * # NOQA
|
||||
|
||||
|
||||
class ComposerError(MarkedYAMLError):
|
||||
pass
|
||||
|
||||
class Composer:
|
||||
|
||||
class Composer:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@ -60,7 +60,6 @@ class Composer:
|
||||
return node
|
||||
|
||||
def compose_node(self, parent, index):
|
||||
event = self.peek_event()
|
||||
self.descend_resolver(parent, index)
|
||||
if self.check_event(ScalarEvent):
|
||||
node = self.compose_scalar_node()
|
||||
@ -116,4 +115,3 @@ class Composer:
|
||||
end_event = self.get_event()
|
||||
node.end_mark = end_event.end_mark
|
||||
return node
|
||||
|
||||
|
@ -1,11 +1,11 @@
|
||||
|
||||
__all__ = ['BaseConstructor', 'Constructor', 'ConstructorError']
|
||||
|
||||
from .error import *
|
||||
from .nodes import *
|
||||
from .markedvalue import *
|
||||
from .error import MarkedYAMLError
|
||||
from .nodes import * # NOQA
|
||||
from .markedvalue import gen_marked_value
|
||||
|
||||
import collections, datetime, base64, binascii, re, sys, types
|
||||
import collections
|
||||
import types
|
||||
|
||||
from functools import wraps
|
||||
|
||||
@ -13,7 +13,8 @@ from functools import wraps
|
||||
try:
|
||||
from __builtin__ import unicode
|
||||
except ImportError:
|
||||
unicode = str
|
||||
unicode = str # NOQA
|
||||
|
||||
|
||||
def marked(func):
|
||||
@wraps(func)
|
||||
@ -21,13 +22,13 @@ def marked(func):
|
||||
return gen_marked_value(func(self, node, *args, **kwargs), node.start_mark)
|
||||
return f
|
||||
|
||||
|
||||
class ConstructorError(MarkedYAMLError):
|
||||
pass
|
||||
|
||||
class BaseConstructor:
|
||||
|
||||
class BaseConstructor:
|
||||
yaml_constructors = {}
|
||||
yaml_multi_constructors = {}
|
||||
|
||||
def __init__(self):
|
||||
self.constructed_objects = {}
|
||||
@ -73,23 +74,7 @@ class BaseConstructor:
|
||||
if node.tag in self.yaml_constructors:
|
||||
constructor = self.yaml_constructors[node.tag]
|
||||
else:
|
||||
for tag_prefix in self.yaml_multi_constructors:
|
||||
if node.tag.startswith(tag_prefix):
|
||||
tag_suffix = node.tag[len(tag_prefix):]
|
||||
constructor = self.yaml_multi_constructors[tag_prefix]
|
||||
break
|
||||
else:
|
||||
if None in self.yaml_multi_constructors:
|
||||
tag_suffix = node.tag
|
||||
constructor = self.yaml_multi_constructors[None]
|
||||
elif None in self.yaml_constructors:
|
||||
constructor = self.yaml_constructors[None]
|
||||
elif isinstance(node, ScalarNode):
|
||||
constructor = self.__class__.construct_scalar
|
||||
elif isinstance(node, SequenceNode):
|
||||
constructor = self.__class__.construct_sequence
|
||||
elif isinstance(node, MappingNode):
|
||||
constructor = self.__class__.construct_mapping
|
||||
raise ConstructorError(None, None, 'no constructor for tag %s' % node.tag)
|
||||
if tag_suffix is None:
|
||||
data = constructor(self, node)
|
||||
else:
|
||||
@ -154,8 +139,8 @@ class BaseConstructor:
|
||||
cls.yaml_constructors = cls.yaml_constructors.copy()
|
||||
cls.yaml_constructors[tag] = constructor
|
||||
|
||||
class Constructor(BaseConstructor):
|
||||
|
||||
class Constructor(BaseConstructor):
|
||||
def construct_scalar(self, node):
|
||||
if isinstance(node, MappingNode):
|
||||
for key_node, value_node in node.value:
|
||||
@ -208,24 +193,14 @@ class Constructor(BaseConstructor):
|
||||
self.construct_scalar(node)
|
||||
return None
|
||||
|
||||
bool_values = {
|
||||
'yes': True,
|
||||
'no': False,
|
||||
'true': True,
|
||||
'false': False,
|
||||
'on': True,
|
||||
'off': False,
|
||||
}
|
||||
|
||||
@marked
|
||||
def construct_yaml_bool(self, node):
|
||||
value = self.construct_scalar(node)
|
||||
return self.bool_values[value.lower()]
|
||||
value = self.construct_scalar(node).value
|
||||
return bool(value)
|
||||
|
||||
@marked
|
||||
def construct_yaml_int(self, node):
|
||||
value = self.construct_scalar(node)
|
||||
value = value.replace('_', '')
|
||||
value = self.construct_scalar(node).value
|
||||
sign = +1
|
||||
if value[0] == '-':
|
||||
sign = -1
|
||||
@ -233,44 +208,19 @@ class Constructor(BaseConstructor):
|
||||
value = value[1:]
|
||||
if value == '0':
|
||||
return 0
|
||||
elif value.startswith('0b'):
|
||||
return sign*int(value[2:], 2)
|
||||
elif value.startswith('0x'):
|
||||
return sign*int(value[2:], 16)
|
||||
elif value[0] == '0':
|
||||
return sign*int(value, 8)
|
||||
elif ':' in value:
|
||||
digits = [int(part) for part in value.split(':')]
|
||||
digits.reverse()
|
||||
base = 1
|
||||
value = 0
|
||||
for digit in digits:
|
||||
value += digit*base
|
||||
base *= 60
|
||||
return sign*value
|
||||
else:
|
||||
return sign*int(value)
|
||||
return sign * int(value)
|
||||
|
||||
@marked
|
||||
def construct_yaml_float(self, node):
|
||||
value = self.construct_scalar(node)
|
||||
value = value.replace('_', '').lower()
|
||||
value = self.construct_scalar(node).value
|
||||
sign = +1
|
||||
if value[0] == '-':
|
||||
sign = -1
|
||||
if value[0] in '+-':
|
||||
value = value[1:]
|
||||
elif ':' in value:
|
||||
digits = [float(part) for part in value.split(':')]
|
||||
digits.reverse()
|
||||
base = 1
|
||||
value = 0.0
|
||||
for digit in digits:
|
||||
value += digit*base
|
||||
base *= 60
|
||||
return sign*value
|
||||
else:
|
||||
return sign*float(value)
|
||||
return sign * float(value)
|
||||
|
||||
def construct_yaml_str(self, node):
|
||||
return self.construct_scalar(node)
|
||||
@ -291,6 +241,7 @@ class Constructor(BaseConstructor):
|
||||
"could not determine a constructor for the tag %r" % node.tag,
|
||||
node.start_mark)
|
||||
|
||||
|
||||
Constructor.add_constructor(
|
||||
'tag:yaml.org,2002:null',
|
||||
Constructor.construct_yaml_null)
|
||||
@ -321,4 +272,3 @@ Constructor.add_constructor(
|
||||
|
||||
Constructor.add_constructor(None,
|
||||
Constructor.construct_undefined)
|
||||
|
||||
|
@ -1,4 +1,3 @@
|
||||
|
||||
__all__ = ['Mark', 'YAMLError', 'MarkedYAMLError', 'echoerr']
|
||||
|
||||
|
||||
@ -8,8 +7,8 @@ import sys
|
||||
def strtrans(s):
|
||||
return s.replace('\t', '>---')
|
||||
|
||||
class Mark:
|
||||
|
||||
class Mark:
|
||||
def __init__(self, name, index, line, column, buffer, pointer):
|
||||
self.name = name
|
||||
self.index = index
|
||||
@ -23,9 +22,9 @@ class Mark:
|
||||
return None
|
||||
head = ''
|
||||
start = self.pointer
|
||||
while start > 0 and self.buffer[start-1] not in '\0\n':
|
||||
while start > 0 and self.buffer[start - 1] not in '\0\n':
|
||||
start -= 1
|
||||
if self.pointer-start > max_length/2-1:
|
||||
if self.pointer - start > max_length / 2 - 1:
|
||||
head = ' ... '
|
||||
start += 5
|
||||
break
|
||||
@ -33,11 +32,11 @@ class Mark:
|
||||
end = self.pointer
|
||||
while end < len(self.buffer) and self.buffer[end] not in '\0\n':
|
||||
end += 1
|
||||
if end-self.pointer > max_length/2-1:
|
||||
if end - self.pointer > max_length / 2 - 1:
|
||||
tail = ' ... '
|
||||
end -= 5
|
||||
break
|
||||
snippet = [self.buffer[start:self.pointer], self.buffer[self.pointer], self.buffer[self.pointer+1:end]]
|
||||
snippet = [self.buffer[start:self.pointer], self.buffer[self.pointer], self.buffer[self.pointer + 1:end]]
|
||||
snippet = [strtrans(s) for s in snippet]
|
||||
return ' ' * indent + head + ''.join(snippet) + tail + '\n' \
|
||||
+ ' ' * (indent + len(head) + len(snippet[0])) + '^'
|
||||
@ -45,7 +44,7 @@ class Mark:
|
||||
def __str__(self):
|
||||
snippet = self.get_snippet()
|
||||
where = " in \"%s\", line %d, column %d" \
|
||||
% (self.name, self.line+1, self.column+1)
|
||||
% (self.name, self.line + 1, self.column + 1)
|
||||
if snippet is not None:
|
||||
where += ":\n" + snippet
|
||||
if type(where) is str:
|
||||
@ -53,6 +52,7 @@ class Mark:
|
||||
else:
|
||||
return where.encode('utf-8')
|
||||
|
||||
|
||||
class YAMLError(Exception):
|
||||
pass
|
||||
|
||||
@ -61,6 +61,7 @@ def echoerr(*args, **kwargs):
|
||||
sys.stderr.write('\n')
|
||||
sys.stderr.write(format_error(*args, **kwargs) + '\n')
|
||||
|
||||
|
||||
def format_error(context=None, context_mark=None, problem=None, problem_mark=None, note=None):
|
||||
lines = []
|
||||
if context is not None:
|
||||
@ -79,8 +80,8 @@ def format_error(context=None, context_mark=None, problem=None, problem_mark=Non
|
||||
lines.append(note)
|
||||
return '\n'.join(lines)
|
||||
|
||||
class MarkedYAMLError(YAMLError):
|
||||
|
||||
class MarkedYAMLError(YAMLError):
|
||||
def __init__(self, context=None, context_mark=None,
|
||||
problem=None, problem_mark=None, note=None):
|
||||
YAMLError.__init__(self, format_error(context, context_mark, problem,
|
||||
|
@ -1,10 +1,11 @@
|
||||
|
||||
# Abstract classes.
|
||||
|
||||
|
||||
class Event(object):
|
||||
def __init__(self, start_mark=None, end_mark=None):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
|
||||
def __repr__(self):
|
||||
attributes = [key for key in ['implicit', 'value']
|
||||
if hasattr(self, key)]
|
||||
@ -12,11 +13,13 @@ class Event(object):
|
||||
for key in attributes])
|
||||
return '%s(%s)' % (self.__class__.__name__, arguments)
|
||||
|
||||
|
||||
class NodeEvent(Event):
|
||||
def __init__(self, start_mark=None, end_mark=None):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
|
||||
|
||||
class CollectionStartEvent(NodeEvent):
|
||||
def __init__(self, implicit, start_mark=None, end_mark=None,
|
||||
flow_style=None):
|
||||
@ -26,20 +29,25 @@ class CollectionStartEvent(NodeEvent):
|
||||
self.end_mark = end_mark
|
||||
self.flow_style = flow_style
|
||||
|
||||
|
||||
class CollectionEndEvent(Event):
|
||||
pass
|
||||
|
||||
|
||||
# Implementations.
|
||||
|
||||
|
||||
class StreamStartEvent(Event):
|
||||
def __init__(self, start_mark=None, end_mark=None, encoding=None):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.encoding = encoding
|
||||
|
||||
|
||||
class StreamEndEvent(Event):
|
||||
pass
|
||||
|
||||
|
||||
class DocumentStartEvent(Event):
|
||||
def __init__(self, start_mark=None, end_mark=None,
|
||||
explicit=None, version=None, tags=None):
|
||||
@ -49,6 +57,7 @@ class DocumentStartEvent(Event):
|
||||
self.version = version
|
||||
self.tags = tags
|
||||
|
||||
|
||||
class DocumentEndEvent(Event):
|
||||
def __init__(self, start_mark=None, end_mark=None,
|
||||
explicit=None):
|
||||
@ -56,9 +65,11 @@ class DocumentEndEvent(Event):
|
||||
self.end_mark = end_mark
|
||||
self.explicit = explicit
|
||||
|
||||
|
||||
class AliasEvent(NodeEvent):
|
||||
pass
|
||||
|
||||
|
||||
class ScalarEvent(NodeEvent):
|
||||
def __init__(self, implicit, value,
|
||||
start_mark=None, end_mark=None, style=None):
|
||||
@ -69,15 +80,18 @@ class ScalarEvent(NodeEvent):
|
||||
self.end_mark = end_mark
|
||||
self.style = style
|
||||
|
||||
|
||||
class SequenceStartEvent(CollectionStartEvent):
|
||||
pass
|
||||
|
||||
|
||||
class SequenceEndEvent(CollectionEndEvent):
|
||||
pass
|
||||
|
||||
|
||||
class MappingStartEvent(CollectionStartEvent):
|
||||
pass
|
||||
|
||||
|
||||
class MappingEndEvent(CollectionEndEvent):
|
||||
pass
|
||||
|
||||
|
@ -1,16 +1,15 @@
|
||||
|
||||
__all__ = ['Loader']
|
||||
|
||||
from .reader import *
|
||||
from .scanner import *
|
||||
from .parser import *
|
||||
from .composer import *
|
||||
from .constructor import *
|
||||
from .resolver import *
|
||||
from .reader import Reader
|
||||
from .scanner import Scanner
|
||||
from .parser import Parser
|
||||
from .composer import Composer
|
||||
from .constructor import Constructor
|
||||
from .resolver import Resolver
|
||||
from .error import echoerr
|
||||
|
||||
class Loader(Reader, Scanner, Parser, Composer, Constructor, Resolver):
|
||||
|
||||
class Loader(Reader, Scanner, Parser, Composer, Constructor, Resolver):
|
||||
def __init__(self, stream):
|
||||
Reader.__init__(self, stream)
|
||||
Scanner.__init__(self)
|
||||
@ -23,4 +22,3 @@ class Loader(Reader, Scanner, Parser, Composer, Constructor, Resolver):
|
||||
def echoerr(self, *args, **kwargs):
|
||||
echoerr(*args, **kwargs)
|
||||
self.haserrors = True
|
||||
|
||||
|
@ -1,12 +1,15 @@
|
||||
__all__ = ['gen_marked_value', 'MarkedValue']
|
||||
|
||||
|
||||
class MarkedValue:
|
||||
def __init__(self, value, mark):
|
||||
self.mark = mark
|
||||
self.value = value
|
||||
|
||||
|
||||
classcache = {}
|
||||
|
||||
|
||||
def gen_marked_value(value, mark):
|
||||
if value.__class__ in classcache:
|
||||
Marked = classcache[value.__class__]
|
||||
|
@ -1,10 +1,10 @@
|
||||
|
||||
class Node(object):
|
||||
def __init__(self, tag, value, start_mark, end_mark):
|
||||
self.tag = tag
|
||||
self.value = value
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
|
||||
def __repr__(self):
|
||||
value = self.value
|
||||
#if isinstance(value, list):
|
||||
@ -22,8 +22,10 @@ class Node(object):
|
||||
value = repr(value)
|
||||
return '%s(tag=%r, value=%s)' % (self.__class__.__name__, self.tag, value)
|
||||
|
||||
|
||||
class ScalarNode(Node):
|
||||
id = 'scalar'
|
||||
|
||||
def __init__(self, tag, value,
|
||||
start_mark=None, end_mark=None, style=None):
|
||||
self.tag = tag
|
||||
@ -32,6 +34,7 @@ class ScalarNode(Node):
|
||||
self.end_mark = end_mark
|
||||
self.style = style
|
||||
|
||||
|
||||
class CollectionNode(Node):
|
||||
def __init__(self, tag, value,
|
||||
start_mark=None, end_mark=None, flow_style=None):
|
||||
@ -41,9 +44,10 @@ class CollectionNode(Node):
|
||||
self.end_mark = end_mark
|
||||
self.flow_style = flow_style
|
||||
|
||||
|
||||
class SequenceNode(CollectionNode):
|
||||
id = 'sequence'
|
||||
|
||||
|
||||
class MappingNode(CollectionNode):
|
||||
id = 'mapping'
|
||||
|
||||
|
@ -1,26 +1,18 @@
|
||||
__all__ = ['Parser', 'ParserError']
|
||||
|
||||
from .error import MarkedYAMLError
|
||||
from .tokens import *
|
||||
from .events import *
|
||||
from .scanner import *
|
||||
from .tokens import * # NOQA
|
||||
from .events import * # NOQA
|
||||
|
||||
|
||||
class ParserError(MarkedYAMLError):
|
||||
pass
|
||||
|
||||
|
||||
class Parser:
|
||||
# Since writing a recursive-descendant parser is a straightforward task, we
|
||||
# do not give many comments here.
|
||||
|
||||
DEFAULT_TAGS = {
|
||||
'!': '!',
|
||||
'!!': 'tag:yaml.org,2002:',
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
self.current_event = None
|
||||
self.yaml_version = None
|
||||
self.tag_handles = {}
|
||||
self.states = []
|
||||
self.marks = []
|
||||
self.state = self.parse_stream_start
|
||||
@ -64,7 +56,6 @@ class Parser:
|
||||
# explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
|
||||
|
||||
def parse_stream_start(self):
|
||||
|
||||
# Parse the stream start.
|
||||
token = self.get_token()
|
||||
event = StreamStartEvent(token.start_mark, token.end_mark,
|
||||
@ -76,10 +67,8 @@ class Parser:
|
||||
return event
|
||||
|
||||
def parse_implicit_document_start(self):
|
||||
|
||||
# Parse an implicit document.
|
||||
if not self.check_token(StreamEndToken):
|
||||
self.tag_handles = self.DEFAULT_TAGS
|
||||
token = self.peek_token()
|
||||
start_mark = end_mark = token.start_mark
|
||||
event = DocumentStartEvent(start_mark, end_mark, explicit=False)
|
||||
@ -94,15 +83,12 @@ class Parser:
|
||||
return self.parse_document_start()
|
||||
|
||||
def parse_document_start(self):
|
||||
|
||||
# Parse an explicit document.
|
||||
if not self.check_token(StreamEndToken):
|
||||
token = self.peek_token()
|
||||
start_mark = token.start_mark
|
||||
self.echoerr(None, None,
|
||||
"expected '<stream end>', but found %r"
|
||||
% self.peek_token().id,
|
||||
self.peek_token().start_mark)
|
||||
"expected '<stream end>', but found %r" % token.id,
|
||||
token.start_mark)
|
||||
return StreamEndEvent(token.start_mark, token.end_mark)
|
||||
else:
|
||||
# Parse the end of the stream.
|
||||
@ -114,7 +100,6 @@ class Parser:
|
||||
return event
|
||||
|
||||
def parse_document_end(self):
|
||||
|
||||
# Parse the document end.
|
||||
token = self.peek_token()
|
||||
start_mark = end_mark = token.start_mark
|
||||
@ -130,7 +115,7 @@ class Parser:
|
||||
return self.parse_node()
|
||||
|
||||
def parse_node(self, indentless_sequence=False):
|
||||
start_mark = end_mark = tag_mark = None
|
||||
start_mark = end_mark = None
|
||||
if start_mark is None:
|
||||
start_mark = end_mark = self.peek_token().start_mark
|
||||
event = None
|
||||
|
@ -1,35 +1,21 @@
|
||||
# This module contains abstractions for the input stream. You don't have to
|
||||
# looks further, there are no pretty code.
|
||||
#
|
||||
# We define two classes here.
|
||||
#
|
||||
# Mark(source, line, column)
|
||||
# It's just a record and its only use is producing nice error messages.
|
||||
# Parser does not use it for any other purposes.
|
||||
#
|
||||
# Reader(source, data)
|
||||
# Reader determines the encoding of `data` and converts it to unicode.
|
||||
# Reader provides the following methods and attributes:
|
||||
# reader.peek(length=1) - return the next `length` characters
|
||||
# reader.forward(length=1) - move the current position to `length` characters.
|
||||
# reader.index - the number of the current character.
|
||||
# reader.line, stream.column - the line and the column of the current character.
|
||||
|
||||
__all__ = ['Reader', 'ReaderError']
|
||||
|
||||
from .error import YAMLError, Mark
|
||||
|
||||
import codecs, re
|
||||
|
||||
import codecs
|
||||
import re
|
||||
|
||||
try:
|
||||
from __builtin__ import unicode, unichr
|
||||
except ImportError:
|
||||
unicode = str
|
||||
unichr = chr
|
||||
unicode = str # NOQA
|
||||
unichr = chr # NOQA
|
||||
|
||||
|
||||
class ReaderError(YAMLError):
|
||||
|
||||
def __init__(self, name, position, character, encoding, reason):
|
||||
self.name = name
|
||||
self.character = character
|
||||
@ -49,6 +35,7 @@ class ReaderError(YAMLError):
|
||||
% (self.character, self.reason,
|
||||
self.name, self.position)
|
||||
|
||||
|
||||
class Reader(object):
|
||||
# Reader:
|
||||
# - determines the data encoding and converts it to a unicode string,
|
||||
@ -59,7 +46,6 @@ class Reader(object):
|
||||
# - a file-like object with its `read` method returning `str`,
|
||||
|
||||
# Yeah, it's ugly and slow.
|
||||
|
||||
def __init__(self, stream):
|
||||
self.name = None
|
||||
self.stream = None
|
||||
@ -87,19 +73,19 @@ class Reader(object):
|
||||
|
||||
def peek(self, index=0):
|
||||
try:
|
||||
return self.buffer[self.pointer+index]
|
||||
return self.buffer[self.pointer + index]
|
||||
except IndexError:
|
||||
self.update(index+1)
|
||||
return self.buffer[self.pointer+index]
|
||||
self.update(index + 1)
|
||||
return self.buffer[self.pointer + index]
|
||||
|
||||
def prefix(self, length=1):
|
||||
if self.pointer+length >= len(self.buffer):
|
||||
if self.pointer + length >= len(self.buffer):
|
||||
self.update(length)
|
||||
return self.buffer[self.pointer:self.pointer+length]
|
||||
return self.buffer[self.pointer:self.pointer + length]
|
||||
|
||||
def forward(self, length=1):
|
||||
if self.pointer+length+1 >= len(self.buffer):
|
||||
self.update(length+1)
|
||||
if self.pointer + length + 1 >= len(self.buffer):
|
||||
self.update(length + 1)
|
||||
while length:
|
||||
ch = self.buffer[self.pointer]
|
||||
self.pointer += 1
|
||||
@ -114,14 +100,14 @@ class Reader(object):
|
||||
return Mark(self.name, self.index, self.line, self.column,
|
||||
self.full_buffer, self.full_pointer)
|
||||
|
||||
NON_PRINTABLE = re.compile('[^\t\n\x20-\x7E' + unichr(0x85) + (unichr(0xA0)+'-'+unichr(0xD7FF)) + (unichr(0xE000)+'-'+unichr(0xFFFD)) + ']')
|
||||
NON_PRINTABLE = re.compile('[^\t\n\x20-\x7E' + unichr(0x85) + (unichr(0xA0) + '-' + unichr(0xD7FF)) + (unichr(0xE000) + '-' + unichr(0xFFFD)) + ']')
|
||||
|
||||
def check_printable(self, data):
|
||||
match = self.NON_PRINTABLE.search(data)
|
||||
if match:
|
||||
character = match.group()
|
||||
position = self.index+(len(self.buffer)-self.pointer)+match.start()
|
||||
raise ReaderError(self.name, position, ord(character),
|
||||
'unicode', "special characters are not allowed")
|
||||
position = self.index + (len(self.buffer) - self.pointer) + match.start()
|
||||
raise ReaderError(self.name, position, ord(character), 'unicode', "special characters are not allowed")
|
||||
|
||||
def update(self, length):
|
||||
if self.raw_buffer is None:
|
||||
@ -136,9 +122,8 @@ class Reader(object):
|
||||
'strict', self.eof)
|
||||
except UnicodeDecodeError as exc:
|
||||
character = self.raw_buffer[exc.start]
|
||||
position = self.stream_pointer-len(self.raw_buffer)+exc.start
|
||||
raise ReaderError(self.name, position, character,
|
||||
exc.encoding, exc.reason)
|
||||
position = self.stream_pointer - len(self.raw_buffer) + exc.start
|
||||
raise ReaderError(self.name, position, character, exc.encoding, exc.reason)
|
||||
self.check_printable(data)
|
||||
self.buffer += data
|
||||
self.full_buffer += data
|
||||
@ -157,4 +142,3 @@ class Reader(object):
|
||||
self.stream_pointer += len(data)
|
||||
if not data:
|
||||
self.eof = True
|
||||
|
||||
|
@ -1,16 +1,16 @@
|
||||
|
||||
__all__ = ['BaseResolver', 'Resolver']
|
||||
|
||||
from .error import *
|
||||
from .nodes import *
|
||||
from .error import MarkedYAMLError
|
||||
from .nodes import * # NOQA
|
||||
|
||||
import re
|
||||
|
||||
|
||||
class ResolverError(MarkedYAMLError):
|
||||
pass
|
||||
|
||||
class BaseResolver:
|
||||
|
||||
class BaseResolver:
|
||||
DEFAULT_SCALAR_TAG = 'tag:yaml.org,2002:str'
|
||||
DEFAULT_SEQUENCE_TAG = 'tag:yaml.org,2002:seq'
|
||||
DEFAULT_MAPPING_TAG = 'tag:yaml.org,2002:map'
|
||||
@ -62,7 +62,7 @@ class BaseResolver:
|
||||
|
||||
def check_resolver_prefix(self, depth, path, kind,
|
||||
current_node, current_index):
|
||||
node_check, index_check = path[depth-1]
|
||||
node_check, index_check = path[depth - 1]
|
||||
if isinstance(node_check, str):
|
||||
if current_node.tag != node_check:
|
||||
return
|
||||
@ -94,16 +94,10 @@ class BaseResolver:
|
||||
if regexp.match(value):
|
||||
return tag
|
||||
else:
|
||||
raise ResolverError('while resolving plain scalar', None,
|
||||
"expected floating-point value, integer, null or boolean, but got %r" % value,
|
||||
self.echoerr('While resolving plain scalar', None,
|
||||
'expected floating-point value, integer, null or boolean, but got %r' % value,
|
||||
mark)
|
||||
implicit = implicit[1]
|
||||
if self.yaml_path_resolvers:
|
||||
exact_paths = self.resolver_exact_paths[-1]
|
||||
if kind in exact_paths:
|
||||
return exact_paths[kind]
|
||||
if None in exact_paths:
|
||||
return exact_paths[None]
|
||||
return self.DEFAULT_SCALAR_TAG
|
||||
if kind is ScalarNode:
|
||||
return self.DEFAULT_SCALAR_TAG
|
||||
elif kind is SequenceNode:
|
||||
@ -111,9 +105,11 @@ class BaseResolver:
|
||||
elif kind is MappingNode:
|
||||
return self.DEFAULT_MAPPING_TAG
|
||||
|
||||
|
||||
class Resolver(BaseResolver):
|
||||
pass
|
||||
|
||||
|
||||
Resolver.add_implicit_resolver(
|
||||
'tag:yaml.org,2002:bool',
|
||||
re.compile(r'''^(?:true|false)$''', re.X),
|
||||
|
@ -1,4 +1,3 @@
|
||||
|
||||
# Scanner produces tokens of the following types:
|
||||
# STREAM-START
|
||||
# STREAM-END
|
||||
@ -11,25 +10,22 @@
|
||||
# FLOW-ENTRY
|
||||
# KEY
|
||||
# VALUE
|
||||
# ALIAS(value)
|
||||
# ANCHOR(value)
|
||||
# TAG(value)
|
||||
# SCALAR(value, plain, style)
|
||||
#
|
||||
# Read comments in the Scanner code for more details.
|
||||
#
|
||||
|
||||
__all__ = ['Scanner', 'ScannerError']
|
||||
|
||||
from .error import MarkedYAMLError
|
||||
from .tokens import *
|
||||
from .tokens import * # NOQA
|
||||
|
||||
|
||||
class ScannerError(MarkedYAMLError):
|
||||
pass
|
||||
|
||||
|
||||
class SimpleKey:
|
||||
# See below simple keys treatment.
|
||||
|
||||
def __init__(self, token_number, index, line, column, mark):
|
||||
self.token_number = token_number
|
||||
self.index = index
|
||||
@ -37,8 +33,8 @@ class SimpleKey:
|
||||
self.column = column
|
||||
self.mark = mark
|
||||
|
||||
class Scanner:
|
||||
|
||||
class Scanner:
|
||||
def __init__(self):
|
||||
"""Initialize the scanner."""
|
||||
# It is assumed that Scanner and Reader will have a common descendant.
|
||||
@ -218,7 +214,7 @@ class Scanner:
|
||||
# position.
|
||||
if self.allow_simple_key:
|
||||
self.remove_possible_simple_key()
|
||||
token_number = self.tokens_taken+len(self.tokens)
|
||||
token_number = self.tokens_taken + len(self.tokens)
|
||||
key = SimpleKey(token_number,
|
||||
self.index, self.line, self.column, self.get_mark())
|
||||
self.possible_simple_keys[self.flow_level] = key
|
||||
@ -226,8 +222,6 @@ class Scanner:
|
||||
def remove_possible_simple_key(self):
|
||||
# Remove the saved possible key position at the current flow level.
|
||||
if self.flow_level in self.possible_simple_keys:
|
||||
key = self.possible_simple_keys[self.flow_level]
|
||||
|
||||
del self.possible_simple_keys[self.flow_level]
|
||||
|
||||
# Fetchers.
|
||||
@ -243,9 +237,7 @@ class Scanner:
|
||||
self.tokens.append(StreamStartToken(mark, mark,
|
||||
encoding=self.encoding))
|
||||
|
||||
|
||||
def fetch_stream_end(self):
|
||||
|
||||
# Reset simple keys.
|
||||
self.remove_possible_simple_key()
|
||||
self.allow_simple_key = False
|
||||
@ -313,7 +305,7 @@ class Scanner:
|
||||
# Add KEY.
|
||||
key = self.possible_simple_keys[self.flow_level]
|
||||
del self.possible_simple_keys[self.flow_level]
|
||||
self.tokens.insert(key.token_number-self.tokens_taken,
|
||||
self.tokens.insert(key.token_number - self.tokens_taken,
|
||||
KeyToken(key.mark, key.mark))
|
||||
|
||||
# There cannot be two simple keys one after another.
|
||||
@ -463,7 +455,6 @@ class Scanner:
|
||||
while True:
|
||||
length = 0
|
||||
while True:
|
||||
ch = self.peek(length)
|
||||
if self.peek(length) not in 'eE.0123456789nul-tr+fas':
|
||||
break
|
||||
length += 1
|
||||
|
@ -1,8 +1,8 @@
|
||||
|
||||
class Token(object):
|
||||
def __init__(self, start_mark, end_mark):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
|
||||
def __repr__(self):
|
||||
attributes = [key for key in self.__dict__
|
||||
if not key.endswith('_mark')]
|
||||
@ -11,47 +11,55 @@ class Token(object):
|
||||
for key in attributes])
|
||||
return '%s(%s)' % (self.__class__.__name__, arguments)
|
||||
|
||||
#class BOMToken(Token):
|
||||
# id = '<byte order mark>'
|
||||
|
||||
class StreamStartToken(Token):
|
||||
id = '<stream start>'
|
||||
|
||||
def __init__(self, start_mark=None, end_mark=None,
|
||||
encoding=None):
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.encoding = encoding
|
||||
|
||||
|
||||
class StreamEndToken(Token):
|
||||
id = '<stream end>'
|
||||
|
||||
|
||||
class FlowSequenceStartToken(Token):
|
||||
id = '['
|
||||
|
||||
|
||||
class FlowMappingStartToken(Token):
|
||||
id = '{'
|
||||
|
||||
|
||||
class FlowSequenceEndToken(Token):
|
||||
id = ']'
|
||||
|
||||
|
||||
class FlowMappingEndToken(Token):
|
||||
id = '}'
|
||||
|
||||
|
||||
class KeyToken(Token):
|
||||
id = '?'
|
||||
|
||||
|
||||
class ValueToken(Token):
|
||||
id = ':'
|
||||
|
||||
|
||||
class FlowEntryToken(Token):
|
||||
id = ','
|
||||
|
||||
|
||||
class ScalarToken(Token):
|
||||
id = '<scalar>'
|
||||
|
||||
def __init__(self, value, plain, start_mark, end_mark, style=None):
|
||||
self.value = value
|
||||
self.plain = plain
|
||||
self.start_mark = start_mark
|
||||
self.end_mark = end_mark
|
||||
self.style = style
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user