diff options
author | Matthew Peveler <matt.peveler@gmail.com> | 2022-02-17 10:08:57 -0500 |
---|---|---|
committer | Matthew Peveler <matt.peveler@gmail.com> | 2022-02-17 10:08:57 -0500 |
commit | 84630fa680a18deb534391f01fd1d0231755259e (patch) | |
tree | 228fa3b01f97e2337fc9cbf39d89aa6b5b0abc33 | |
parent | eb304c9113364f9642e140ed9fd814097f645d0b (diff) | |
download | asciidoc-py3-84630fa680a18deb534391f01fd1d0231755259e.tar.gz |
Move Message class to own module
Signed-off-by: Matthew Peveler <matt.peveler@gmail.com>
-rw-r--r-- | asciidoc/asciidoc.py | 71 | ||||
-rw-r--r-- | asciidoc/message.py | 77 |
2 files changed, 81 insertions, 67 deletions
diff --git a/asciidoc/asciidoc.py b/asciidoc/asciidoc.py index 795b22f..8550609 100644 --- a/asciidoc/asciidoc.py +++ b/asciidoc/asciidoc.py @@ -37,6 +37,7 @@ from collections import OrderedDict from .blocks.table import parse_table_span_spec, Cell, Column from .collections import AttrDict, InsensitiveDict from .exceptions import EAsciiDoc +from .message import Message from . import utils CONF_DIR = os.path.join(os.path.dirname(__file__), 'resources') @@ -111,71 +112,6 @@ class Trace(object): message.stderr(msg) -class Message: - """ - Message functions. - """ - PROG = os.path.basename(os.path.splitext(__file__)[0]) - - def __init__(self): - # 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 = '' - - @staticmethod - def stdout(msg): - print(msg) - - def stderr(self, msg=''): - if msg == self.prev_msg: # Suppress repeated messages. - return - self.messages.append(msg) - if APPLICATION_CALLER == '__main__': - sys.stderr.write('%s: %s%s' % (self.PROG, msg, os.linesep)) - self.prev_msg = msg - - def verbose(self, msg, linenos=True): - if 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) - 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 reader.cursor): - if cursor is None: - cursor = 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) - document.has_errors = True - - def unsafe(self, msg): - self.error('unsafe: '+msg) - - def safe(): return document.safe @@ -5802,7 +5738,7 @@ document = Document() # The document being processed. config = Config() # Configuration file reader. reader = Reader() # Input stream line reader. writer = Writer() # Output stream line writer. -message = Message() # Message functions. +message = Message(APPLICATION_CALLER, document, config, reader) # Message functions. paragraphs = Paragraphs() # Paragraph definitions. lists = Lists() # List definitions. blocks = DelimitedBlocks() # DelimitedBlock definitions. @@ -5820,6 +5756,7 @@ messages = message.messages def set_caller(name: str) -> None: global APPLICATION_CALLER APPLICATION_CALLER = name + message.set_caller(APPLICATION_CALLER) def reset_asciidoc(): @@ -5831,7 +5768,7 @@ def reset_asciidoc(): config = Config() reader = Reader() writer = Writer() - message = Message() + message = Message(APPLICATION_CALLER, document, config, reader) paragraphs = Paragraphs() lists = Lists() blocks = DelimitedBlocks() diff --git a/asciidoc/message.py b/asciidoc/message.py new file mode 100644 index 0000000..46d4998 --- /dev/null +++ b/asciidoc/message.py @@ -0,0 +1,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) |