Skip to content

Commit

Permalink
Threading: Add Support for Threading for all Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tks18 committed May 23, 2021
1 parent ede5e43 commit 6fc4d73
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 9 deletions.
60 changes: 55 additions & 5 deletions app/common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from openpyxl.utils import get_column_letter
from openpyxl.styles import Font

from app.common.threader import threader


def resource_path(relative_path):
try:
Expand Down Expand Up @@ -75,7 +77,7 @@ def get_user_json_directory(
return {"ready_to_process": False, "source_dir": "", "final_dir": ""}


def write_basic_data_sheet(work_sheet, basic_data, heading_map):
def basic_data_function(work_sheet, basic_data, heading_map):
work_sheet[f"{get_column_letter(4)}3"] = "Particulars"
work_sheet[f"{get_column_letter(4)}3"].font = Font(bold=True)
work_sheet[f"{get_column_letter(5)}3"] = "Value"
Expand Down Expand Up @@ -104,7 +106,7 @@ def write_basic_data_sheet(work_sheet, basic_data, heading_map):
basic_data_column = start_column


def generate_invoices_list(
def gen_invoices_function(
sales_data, sales_type, invoice_term, app_mode, create_file_dir_modes, file_name
):
invoice_list = []
Expand Down Expand Up @@ -137,7 +139,7 @@ def generate_invoices_list(
return invoice_list


def write_invoices_to_excel(work_sheet, invoice_list, heading_map, heading_list):
def invoice_to_excel_function(work_sheet, invoice_list, heading_map, heading_list):
heading_ref_map = {}
heading_column = 1
heading_row = 1
Expand Down Expand Up @@ -174,6 +176,54 @@ def write_invoices_to_excel(work_sheet, invoice_list, heading_map, heading_list)
invoice_row += 1


def make_archive(path_to_files, file_name):
def archive_function(path_to_files, file_name):
shutil.make_archive(base_name=file_name, format="zip", root_dir=path_to_files)
shutil.rmtree(path_to_files)
shutil.rmtree(path_to_files)


def write_basic_data_sheet(work_sheet, basic_data, heading_map):
basic_data_thread = threader(
function=basic_data_function,
work_sheet=work_sheet,
basic_data=basic_data,
heading_map=heading_map,
)
basic_data_thread.start()
basic_data_thread.join()


def generate_invoices_list(
sales_data, sales_type, invoice_term, app_mode, create_file_dir_modes, file_name
):
gen_invoices_thread = threader(
function=gen_invoices_function,
sales_data=sales_data,
sales_type=sales_type,
invoice_term=invoice_term,
app_mode=app_mode,
create_file_dir_modes=create_file_dir_modes,
file_name=file_name,
)
gen_invoices_thread.start()
invoice_list = gen_invoices_thread.join()
return invoice_list


def write_invoices_to_excel(work_sheet, invoice_list, heading_map, heading_list):
invoices_to_excel_thread = threader(
function=invoice_to_excel_function,
work_sheet=work_sheet,
invoice_list=invoice_list,
heading_map=heading_map,
heading_list=heading_list,
)
invoices_to_excel_thread.start()
invoices_to_excel_thread.join()


def make_archive(path_to_files, file_name):
archive_thread = threader(
function=archive_function, path_to_files=path_to_files, file_name=file_name
)
archive_thread.start()
archive_thread.join()
16 changes: 16 additions & 0 deletions app/common/threader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import threading


class threader(threading.Thread):
def __init__(self, function, **arguments):
threading.Thread.__init__(self)
self.func = function
self.arguments = arguments
self.return_val = None

def run(self):
self.return_val = self.func(**self.arguments)

def join(self):
threading.Thread.join(self)
return self.return_val
57 changes: 53 additions & 4 deletions app/reco/gstr_1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
from app.common.helpers import generate_invoices_list
from openpyxl import workbook
import csv
from pathlib import Path

def generate_csv_template():
pass

def get_paths():
csv_write_path = Path(
input("\nEnter the Dir to which the Format CSV Has to be Saved? ").lower()
)
return csv_write_path


def generate_csv_template(write_path):
with open(
file=Path(f"{write_path}/ledger_data_format.csv"), mode="w", newline=""
) as csv_file:
headers = [
"Date",
"Particulars",
"Type",
"GSTIN/UIN",
"Taxability",
"Invoice Value",
"Taxable",
"Integrated Tax",
"Central Tax",
"State Tax",
"Total Tax",
"Rate",
"Month",
]
writer = csv.DictWriter(csv_file, fieldnames=headers)

writer.writeheader()
writer.writerow(
{
"Date": "01-04-2018",
"Particulars": "Supplier name",
"Type": "Taxable",
"GSTIN/UIN": "09BIOPA1154J1Z9",
"Taxability": "TRUE",
"Invoice Value": 79868.30,
"Taxable": 67685.00,
"Integrated Tax": 0,
"Central Tax": 6091.65,
"State Tax": 6091.65,
"Total Tax": 12183.30,
"Rate": 0.18,
"Month": "Apr-18",
}
)


paths = get_paths()
generate_csv_template(write_path=paths)

0 comments on commit 6fc4d73

Please sign in to comment.