ordigi/ordigi/summary.py

100 lines
2.9 KiB
Python
Raw Normal View History

2021-11-01 11:42:01 +01:00
# import pandas as pd
from tabulate import tabulate
2021-12-05 18:27:04 +01:00
2021-11-01 11:42:01 +01:00
class Tables:
"""Create table and display result in Pandas DataFrame"""
def __init__(self, actions):
self.actions = actions
self.table = []
self.columns = ['action', 'file_path', 'dest_path']
# self.df = self.dataframe()
def append(self, action, file_path=None, dest_path=None):
row = (action, file_path, dest_path)
self.table.append(row)
def sum(self, action=None):
if not action:
return len(self.table)
count = 0
for row in self.table:
if row[0] == action:
count += 1
return count
# def dataframe(self):
# return pd.DataFrame(self.table, columns=self.columns)
def tabulate(self):
errors_headers = self.columns
return tabulate(self.table, headers=errors_headers)
2021-12-05 18:27:04 +01:00
2021-10-18 07:45:54 +02:00
class Summary:
2021-11-01 11:42:01 +01:00
"""Result summary of ordigi program call"""
def __init__(self, root):
2021-10-27 00:06:38 +02:00
self.actions = (
'check',
'import',
2021-11-01 11:42:01 +01:00
'remove',
2021-10-27 00:06:38 +02:00
'sort',
'update',
)
2021-10-18 07:45:54 +02:00
2021-11-01 11:42:01 +01:00
# Set labels
self.state = ['success', 'errors']
self.root = root
self.success_table = Tables(self.actions)
self.errors_table = Tables(self.actions)
2021-10-18 07:45:54 +02:00
self.errors = 0
2021-11-01 11:42:01 +01:00
def append(self, action, success, file_path=None, dest_path=None):
2021-10-27 00:06:38 +02:00
if action:
2021-11-01 11:42:01 +01:00
if success:
self.success_table.append(action, file_path, dest_path)
else:
self.errors_table.append(action, file_path, dest_path)
if not success:
2021-12-05 18:27:04 +01:00
self.errors += 1
2021-09-29 07:36:47 +02:00
def print(self):
2021-11-01 11:42:01 +01:00
"""Print summary"""
print()
2021-11-01 11:42:01 +01:00
for action in self.actions:
nb = self.success_table.sum(action)
if nb != 0:
2021-10-27 00:06:38 +02:00
if action == 'check':
2021-11-01 11:42:01 +01:00
print(f"SUMMARY: {nb} files checked in {self.root}.")
2021-10-27 00:06:38 +02:00
elif action == 'import':
2021-11-01 11:42:01 +01:00
print(f"SUMMARY: {nb} files imported into {self.root}.")
2021-10-27 00:06:38 +02:00
elif action == 'sort':
2021-11-01 11:42:01 +01:00
print(f"SUMMARY: {nb} files sorted inside {self.root}.")
2021-10-27 00:06:38 +02:00
elif action == 'remove_excluded':
2021-11-01 11:42:01 +01:00
print(f"SUMMARY: {nb} files deleted in {self.root}.")
2021-10-27 00:06:38 +02:00
elif action == 'remove_empty_folders':
2021-11-01 11:42:01 +01:00
print(f"SUMMARY: {nb} empty folders removed in {self.root}.")
2021-10-27 00:06:38 +02:00
elif action == 'update':
2021-11-01 11:42:01 +01:00
print(f"SUMMARY: {nb} files updated in {self.root} database.")
2021-10-27 00:06:38 +02:00
2021-11-01 11:42:01 +01:00
success = self.success_table.sum()
if not success and not self.errors:
print(f"SUMMARY: no action done in {self.root}.")
2021-10-18 07:45:54 +02:00
2021-11-01 11:42:01 +01:00
errors = self.errors_table.sum()
if errors:
2021-10-18 07:45:54 +02:00
print()
2021-11-01 11:42:01 +01:00
print(f"ERROR: {errors} errors reported for files:")
print(self.success_table.tabulate())
2021-10-18 07:45:54 +02:00
2021-11-01 11:42:01 +01:00
elif self.errors:
print(f"ERROR: {errors} errors reported.")