105 lines
2.9 KiB
Python
105 lines
2.9 KiB
Python
import argparse
|
|
import yaml
|
|
from pathlib import Path
|
|
|
|
from invoice_generator import html_generator
|
|
|
|
|
|
class DataObject:
|
|
def __init__(self, **kwargs):
|
|
for key, value in kwargs.items():
|
|
setattr(self, key, value)
|
|
|
|
|
|
def parse_yaml_file(file_path):
|
|
with open(file_path, 'r') as file:
|
|
return yaml.safe_load(file)
|
|
|
|
|
|
def check_file(file_path):
|
|
try_out_extensions = ['.yaml', '.yml']
|
|
|
|
for ext in try_out_extensions:
|
|
if file_path.exists():
|
|
return file_path
|
|
file_path = file_path.with_suffix(ext)
|
|
return None
|
|
|
|
|
|
# Utility function
|
|
|
|
|
|
def main():
|
|
print('Simple invoice generator for freelancers and small businesses by Torsten Ueberschar')
|
|
print()
|
|
parser = argparse.ArgumentParser(description='Read invoice and envelope data from yaml file')
|
|
parser.add_argument('-b', '--base', type=str, required=True, help='base directory for invoice and envelope files')
|
|
parser.add_argument('-i', '--invoice', type=str, required=True, help='Invoice file name')
|
|
parser.add_argument('-e', '--envelope', type=str, required=False, default='envelope.yaml',
|
|
help='Envelope file name')
|
|
parser.add_argument('-t', '--template', type=str, required=False, default='template',
|
|
help='directory for template files')
|
|
|
|
args = parser.parse_args()
|
|
|
|
invoice_file = args.invoice
|
|
envelope_file = args.envelope
|
|
|
|
print(f'Searching for invoice files in: {args.base}')
|
|
|
|
invoice_file = Path(args.base) / invoice_file
|
|
envelope_file = Path(args.base) / envelope_file
|
|
|
|
invoice_file = check_file(invoice_file)
|
|
envelope_file = check_file(envelope_file)
|
|
|
|
print(f'Found invoice file: {invoice_file}')
|
|
print(f'Found envelope file: {envelope_file}')
|
|
|
|
if invoice_file is None:
|
|
print('Error: No invoice file found')
|
|
return
|
|
if envelope_file is None:
|
|
print('Error: No envelope file found')
|
|
return
|
|
|
|
try:
|
|
invoice = parse_yaml_file(invoice_file)
|
|
envelope = parse_yaml_file(envelope_file)
|
|
|
|
print('Invoice data:')
|
|
print(invoice)
|
|
|
|
print(envelope)
|
|
|
|
print('Envelope data:')
|
|
envelope_data = DataObject(**envelope)
|
|
print(envelope_data.__dict__)
|
|
|
|
print('Invoice data:')
|
|
invoice_data = DataObject(**invoice)
|
|
print(invoice_data.__dict__)
|
|
|
|
except FileNotFoundError as e:
|
|
print(f'Error: {e}')
|
|
return
|
|
except yaml.YAMLError as e:
|
|
print(f'Error: {e}')
|
|
return
|
|
except Exception as e:
|
|
print(f'Error: {e}')
|
|
return
|
|
|
|
generator = html_generator.HtmlTemplate(args.template)
|
|
template = generator.prepare_template(invoice_data, envelope_data)
|
|
|
|
try:
|
|
html_generator.HtmlTemplate.convert_html_to_pdf(template, 'invoice.pdf')
|
|
except Exception as e:
|
|
print(f'Error: {e}')
|
|
return
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|