Skip to content
Snippets Groups Projects
Commit d6f6e0e2 authored by James Vasile's avatar James Vasile
Browse files

Invoice linting

parent 961c3296
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
# Lint an invoice. So far, this just makes sure dates match.
import datetime
import dateparser
import os
from pathlib import Path
import sys
def date2ref(datestr:str, fname:Path):
return dateparser.parse(datestr).strftime("%Y%m%d") + "-" + fname.split("-")[1]
def get_text_in_braces(line:str):
"""Return text between first set of curly braces"""
return line.split("{")[1].split("}")[0]
def lint_invoice(fname:Path) -> bool:
"""Return True if lint passes, else false"""
invoice = fname.read_text()
for line in invoice.split("\n"):
if line.startswith(r"\date"):
datestr = get_text_in_braces(line)
elif line.startswith(r"\myref"):
myref = get_text_in_braces(line)
if fname.name != myref+".ltx":
sys.stderr.write("lint FAIL: Filename date/serial doesn't match myref date/serial\n")
return False
elif date2ref(datestr, fname.stem) != myref:
sys.stderr.write(f"lint FAIL: {datestr}, {myref}, and {fname.name} do not agree.\n")
return False
return True
def lint_dir(direc:Path) -> bool:
for fname in direc.iterdir():
if fname.suffix == ".ltx":
if not lint_invoice(fname):
return False
return True
sys.exit(not lint_dir(Path(os.getcwd())))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment