Better handling of descriptions.
This commit is contained in:
parent
545aa62e73
commit
168f1be13c
|
@ -11,7 +11,7 @@ from rich.table import Table
|
|||
from rich import print as rich_print
|
||||
from rich.panel import Panel
|
||||
|
||||
__version__ = "0.1.1"
|
||||
__version__ = "0.2"
|
||||
|
||||
DATE_RE = r"([0-9]{1,2}/[0-9]{2}/[0-9]{2,4})"
|
||||
|
||||
|
@ -70,6 +70,11 @@ class Line:
|
|||
|
||||
def add_description(self, description_line):
|
||||
"""Add a line to a long description."""
|
||||
description_line = re.sub("\s+", " ", description_line).strip()
|
||||
if not description_line:
|
||||
return
|
||||
if self.description:
|
||||
self.description += "\n"
|
||||
self.description += description_line
|
||||
|
||||
@property
|
||||
|
@ -219,9 +224,13 @@ class Statement:
|
|||
return {}
|
||||
return headers
|
||||
|
||||
def _parse_lines(self):
|
||||
def _parse_lines(self, text):
|
||||
current_line = None
|
||||
for text_line in self.text.splitlines():
|
||||
for text_line in text.splitlines():
|
||||
if text_line.strip() == "":
|
||||
if current_line:
|
||||
self.lines.append(current_line)
|
||||
current_line = None
|
||||
line = self.LineImpl(self, text_line)
|
||||
if line.match:
|
||||
if current_line:
|
||||
|
@ -238,6 +247,18 @@ class Statement:
|
|||
buf.append(str(line))
|
||||
return "\n".join(buf)
|
||||
|
||||
def pretty_print(self, show_desriptions):
|
||||
table = Table()
|
||||
table.add_column("Label", justify="right", style="cyan", no_wrap=True)
|
||||
table.add_column("Value", style="magenta")
|
||||
for line in self.lines:
|
||||
if line.description and show_desriptions:
|
||||
table.add_row(line.label + "\n" + line.description, str(line.value))
|
||||
else:
|
||||
table.add_row(line.label, str(line.value))
|
||||
|
||||
Console().print(table)
|
||||
|
||||
|
||||
class AccountStatement(Statement):
|
||||
LineImpl = AccountLine
|
||||
|
@ -260,32 +281,29 @@ class AccountStatement(Statement):
|
|||
)
|
||||
|
||||
def _parse(self):
|
||||
self._parse_soldes()
|
||||
self._parse_lines()
|
||||
start, stop = self._parse_soldes()
|
||||
self._parse_lines("\n".join(self.text.splitlines()[start + 1 : stop]))
|
||||
|
||||
def _parse_soldes(self):
|
||||
for text in self.text.splitlines():
|
||||
start = stop = 0
|
||||
for lineno, text in enumerate(self.text.splitlines()):
|
||||
line = BalanceBeforeLine(self, text)
|
||||
if line.match:
|
||||
self.balance_before = line.value
|
||||
start = lineno
|
||||
line = BalanceAfterLine(self, text)
|
||||
if line.match:
|
||||
self.balance_after = line.value
|
||||
stop = lineno
|
||||
return start, stop
|
||||
|
||||
def pretty_print(self):
|
||||
def pretty_print(self, show_desriptions):
|
||||
table = Table(title=str(self.filename))
|
||||
table.add_column("Date")
|
||||
table.add_column("RIB")
|
||||
table.add_row(str(self.headers["date"]), self.headers["RIB"])
|
||||
Console().print(table)
|
||||
|
||||
table = Table()
|
||||
table.add_column("Label", justify="right", style="cyan", no_wrap=True)
|
||||
table.add_column("Value", style="magenta")
|
||||
for line in self.lines:
|
||||
table.add_row(line.label, str(line.value))
|
||||
|
||||
Console().print(table)
|
||||
super().pretty_print(show_desriptions)
|
||||
|
||||
|
||||
class CardStatement(Statement):
|
||||
|
@ -310,7 +328,7 @@ class CardStatement(Statement):
|
|||
def _parse(self):
|
||||
self._parse_card_owner()
|
||||
self._parse_card_debit()
|
||||
self._parse_lines()
|
||||
self._parse_lines(self.text)
|
||||
|
||||
def _parse_card_debit(self):
|
||||
for text in self.text.splitlines():
|
||||
|
@ -330,7 +348,7 @@ class CardStatement(Statement):
|
|||
self.headers["card_owner"] = re.sub(r"\s+", " ", match["porteur"])
|
||||
break
|
||||
|
||||
def pretty_print(self):
|
||||
def pretty_print(self, show_descriptions):
|
||||
table = Table(title=str(self.filename))
|
||||
table.add_column("Date")
|
||||
table.add_column("RIB")
|
||||
|
@ -345,14 +363,7 @@ class CardStatement(Statement):
|
|||
self.headers["card_owner"],
|
||||
)
|
||||
Console().print(table)
|
||||
|
||||
table = Table()
|
||||
table.add_column("Label", justify="right", style="cyan", no_wrap=True)
|
||||
table.add_column("Value", style="magenta")
|
||||
for line in self.lines:
|
||||
table.add_row(line.label, str(line.value))
|
||||
|
||||
Console().print(table)
|
||||
super().pretty_print(show_descriptions)
|
||||
|
||||
|
||||
def main():
|
||||
|
@ -366,7 +377,7 @@ def main():
|
|||
statement = Statement.from_pdf(file)
|
||||
if args.debug:
|
||||
rich_print(Panel(statement.text))
|
||||
statement.pretty_print()
|
||||
statement.pretty_print(args.show_descriptions)
|
||||
statement.validate()
|
||||
|
||||
|
||||
|
@ -376,6 +387,7 @@ def parse_args():
|
|||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("-d", "--debug", action="store_true")
|
||||
parser.add_argument("-s", "--show-descriptions", action="store_true")
|
||||
parser.add_argument("files", nargs="*", type=Path)
|
||||
return parser.parse_args()
|
||||
|
||||
|
|
Loading…
Reference in New Issue