Commit fbb517aa authored by Matthieu Boileau's avatar Matthieu Boileau
Browse files

Color logging in

Fix #22
parent 37b8208b
......@@ -6,12 +6,15 @@ Split YAML file into separate files and clean them
import argparse
import codecs
from colorlog import ColoredFormatter
import itertools as it
import logging
import os
import re
from ruamel.yaml import YAML
from ruamel.yaml.reader import Reader
import shutil
import sys
SPIPFILES = "spip_auteurs.yml", \
"spip_auteurs_liens.yml", \
......@@ -70,7 +73,7 @@ def force_encode(line, iline, codecs=('cp1252', 'utf8')):
return line.decode('utf-8').encode(i).decode()
except (UnicodeDecodeError, UnicodeEncodeError) as e:
print(f"Line = {iline} Error = {e}")
logger.debug(f"Line = {iline} Error = {e}")
def clean_yaml(yml_filename):
......@@ -78,7 +81,7 @@ def clean_yaml(yml_filename):
base_filename = os.path.splitext(yml_filename)[0]
output_filename = base_filename + "_clean.yml"
print(f">>> Cleaning {yml_filename} -> {output_filename}")">>> Cleaning {yml_filename} -> {output_filename}")
yaml = YAML(typ='safe')
# Load original yaml as a list
......@@ -112,7 +115,7 @@ def clean_yaml(yml_filename):
def reset_output_directory():
"""Erase existing output files and create empty output directories"""
if os.path.exists(OUTPUTDIR):
print(f">>> Cleaning output directory: ./{OUTPUTDIR}/")">>> Cleaning output directory: ./{OUTPUTDIR}/")
......@@ -121,7 +124,7 @@ def reset_output_directory():
def split_yaml(yml_filename):
"""Split Spip YAML file into SPIPFILES"""
print(f">>> Splitting {yml_filename} into:")">>> Splitting {yml_filename} into:")
with, "r", encoding='utf-8', errors='ignore') as yml_file:
filename = ''
......@@ -131,7 +134,7 @@ def split_yaml(yml_filename):
filename = f"{list(group)[0].split('.')[1].strip()}.yml"
filepath = os.path.join(OUTPUTDIR, filename)
if not key and filename in SPIPFILES:
print(f" {filename}")" {filename}")
content = "".join(list(group))
with open(filepath, 'w') as yml_subfile:
......@@ -152,6 +155,19 @@ def parse_cl_args():
if __name__ == '__main__':
logger = logging.getLogger()
# create console handler with higher log level and colored output
ch = logging.StreamHandler(sys.stdout)
LOGFORMAT = "%(log_color)s%(message)s%(reset)s"
color_formatter = ColoredFormatter(LOGFORMAT)
# add the handlers to logger
args = parse_cl_args()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment