Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
error.py
Go to the documentation of this file.
1 # (c) Copyright Rosetta Commons Member Institutions.
2 # (c) This file is part of the Rosetta software suite and is made available under license.
3 # (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
4 # (c) For more information, see http://www.rosettacommons.org. Questions about this can be
5 # (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
6 
7 __all__ = ['Mark', 'YAMLError', 'MarkedYAMLError']
8 
9 class Mark(object):
10 
11  def __init__(self, name, index, line, column, buffer, pointer):
12  self.name = name
13  self.index = index
14  self.line = line
15  self.column = column
16  self.buffer = buffer
17  self.pointer = pointer
18 
19  def get_snippet(self, indent=4, max_length=75):
20  if self.buffer is None:
21  return None
22  head = ''
23  start = self.pointer
24  while start > 0 and self.buffer[start-1] not in u'\0\r\n\x85\u2028\u2029':
25  start -= 1
26  if self.pointer-start > max_length/2-1:
27  head = ' ... '
28  start += 5
29  break
30  tail = ''
31  end = self.pointer
32  while end < len(self.buffer) and self.buffer[end] not in u'\0\r\n\x85\u2028\u2029':
33  end += 1
34  if end-self.pointer > max_length/2-1:
35  tail = ' ... '
36  end -= 5
37  break
38  snippet = self.buffer[start:end].encode('utf-8')
39  return ' '*indent + head + snippet + tail + '\n' \
40  + ' '*(indent+self.pointer-start+len(head)) + '^'
41 
42  def __str__(self):
43  snippet = self.get_snippet()
44  where = " in \"%s\", line %d, column %d" \
45  % (self.name, self.line+1, self.column+1)
46  if snippet is not None:
47  where += ":\n"+snippet
48  return where
49 
51  pass
52 
53 class MarkedYAMLError(YAMLError):
54 
55  def __init__(self, context=None, context_mark=None,
56  problem=None, problem_mark=None, note=None):
57  self.context = context
58  self.context_mark = context_mark
59  self.problem = problem
60  self.problem_mark = problem_mark
61  self.note = note
62 
63  def __str__(self):
64  lines = []
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  if self.note is not None:
78  lines.append(self.note)
79  return '\n'.join(lines)
80 
def __str__
Definition: error.py:42
Fstring::size_type len(Fstring const &s)
Length.
Definition: Fstring.hh:2207
def __init__
Definition: error.py:11
def get_snippet
Definition: error.py:19