1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
import os
import sys
from .exceptions import EAsciiDoc
class Message:
"""
Message functions.
"""
PROG = 'asciidoc'
def __init__(self, application_caller, document, config, reader):
# Set to True or False to globally override line numbers method
# argument. Has no effect when set to None.
self.linenos = None
self.messages = []
self.prev_msg = ''
self.application_caller = application_caller
self.document = document
self.config = config
self.reader = reader
def set_caller(self, application_caller):
self.application_caller = application_caller
@staticmethod
def stdout(msg):
print(msg)
def stderr(self, msg=''):
if msg == self.prev_msg: # Suppress repeated messages.
return
self.messages.append(msg)
if self.application_caller == '__main__':
sys.stderr.write('%s: %s%s' % (Message.PROG, msg, os.linesep))
self.prev_msg = msg
def verbose(self, msg, linenos=True):
if self.config.verbose:
msg = self.format(msg, linenos=linenos)
self.stderr(msg)
def warning(self, msg, linenos=True, offset=0):
msg = self.format(msg, 'WARNING: ', linenos, offset=offset)
self.document.has_warnings = True
self.stderr(msg)
def deprecated(self, msg, linenos=True):
msg = self.format(msg, 'DEPRECATED: ', linenos)
self.stderr(msg)
def format(self, msg, prefix='', linenos=True, cursor=None, offset=0):
"""Return formatted message string."""
if self.linenos is not False and \
((linenos or self.linenos) and self.reader.cursor):
if cursor is None:
cursor = self.reader.cursor
prefix += '%s: line %d: ' % (os.path.basename(cursor[0]), cursor[1]+offset)
return prefix + msg
def error(self, msg, cursor=None, halt=False):
"""
Report fatal error.
If halt=True raise EAsciiDoc exception.
If halt=False don't exit application, continue in the hope of reporting
all fatal errors finishing with a non-zero exit code.
"""
if halt:
raise EAsciiDoc(self.format(msg, linenos=False, cursor=cursor))
else:
msg = self.format(msg, 'ERROR: ', cursor=cursor)
self.stderr(msg)
self.document.has_errors = True
def unsafe(self, msg):
self.error('unsafe: '+msg)
|