1a5f9918aSopenharmony_ci 2a5f9918aSopenharmony_ci__all__ = ['Mark', 'YAMLError', 'MarkedYAMLError'] 3a5f9918aSopenharmony_ci 4a5f9918aSopenharmony_ciclass Mark: 5a5f9918aSopenharmony_ci 6a5f9918aSopenharmony_ci def __init__(self, name, index, line, column, buffer, pointer): 7a5f9918aSopenharmony_ci self.name = name 8a5f9918aSopenharmony_ci self.index = index 9a5f9918aSopenharmony_ci self.line = line 10a5f9918aSopenharmony_ci self.column = column 11a5f9918aSopenharmony_ci self.buffer = buffer 12a5f9918aSopenharmony_ci self.pointer = pointer 13a5f9918aSopenharmony_ci 14a5f9918aSopenharmony_ci def get_snippet(self, indent=4, max_length=75): 15a5f9918aSopenharmony_ci if self.buffer is None: 16a5f9918aSopenharmony_ci return None 17a5f9918aSopenharmony_ci head = '' 18a5f9918aSopenharmony_ci start = self.pointer 19a5f9918aSopenharmony_ci while start > 0 and self.buffer[start-1] not in '\0\r\n\x85\u2028\u2029': 20a5f9918aSopenharmony_ci start -= 1 21a5f9918aSopenharmony_ci if self.pointer-start > max_length/2-1: 22a5f9918aSopenharmony_ci head = ' ... ' 23a5f9918aSopenharmony_ci start += 5 24a5f9918aSopenharmony_ci break 25a5f9918aSopenharmony_ci tail = '' 26a5f9918aSopenharmony_ci end = self.pointer 27a5f9918aSopenharmony_ci while end < len(self.buffer) and self.buffer[end] not in '\0\r\n\x85\u2028\u2029': 28a5f9918aSopenharmony_ci end += 1 29a5f9918aSopenharmony_ci if end-self.pointer > max_length/2-1: 30a5f9918aSopenharmony_ci tail = ' ... ' 31a5f9918aSopenharmony_ci end -= 5 32a5f9918aSopenharmony_ci break 33a5f9918aSopenharmony_ci snippet = self.buffer[start:end] 34a5f9918aSopenharmony_ci return ' '*indent + head + snippet + tail + '\n' \ 35a5f9918aSopenharmony_ci + ' '*(indent+self.pointer-start+len(head)) + '^' 36a5f9918aSopenharmony_ci 37a5f9918aSopenharmony_ci def __str__(self): 38a5f9918aSopenharmony_ci snippet = self.get_snippet() 39a5f9918aSopenharmony_ci where = " in \"%s\", line %d, column %d" \ 40a5f9918aSopenharmony_ci % (self.name, self.line+1, self.column+1) 41a5f9918aSopenharmony_ci if snippet is not None: 42a5f9918aSopenharmony_ci where += ":\n"+snippet 43a5f9918aSopenharmony_ci return where 44a5f9918aSopenharmony_ci 45a5f9918aSopenharmony_ciclass YAMLError(Exception): 46a5f9918aSopenharmony_ci pass 47a5f9918aSopenharmony_ci 48a5f9918aSopenharmony_ciclass MarkedYAMLError(YAMLError): 49a5f9918aSopenharmony_ci 50a5f9918aSopenharmony_ci def __init__(self, context=None, context_mark=None, 51a5f9918aSopenharmony_ci problem=None, problem_mark=None, note=None): 52a5f9918aSopenharmony_ci self.context = context 53a5f9918aSopenharmony_ci self.context_mark = context_mark 54a5f9918aSopenharmony_ci self.problem = problem 55a5f9918aSopenharmony_ci self.problem_mark = problem_mark 56a5f9918aSopenharmony_ci self.note = note 57a5f9918aSopenharmony_ci 58a5f9918aSopenharmony_ci def __str__(self): 59a5f9918aSopenharmony_ci lines = [] 60a5f9918aSopenharmony_ci if self.context is not None: 61a5f9918aSopenharmony_ci lines.append(self.context) 62a5f9918aSopenharmony_ci if self.context_mark is not None \ 63a5f9918aSopenharmony_ci and (self.problem is None or self.problem_mark is None 64a5f9918aSopenharmony_ci or self.context_mark.name != self.problem_mark.name 65a5f9918aSopenharmony_ci or self.context_mark.line != self.problem_mark.line 66a5f9918aSopenharmony_ci or self.context_mark.column != self.problem_mark.column): 67a5f9918aSopenharmony_ci lines.append(str(self.context_mark)) 68a5f9918aSopenharmony_ci if self.problem is not None: 69a5f9918aSopenharmony_ci lines.append(self.problem) 70a5f9918aSopenharmony_ci if self.problem_mark is not None: 71a5f9918aSopenharmony_ci lines.append(str(self.problem_mark)) 72a5f9918aSopenharmony_ci if self.note is not None: 73a5f9918aSopenharmony_ci lines.append(self.note) 74a5f9918aSopenharmony_ci return '\n'.join(lines) 75a5f9918aSopenharmony_ci 76