2 __all__
= ['Mark', 'YAMLError', 'MarkedYAMLError']
6 def __init__(self
, name
, index
, line
, column
, buffer, pointer
):
12 self
.pointer
= pointer
14 def get_snippet(self
, indent
=4, max_length
=75):
15 if self
.buffer is None:
19 while start
> 0 and self
.buffer[start
-1] not in u
'\0\r\n\x85\u2028\u2029':
21 if self
.pointer
-start
> max_length
/2-1:
27 while end
< len(self
.buffer) and self
.buffer[end
] not in u
'\0\r\n\x85\u2028\u2029':
29 if end
-self
.pointer
> max_length
/2-1:
33 snippet
= self
.buffer[start
:end
].encode('utf-8')
34 return ' '*indent
+ head
+ snippet
+ tail
+ '\n' \
35 + ' '*(indent
+self
.pointer
-start
+len(head
)) + '^'
38 snippet
= self
.get_snippet()
39 where
= " in \"%s\", line %d, column %d" \
40 % (self
.name
, self
.line
+1, self
.column
+1)
41 if snippet
is not None:
42 where
+= ":\n"+snippet
45 class YAMLError(Exception):
48 class MarkedYAMLError(YAMLError
):
50 def __init__(self
, context
=None, context_mark
=None,
51 problem
=None, problem_mark
=None):
52 self
.context
= context
53 self
.context_mark
= context_mark
54 self
.problem
= problem
55 self
.problem_mark
= problem_mark
59 #for (place, mark) in [(self.context, self.context_mark),
60 # (self.problem, self.problem_mark)]:
61 # if place is not None:
63 # if mark is not None:
64 # lines.append(str(mark))
65 if self
.context
is not None:
66 lines
.append(self
.context
)
67 if self
.context_mark
is not None \
68 and (self
.problem
is None or self
.problem_mark
is None
69 or self
.context_mark
.name
!= self
.problem_mark
.name
70 or self
.context_mark
.line
!= self
.problem_mark
.line
71 or self
.context_mark
.column
!= self
.problem_mark
.column
):
72 lines
.append(str(self
.context_mark
))
73 if self
.problem
is not None:
74 lines
.append(self
.problem
)
75 if self
.problem_mark
is not None:
76 lines
.append(str(self
.problem_mark
))
77 return '\n'.join(lines
)