Source code for libka.ka_print
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# pylint: disable=line-too-long
# kate: space-indent on; indent-width 4; replace-tabs on; indent-mode python; remove-trailing-space modified;
# vim: expandtab ts=4
# pylint: enable=line-too-long
############################################################################
# Copyright © 2016-2023 José Manuel Santamaría Lema <panfaust@gmail.com> #
# #
# 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 2 of the License, or #
# (at your option) any later version. #
############################################################################
"""Small library providing utility print functions used by Kubuntu Automation"""
import sys
import os
import subprocess
from termcolor import cprint
from libka.ka_configuration import get_ka_config_parser
this = sys.modules[__name__] #pylint: disable=invalid-name
this._ka_log_file = None #pylint: disable=invalid-name,protected-access
[docs]
def ka_enable_logs(file_name):
"""
Enable logging of everything printed by `ka_print_*()`, `file_name` is
the full path of the log file.
"""
global _ka_log_file #pylint: disable=invalid-name,global-variable-undefined
try:
directory = os.path.dirname(file_name)
if not os.path.exists(directory): #Make sure the directory exists
os.makedirs(directory)
_ka_log_file = open(file_name, 'w')
except Exception as exception: #pylint: disable=broad-except
ka_print_error("Error opening log file %s" % file_name)
ka_print_error(str(exception))
sys.exit(1)
[docs]
def ka_disable_logs():
"""Disable logging of anything printed by `ka_print_*()`."""
global _ka_log_file #pylint: disable=invalid-name,global-variable-undefined
_ka_log_file = None
[docs]
def ka_print_plain(string, end='\n'):
"""Prints `string` without using any special color."""
print(string, end=end)
if _ka_log_file is not None:
_ka_log_file.write(string + end)
_ka_log_file.flush()
[docs]
def ka_print_error(string, end='\n'):
"""Prints `string` using the error color (red)."""
config = get_ka_config_parser()
if config['global'].getboolean('colors'):
cprint(string, 'red', end=end)
else:
print(string, end=end)
if _ka_log_file is not None:
_ka_log_file.write(string + end)
_ka_log_file.flush()
[docs]
def ka_print_warning(string, end='\n'):
"""Prints `string` using the warning color (red)."""
config = get_ka_config_parser()
if config['global'].getboolean('colors'):
cprint(string, 'yellow', end=end)
else:
print(string, end=end)
if _ka_log_file is not None:
_ka_log_file.write(string + end)
_ka_log_file.flush()
[docs]
def ka_print_info(string, end='\n'):
"""Prints `string` using the info color (blue)."""
config = get_ka_config_parser()
if config['global'].getboolean('colors'):
cprint(string, 'blue', attrs=['bold'], end=end)
else:
print(string, end=end)
if _ka_log_file is not None:
_ka_log_file.write(string + end)
_ka_log_file.flush()
[docs]
def ka_print_good_stuff(string, end='\n'):
"""Prints `string` using the Good Stuff color (green)."""
config = get_ka_config_parser()
if config['global'].getboolean('colors'):
cprint(string, 'green', end=end)
else:
print(string, end=end)
if _ka_log_file is not None:
_ka_log_file.write(string + end)
_ka_log_file.flush()
[docs]
def ka_print_git_diff(title='git'):
"""Prints the git diff of the current directory"""
#Set the correct git args for color print or not
git_diff_color = ["git", "--no-pager", "diff", "--color=always"]
git_diff_no_color = ["git", "--no-pager", "diff", "--no-color"]
config = get_ka_config_parser()
if config['global'].getboolean('colors'):
program = git_diff_color
else:
program = git_diff_no_color
#Print the git diff
ka_print_good_stuff("=== %s diff start" % title)
sys.stdout.flush()
program_output = subprocess.check_output(program)
program_output = program_output.decode()
print(program_output)
sys.stdout.flush()
ka_print_good_stuff("=== %s diff end" % title)
#Print to log file if needed
if _ka_log_file is not None:
program_output = subprocess.check_output(program)
program_output = program_output.decode()
print(program_output)
_ka_log_file.write(program_output)
_ka_log_file.flush()
if __name__ == "__main__":
ka_print_error("Error text")
ka_print_warning("Warning text")
ka_print_info("Info text")
ka_print_good_stuff("Good stuff text")