From 54b092a4e590f557a2e7876192372b305aa5c633 Mon Sep 17 00:00:00 2001 From: Martin Folkers Date: Tue, 11 May 2021 10:46:04 +0200 Subject: [PATCH] Rename 'lib' to 'core' to avoid git error * Move CSV export to utils as 'dump_csv' * Make date of last order human-readable * Fix git error since 'lib/' is usually ignored * Remove deprecated import of 'expanduser' --- {lib => core}/__init__.py | 0 {lib => core}/config.py | 5 +++-- {lib => core}/database.py | 6 +++--- {lib => core}/tasks.py | 37 ++++++++++++------------------------- {lib => core}/utils.py | 11 +++++++++-- main.py | 6 +++--- 6 files changed, 30 insertions(+), 35 deletions(-) rename {lib => core}/__init__.py (100%) rename {lib => core}/config.py (96%) rename {lib => core}/database.py (98%) rename {lib => core}/tasks.py (90%) rename {lib => core}/utils.py (92%) diff --git a/lib/__init__.py b/core/__init__.py similarity index 100% rename from lib/__init__.py rename to core/__init__.py diff --git a/lib/config.py b/core/config.py similarity index 96% rename from lib/config.py rename to core/config.py index 4a644ba..5d7de36 100644 --- a/lib/config.py +++ b/core/config.py @@ -1,12 +1,13 @@ # ~*~ coding=utf-8 ~*~ + from configparser import SafeConfigParser from os import getcwd -from os.path import expanduser, isfile, join, realpath +from os.path import isfile, join, realpath from xdg import xdg_config_home, xdg_data_home -from lib.utils import create_path +from core.utils import create_path class Config(object): diff --git a/lib/database.py b/core/database.py similarity index 98% rename from lib/database.py rename to core/database.py index c5bbeb6..61ad5b3 100644 --- a/lib/database.py +++ b/core/database.py @@ -7,8 +7,8 @@ from operator import itemgetter from shutil import move -from lib.utils import load_csv, load_json, dump_json -from lib.utils import build_path, dedupe, group_data +from core.utils import load_csv, load_json, dump_json +from core.utils import build_path, dedupe, group_data class Database: @@ -255,7 +255,7 @@ def import_invoices(self) -> None: # Helper tasks - def convert_date(self, string) -> str: + def convert_date(self, string: str) -> str: return datetime.strptime(string, '%d.%m.%Y').strftime('%Y-%m-%d') diff --git a/lib/tasks.py b/core/tasks.py similarity index 90% rename from lib/tasks.py rename to core/tasks.py index 32d2cea..26488ec 100644 --- a/lib/tasks.py +++ b/core/tasks.py @@ -7,11 +7,10 @@ import click import pendulum -from pandas import DataFrame from PyPDF2 import PdfFileReader, PdfFileMerger -from lib.utils import load_json -from lib.utils import build_path, create_path, dedupe, group_data +from core.utils import dump_csv, load_json +from core.utils import build_path, create_path, dedupe, group_data class Tasks: @@ -47,7 +46,9 @@ def task_match_payments(self, year, quarter) -> None: self.export_invoices(matches, invoices) # Write results to CSV files - self.export_matches(matches, self.config.matches_dir) + for code, data in group_data(matches).items(): + csv_file = join(self.config.matches_dir, code, code + '.csv') + dump_csv(data, csv_file) def match_payments(self, payments, orders, infos) -> list: @@ -170,24 +171,6 @@ def export_invoices(self, matches, invoice_list) -> None: merger.write(invoice_file) - def export_matches(self, matches, base_dir) -> None: - for code, data in group_data(matches).items(): - # Assign CSV file path & create directory if necessary - csv_file = join(base_dir, code, code + '.csv') - create_path(csv_file) - - # Write matches to CSV file - DataFrame(data).to_csv(csv_file, index=False) - - - def export_csv(self, data, csv_file) -> None: - # Create directory if necessary - create_path(csv_file) - - # Write CSV file - DataFrame(data).to_csv(csv_file, index=False) - - def task_rank_sales(self, year, quarter) -> None: # Select order files to be analyzed order_files = build_path(self.config.order_dir, year=year, quarter=quarter) @@ -210,7 +193,7 @@ def task_rank_sales(self, year, quarter) -> None: file_name = basename(order_files[0])[:-5] + '_' + basename(order_files[-1])[:-5] + '_' + str(count) ranking_file = join(self.config.rankings_dir, file_name + '.csv') - self.export_csv(ranking, ranking_file) + dump_csv(ranking, ranking_file) def rank_sales(self, orders: list) -> list: @@ -265,7 +248,7 @@ def task_create_contacts(self, cutoff_date: str): file_name = cutoff_date + '_' + today.to_datetime_string()[:10] contacts_file = join(self.config.contacts_dir, file_name + '.csv') - self.export_csv(contacts, contacts_file) + dump_csv(contacts, contacts_file) def create_contacts(self, orders: list, cutoff_date: str = None, blocklist = []) -> list: @@ -299,10 +282,14 @@ def create_contacts(self, orders: list, cutoff_date: str = None, blocklist = []) contact['Nachname'] = order['Nachname'] contact['Name'] = order['Name'] contact['Email'] = order['Email'] - contact['Letzte Bestelltung'] = order['Datum'] + contact['Letzte Bestelltung'] = self.convert_date(order['Datum']) if mail_address not in codes: codes.add(mail_address) contacts.append(contact) return contacts + + + def convert_date(self, string: str) -> str: + return datetime.strptime(string, '%Y-%m-%d').strftime('%d.%m.%Y') diff --git a/lib/utils.py b/core/utils.py similarity index 92% rename from lib/utils.py rename to core/utils.py index c094856..5c405bf 100644 --- a/lib/utils.py +++ b/core/utils.py @@ -1,4 +1,3 @@ -#! /usr/bin/python # ~*~ coding=utf-8 ~*~ @@ -8,7 +7,7 @@ from os import makedirs from os.path import abspath, exists, dirname, join, realpath -from pandas import concat, read_csv +from pandas import DataFrame, concat, read_csv # CSV tasks @@ -23,6 +22,14 @@ def load_csv(csv_files, encoding='iso-8859-1', delimiter=';') -> list: return df.to_dict('records') +def dump_csv(data, csv_file) -> None: + # Create directory if necessary + create_path(csv_file) + + # Write CSV file + DataFrame(data).to_csv(csv_file, index=False) + + # JSON tasks def load_json(json_files) -> list: diff --git a/main.py b/main.py index 15e754e..21e8a8c 100755 --- a/main.py +++ b/main.py @@ -6,9 +6,9 @@ import click -from lib.config import Config -from lib.database import Database -from lib.tasks import Tasks +from core.config import Config +from core.database import Database +from core.tasks import Tasks pass_config = click.make_pass_decorator(Config, ensure=True)