Source code for vdat.command_interpreter.helpers
# Virus Data Analysis Tool: a data reduction GUI for HETDEX/VIRUS data
# Copyright (C) 2015, 2016 "The HETDEX collaboration"
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Helper function and classes.
These functionalities are not essential for the interpreter, but can help the
to setup things.
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import logging
import sys
[docs]def print_first(int_, string_):
"""Print ``string_`` when ``int_`` is zero.
Can be connected to
:class:`vdat.command_interpreter.signals.CICommandString`
"""
if int_ == 0:
print(string_)
[docs]def print_progress(int_tot, int_done, int_skipped, int_fail):
"""Print the percentages of finished, successful and failed jobs,
overwriting the line. Skipped are counted as successful.
Can be connected to
:class:`vdat.command_interpreter.signals.CIProgress`
"""
int_success = int_done - int_fail
line = "Progress: {0:.0%}, success: {1:.0%}, failures: {2:.0%}"
line = line.format(int_done / int_tot,
int_success / int_tot,
int_fail / int_tot)
sys.stdout.write("\r\x1b[K" + line)
sys.stdout.flush()
[docs]def print_done(bool_cidone):
"""Function that prints ``command done`` when ``bool_cidone == False`` and
``execution done`` otherwise
Can be connected to
:class:`vdat.command_interpreter.signals.CICommandDone`
"""
if bool_cidone:
print("execution done")
else:
print("command done")
[docs]def print_global_logger(int_level, string_msg):
"""Function that prints ``levelname: string_msg``. The conversion is done
Can be connected to
:class:`vdat.command_interpreter.signals.CIGlobalLogger`
"""
print(logging.getLevelName(int_level) + ": " + string_msg)
[docs]def print_n_primaries(int_):
"""Function that prints ``Number of primaries: int_``.
Can be connected to
:class:`vdat.command_interpreter.signals.CINPrimaries`
"""
print("Number of primaries: {}".format(int_))
[docs]def log_command_logger(string_primary, string_command, string_info,
string_warning, string_error, string_critical,
dict_config):
"""Function that log the command, and the
strings_{info,warning,error,critical} to a logger called with the name of
the executable, from the first element of the string command.
Can be connected to
:class:`vdat.command_interpreter.signals.CICommandLogger`
"""
exe = string_command.split()[0]
log = logging.getLogger(name=exe)
log.info(string_command)
if string_info:
log.info(string_info)
if string_warning:
log.warning(string_warning)
if string_error:
log.error(string_error)
if string_critical:
log.critical(string_critical)