|
Packit Service |
76cb02 |
# This file is part of Rubber and thus covered by the GPL
|
|
Packit Service |
76cb02 |
# (c) Emmanuel Beffara, 2002--2006
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
This module defines the messages diplay class, and creates the application-wide
|
|
Packit Service |
76cb02 |
msg object.
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
cd7d79 |
from __future__ import print_function
|
|
Packit Service |
cd7d79 |
|
|
Packit Service |
76cb02 |
import os, os.path
|
|
Packit Service |
76cb02 |
import sys
|
|
Packit Service |
76cb02 |
import logging
|
|
Packit Service |
cd7d79 |
from io import open
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
def _(txt): return txt
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
class Message (object):
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
All messages in the program are output using the `msg' object in the
|
|
Packit Service |
76cb02 |
main package. This class defines the interface for this object.
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
def __init__ (self, level=1, write=None):
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
Initialize the object with the specified verbosity level and an
|
|
Packit Service |
76cb02 |
optional writing function. If no such function is specified, no
|
|
Packit Service |
76cb02 |
message will be output until the 'write' field is changed.
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
self.level = level
|
|
Packit Service |
76cb02 |
self.write = self.write_stdout
|
|
Packit Service |
76cb02 |
if write:
|
|
Packit Service |
76cb02 |
self.write = write
|
|
Packit Service |
76cb02 |
self.short = 0
|
|
Packit Service |
76cb02 |
self.path = ""
|
|
Packit Service |
76cb02 |
self.cwd = "./"
|
|
Packit Service |
76cb02 |
self.pos = []
|
|
Packit Service |
76cb02 |
self._log = logging.getLogger("dblatex")
|
|
Packit Service |
76cb02 |
level = self._log.getEffectiveLevel()
|
|
Packit Service |
76cb02 |
if level >= logging.WARNING:
|
|
Packit Service |
cd7d79 |
self.stdout = open(os.devnull, "wb")
|
|
Packit Service |
76cb02 |
else:
|
|
Packit Service |
76cb02 |
self.stdout = None
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
def write_stdout(self, text, level=0):
|
|
Packit Service |
cd7d79 |
print(text)
|
|
Packit Service |
76cb02 |
def write_stderr(self, text, level=0):
|
|
Packit Service |
cd7d79 |
print(text, file=sys.stderr)
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
def push_pos (self, pos):
|
|
Packit Service |
76cb02 |
self.pos.append(pos)
|
|
Packit Service |
76cb02 |
def pop_pos (self):
|
|
Packit Service |
76cb02 |
del self.pos[-1]
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
def __call__ (self, level, text):
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
This is the low level printing function, it receives a line of text
|
|
Packit Service |
76cb02 |
with an associated verbosity level, so that output can be filtered
|
|
Packit Service |
76cb02 |
depending on command-line options.
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
if self.write and level <= self.level:
|
|
Packit Service |
76cb02 |
self.write(text, level=level)
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
def display (self, kind, text, **info):
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
Print an error or warning message. The argument 'kind' indicates the
|
|
Packit Service |
76cb02 |
kind of message, among "error", "warning", "abort", the argument
|
|
Packit Service |
76cb02 |
'text' is the main text of the message, the other arguments provide
|
|
Packit Service |
76cb02 |
additional information, including the location of the error.
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
if kind == "error":
|
|
Packit Service |
76cb02 |
if text[0:13] == "LaTeX Error: ":
|
|
Packit Service |
76cb02 |
text = text[13:]
|
|
Packit Service |
76cb02 |
self._log.error(self.format_pos(info, text))
|
|
Packit Service |
cd7d79 |
if "code" in info and info["code"] and not self.short:
|
|
Packit Service |
76cb02 |
self._log.error(self.format_pos(info,
|
|
Packit Service |
76cb02 |
_("leading text: ") + info["code"]))
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
elif kind == "abort":
|
|
Packit Service |
76cb02 |
if self.short:
|
|
Packit Service |
76cb02 |
msg = _("compilation aborted ") + info["why"]
|
|
Packit Service |
76cb02 |
else:
|
|
Packit Service |
76cb02 |
msg = _("compilation aborted: %s %s") % (text, info["why"])
|
|
Packit Service |
76cb02 |
self._log.error(self.format_pos(info, msg))
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
# elif kind == "warning":
|
|
Packit Service |
76cb02 |
# self._log.warning(self.format_pos(info, text))
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
def error (self, text, **info):
|
|
Packit Service |
76cb02 |
self.display(kind="error", text=text, **info)
|
|
Packit Service |
76cb02 |
def warn (self, what, **where):
|
|
Packit Service |
76cb02 |
self._log.warning(self.format_pos(where, what))
|
|
Packit Service |
76cb02 |
def progress (self, what, **where):
|
|
Packit Service |
76cb02 |
self._log.info(self.format_pos(where, what + "..."))
|
|
Packit Service |
76cb02 |
def info (self, what, **where):
|
|
Packit Service |
76cb02 |
self._log.info(self.format_pos(where, what))
|
|
Packit Service |
76cb02 |
def log (self, what, **where):
|
|
Packit Service |
76cb02 |
self._log.debug(self.format_pos(where, what))
|
|
Packit Service |
76cb02 |
def debug (self, what, **where):
|
|
Packit Service |
76cb02 |
self._log.debug(self.format_pos(where, what))
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
def format_pos (self, where, text):
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
Format the given text into a proper error message, with file and line
|
|
Packit Service |
76cb02 |
information in the standard format. Position information is taken from
|
|
Packit Service |
76cb02 |
the dictionary given as first argument.
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
if len(self.pos) > 0:
|
|
Packit Service |
cd7d79 |
if where is None or "file" not in where:
|
|
Packit Service |
76cb02 |
where = self.pos[-1]
|
|
Packit Service |
76cb02 |
elif where is None or where == {}:
|
|
Packit Service |
76cb02 |
return text
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
cd7d79 |
if "file" in where and where["file"] is not None:
|
|
Packit Service |
76cb02 |
pos = self.simplify(where["file"])
|
|
Packit Service |
cd7d79 |
if "line" in where and where["line"]:
|
|
Packit Service |
76cb02 |
pos = "%s:%d" % (pos, int(where["line"]))
|
|
Packit Service |
cd7d79 |
if "last" in where:
|
|
Packit Service |
76cb02 |
if where["last"] != where["line"]:
|
|
Packit Service |
76cb02 |
pos = "%s-%d" % (pos, int(where["last"]))
|
|
Packit Service |
76cb02 |
pos = pos + ": "
|
|
Packit Service |
76cb02 |
else:
|
|
Packit Service |
76cb02 |
pos = ""
|
|
Packit Service |
cd7d79 |
if "page" in where:
|
|
Packit Service |
76cb02 |
text = "%s (page %d)" % (text, int(where["page"]))
|
|
Packit Service |
cd7d79 |
if "pkg" in where:
|
|
Packit Service |
76cb02 |
text = "[%s] %s" % (where["pkg"], text)
|
|
Packit Service |
76cb02 |
return pos + text
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
def simplify (self, name):
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
Simplify an path name by removing the current directory if the
|
|
Packit Service |
76cb02 |
specified path is in a subdirectory.
|
|
Packit Service |
76cb02 |
"""
|
|
Packit Service |
76cb02 |
path = os.path.normpath(os.path.join(self.path, name))
|
|
Packit Service |
76cb02 |
if path[:len(self.cwd)] == self.cwd:
|
|
Packit Service |
76cb02 |
return path[len(self.cwd):]
|
|
Packit Service |
76cb02 |
return path
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
def display_all (self, generator, writer=None):
|
|
Packit Service |
76cb02 |
if writer:
|
|
Packit Service |
76cb02 |
write = self.write
|
|
Packit Service |
76cb02 |
self.write = writer
|
|
Packit Service |
76cb02 |
something = 0
|
|
Packit Service |
76cb02 |
for msg in generator:
|
|
Packit Service |
76cb02 |
self.display(**msg)
|
|
Packit Service |
76cb02 |
something = 1
|
|
Packit Service |
76cb02 |
if writer:
|
|
Packit Service |
76cb02 |
self.write = write
|
|
Packit Service |
76cb02 |
return something
|
|
Packit Service |
76cb02 |
|
|
Packit Service |
76cb02 |
msg = Message()
|
|
Packit Service |
76cb02 |
|