Page MenuHomec4science

warning_manager.py
No OneTemporary

File Metadata

Created
Sun, May 5, 14:41

warning_manager.py

# Copyright (C) 2018 by the RROMPy authors
#
# This file is part of RROMPy.
#
# RROMPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RROMPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RROMPy. If not, see <http://www.gnu.org/licenses/>.
#
import traceback as tb
from datetime import datetime
__all__ = ['RROMPyWarning']
def getTimestamp() -> str:
global RROMPy_verbosity_no_color
time = datetime.now().strftime("%H:%M:%S.%f")
if "RROMPy_verbosity_no_color" in globals(): return time
return "\x1b[42m{}\x1b[0m".format(time)
def RROMPyWarning(msg : str = "", stacklevel : int = 0):
global RROMPy_verbosity_no_color
if "RROMPy_verbosity_no_color" in globals():
copen, copen1, cclose = "", "", ""
else:
copen, copen1, cclose = "\x1b[3m", "\x1b[31m", "\x1b[0m"
frameSummary = tb.extract_stack()[- 3 - stacklevel]
timestamp = getTimestamp()
if frameSummary.name == "<module>": name = ""
else: name = ", within {}".format(frameSummary.name)
print("{}{} Warning at {}:{}{}:{}".format(timestamp, copen,
frameSummary.filename,
frameSummary.lineno,
name, cclose))
print("> {}{}{}".format(copen1, frameSummary.line, cclose))
if len(msg) > 0:
print("{} {}{}\n".format(copen, msg, cclose))

Event Timeline