Skip to content
This repository has been archived by the owner on Sep 5, 2022. It is now read-only.

Commit

Permalink
Rename 'lib' to 'core' to avoid git error
Browse files Browse the repository at this point in the history
* 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'
  • Loading branch information
S1SYPHOS authored May 11, 2021
1 parent d56ce5a commit 54b092a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 35 deletions.
File renamed without changes.
5 changes: 3 additions & 2 deletions lib/config.py → core/config.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
6 changes: 3 additions & 3 deletions lib/database.py → core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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')


Expand Down
37 changes: 12 additions & 25 deletions lib/tasks.py → core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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')
11 changes: 9 additions & 2 deletions lib/utils.py → core/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#! /usr/bin/python
# ~*~ coding=utf-8 ~*~


Expand All @@ -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
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 54b092a

Please sign in to comment.