Page MenuHomec4science

verbosity_depth.py
No OneTemporary

File Metadata

Created
Sat, Apr 27, 22:47

verbosity_depth.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/>.
#
from copy import deepcopy as copy
from datetime import datetime
from rrompy.utilities.exception_manager import RROMPyException
__all__ = ["verbosityDepth", "verbosityManager"]
def getTimestamp() -> str:
time = datetime.now().strftime("%H:%M:%S.%f")
return "\x1b[42m{}\x1b[0m".format(time)
def updateVerbosityCheckpoint(vctype:int) -> str:
global RROMPy_verbosity_checkpoint, RROMPy_verbosity_buffer
if "RROMPy_verbosity_checkpoint" not in globals():
RROMPy_verbosity_checkpoint = 0
RROMPy_verbosity_checkpoint += vctype
if "RROMPy_verbosity_buffer" not in globals():
RROMPy_verbosity_buffer = ""
if RROMPy_verbosity_checkpoint <= 0:
buffer = copy(RROMPy_verbosity_buffer)
del RROMPy_verbosity_buffer
return buffer
return None
def verbosityDepth(vdtype:str, message:str, end : str = "\n",
timestamp : bool = True):
global RROMPy_verbosity_depth, RROMPy_verbosity_checkpoint, \
RROMPy_verbosity_buffer
assert isinstance(vdtype, str)
vdtype = vdtype.upper()
if vdtype not in ["INIT", "MAIN", "DEL"]:
raise RROMPyException("Verbosity depth type not recognized.")
if "RROMPy_verbosity_checkpoint" not in globals():
RROMPy_verbosity_checkpoint = 0
out = "{} ".format(getTimestamp()) if timestamp else ""
if vdtype == "INIT":
if "RROMPy_verbosity_depth" not in globals():
RROMPy_verbosity_depth = 0
RROMPy_verbosity_depth += 1
out += "│" * (RROMPy_verbosity_depth - 1)
out += "┌"
else:
assert "RROMPy_verbosity_depth" in globals()
if vdtype == "MAIN":
out += "│" * (RROMPy_verbosity_depth - 1)
out += "├"
elif vdtype == "DEL":
RROMPy_verbosity_depth -= 1
out += "│" * RROMPy_verbosity_depth
out += "└"
if RROMPy_verbosity_depth <= 0: del RROMPy_verbosity_depth
from rrompy.utilities.parallel import poolRank, poolSize, masterCore
if message != "" and masterCore():
if RROMPy_verbosity_checkpoint and poolSize() > 1:
poolrk = "{{\x1b[34m{}\x1b[0m}}".format(poolRank())
else:
poolrk = ""
msg = "{}{}{}{}".format(out, poolrk, message, end)
if RROMPy_verbosity_checkpoint:
assert "RROMPy_verbosity_buffer" in globals()
RROMPy_verbosity_buffer += msg
else:
print(msg, end = "", flush = True)
return
def verbosityManager(object, vdtype:str, message:str, vlvl : int = 0,
end : str = "\n"):
if object.verbosity >= vlvl:
return verbosityDepth(vdtype, message, end, object.timestamp)

Event Timeline