Skip to content

Commit

Permalink
moving the logger to common module (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunilmut authored Feb 5, 2024
1 parent ec6b087 commit 52819f0
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ on your system
In the opened command prompt, run:

```
pip3 install guizero numpy pandas xlrd xlsxwriter matplotlib openpyxl nested_dict h5py scipy pathlib
pip3 install guizero numpy pandas xlrd xlsxwriter matplotlib openpyxl nested_dict h5py scipy pathlib bcolors==1.0.2
```

## Update the code:
Expand Down
41 changes: 9 additions & 32 deletions binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
OUTPUT_NO_PARAMETERS = "_Not"
OUTPUT_ZERO_TO_ONE_CSV_NAME = "01"
OUTPUT_ONE_TO_ZERO_CSV_NAME = "10"
OUTPUT_LOG_FILE = "output.txt"
OUTPUT_LOG_FILE = "binary_output.log"

# UI related constants
INPUT_FOLDER_NAME_BOX_MAX_WIDTH = 26
Expand Down Expand Up @@ -211,7 +211,7 @@ def format_out_name_with_param_val(
):
global param_file_exists

if param_file_exists == False or (
if param_file_exists is False or (
param_min_time_duration_before == 0 and param_min_time_duration_after == 0
):
return ""
Expand Down Expand Up @@ -668,7 +668,7 @@ def process_input_file(input_file, output_folder):

# Get a base output DataFrame without any criteria's applied
result, out_df = process_input_df(df)
if result == False or out_df.empty:
if result is False or out_df.empty:
return False

out_base_file = out_base(input_file, output_folder)
Expand Down Expand Up @@ -806,32 +806,6 @@ def get_input_files(input_dir):
return input_files


class loghandler(logging.StreamHandler):
"""
Custom logging handler
"""

def emit(self, record):
"""
Writes the message to the output file (or the default logger stream),
stdout and the UI result text box
"""
try:
# TODO: Have different log format for input depending on whether
# it is the file output or anything else. And, also based on the
# log level. Ex: error logs can be prepend with some string like
# "ERROR:: "
msg = self.format(record)
if r_log_box is not None:
r_log_box.value += msg
print(msg)
self.flush()
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)


def main(input_folder_or_file, separate_files, output_folder):
global output_dir, create_output_folder_with_file_name

Expand Down Expand Up @@ -1267,13 +1241,13 @@ def csv_file_name_to_output():
Main entry point
"""

progress = loghandler()
custom_log_handler = common.loghandler()
logging.basicConfig(filename=OUTPUT_LOG_FILE,
level=logging.INFO, format="")
common.logger = logging.getLogger(__name__)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
progress.setFormatter(formatter)
common.logger.addHandler(progress)
custom_log_handler.setFormatter(formatter)
common.logger.addHandler(custom_log_handler)
argv = sys.argv[1:]
console_mode = False
separate_files = False
Expand Down Expand Up @@ -1519,6 +1493,9 @@ def csv_file_name_to_output():
r_log_box = TextBox(
rwin, text="", height=200, width=500, multiline=True, scrollbar=True
)

custom_log_handler.set_result_log_ui_box(r_log_box)

# Non editable
r_log_box.disable()

Expand Down
2 changes: 1 addition & 1 deletion colorsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from nested_dict import nested_dict

# constants
OUTPUT_LOG_FILE = "output.txt"
OUTPUT_LOG_FILE = "colorsort_output.log"

# globals
output_file = ""
Expand Down
49 changes: 49 additions & 0 deletions common.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/python

import logging
import os
import subprocess
import sys
from csv import reader

import bcolors
import pandas as pd
from pandas.api.types import is_numeric_dtype

Expand All @@ -22,6 +24,10 @@
OUTPUT_DIR_NAME = "_output"
CSV_EXT = ".csv"

# Colors
LIGHT_RED_COLOR = "#FFCCCB"
LIGHT_YELLOW_COLOR = "#FFFFED"

# globals
logger = None
input_dir = ""
Expand Down Expand Up @@ -179,3 +185,46 @@ def compare_csv_files(self, expected_csv_file, actual_file):
else:
self.assertEqual(exp_w, actual_w)
x += 1


class loghandler(logging.StreamHandler):
"""
Custom logging handler
"""

def __init__(self):
self.result_log_ui_box = None
logging.StreamHandler.__init__(self=self)

def set_result_log_ui_box(self, result_log_ui_box):
self.result_log_ui_box = result_log_ui_box

def emit(self, record):
"""
Writes the message to the output file (or the default logger stream),
stdout and the UI result text box
"""
try:
msg = self.format(record)
match record.levelno:
case logging.WARNING:
p_msg = bcolors.WARN + msg + bcolors.ENDC
case logging.CRITICAL | logging.ERROR:
p_msg = bcolors.ERR + msg + bcolors.ENDC
case _:
p_msg = msg
if self.result_log_ui_box is not None:
match record.levelno:
case logging.WARNING:
# If it wasn't previously set to higher attention.
if not self.result_log_ui_box.bg == LIGHT_RED_COLOR:
self.result_log_ui_box.bg = LIGHT_YELLOW_COLOR
case logging.CRITICAL | logging.ERROR:
self.result_log_ui_box.bg = LIGHT_RED_COLOR
self.result_log_ui_box.value += msg
print(p_msg)
self.flush()
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
37 changes: 9 additions & 28 deletions dff.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
PARAM_UI_TIME_WINDOW_DURATION_TEXT = "Time window duration (secs): "

# Other constants
OUTPUT_LOG_FILE = "output.txt"
OUTPUT_LOG_FILE = "dff_output.log"
OUTPUT_COL0_TS = "Start time (sec)"
OUTPUT_COL1_LEN = "Bout length (sec)"
OUTPUT_COL2_MI_AVG = "Motion Index (avg)"
Expand Down Expand Up @@ -134,7 +134,7 @@ def main(input_dir: str, parameter_obj: Parameters):
)
for idx, v in enumerate(nan):
# Discard NaN
if v == False:
if v is False:
cleaned_z_score.append(z_score[idx])
cleaned_ts.append(ts[idx])
z_score = cleaned_z_score
Expand Down Expand Up @@ -217,7 +217,7 @@ def main(input_dir: str, parameter_obj: Parameters):
out_df_not = results[17]

param_ext = ""
if param == None:
if param is None:
param_ext = "_outside-parameters"
elif not param == "":
param_ext = "_" + param
Expand Down Expand Up @@ -598,28 +598,6 @@ def process(
]


class loghandler(logging.StreamHandler):
"""
Custom logging handler
"""

def emit(self, record):
"""
Writes the message to the output file (or the default logger stream),
stdout and the UI result text box
"""
try:
msg = self.format(record)
if r_log_box is not None:
r_log_box.value += msg
print(msg)
self.flush()
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)


def print_help():
"""
Display help
Expand Down Expand Up @@ -843,13 +821,13 @@ def line_r(rwin):
Main entry point
"""

progress = loghandler()
custom_log_handler = common.loghandler()
logging.basicConfig(filename=OUTPUT_LOG_FILE,
level=logging.INFO, format="")
common.logger = logging.getLogger(__name__)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
progress.setFormatter(formatter)
common.logger.addHandler(progress)
custom_log_handler.setFormatter(formatter)
common.logger.addHandler(custom_log_handler)
argv = sys.argv[1:]
console_mode = False
separate_files = False
Expand Down Expand Up @@ -1034,6 +1012,9 @@ def line_r(rwin):
r_log_box = TextBox(
rwin, text="", height=200, width=500, multiline=True, scrollbar=True
)

custom_log_handler.set_result_log_ui_box(r_log_box)

# Non editable
r_log_box.disable()

Expand Down
3 changes: 1 addition & 2 deletions test_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ def setUp(self):
level=logging.DEBUG, format="")
common.logger = logging.getLogger(__name__)
if not common.logger.handlers:
progress = b.loghandler()
common.logger.addHandler(progress)
common.logger.addHandler(common.loghandler())

def validate_df(self, df, expected_data):
expected_df = pd.DataFrame(expected_data)
Expand Down
3 changes: 1 addition & 2 deletions test_dff.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def setUp(self):
level=logging.INFO, format="")
common.logger = logging.getLogger(__name__)
if not common.logger.handlers:
progress = dff.loghandler()
common.logger.addHandler(progress)
common.logger.addHandler(common.loghandler())

def test_bvt(self):
# Whole duration parameter
Expand Down
2 changes: 1 addition & 1 deletion test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def validate_min_t_duration(self, input_dir):
min_time_duration_before,
min_time_duration_after,
) = expected_param.get_min_time_duration_values()
self.assertEqual(param_file_exists == False, True)
self.assertFalse(param_file_exists, False)
self.assertEqual(
min_time_duration_before == Parameters.MIN_TIME_DURATION_BEFORE_DEFAULT,
True,
Expand Down

0 comments on commit 52819f0

Please sign in to comment.