Preliminary version seemingly functional
This commit is contained in:
parent
ca78ecd9f0
commit
33c1e9eb9d
@ -29,7 +29,7 @@
|
|||||||
; commodity $
|
; commodity $
|
||||||
|
|
||||||
2008/01/01 income
|
2008/01/01 income
|
||||||
assets:bank:checking $1
|
assets:bank:checking $1.45
|
||||||
income:salary
|
income:salary
|
||||||
|
|
||||||
2008/06/01 gift
|
2008/06/01 gift
|
||||||
|
89
src/db.py
89
src/db.py
@ -1,7 +1,7 @@
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from ledger import Transaction
|
from ledger import Posting, Transaction
|
||||||
|
|
||||||
|
|
||||||
class CursorContextManager:
|
class CursorContextManager:
|
||||||
@ -38,17 +38,53 @@ def create_tables(cursor: sqlite3.Cursor) -> None:
|
|||||||
"""
|
"""
|
||||||
CREATE TABLE transactions
|
CREATE TABLE transactions
|
||||||
(
|
(
|
||||||
|
id integer,
|
||||||
date text,
|
date text,
|
||||||
check_num text,
|
comment text,
|
||||||
note text,
|
|
||||||
account text,
|
|
||||||
amount real,
|
|
||||||
cleared integer,
|
cleared integer,
|
||||||
tags text
|
tags text
|
||||||
)
|
)
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
cursor.execute(
|
||||||
|
"""
|
||||||
|
CREATE TABLE postings
|
||||||
|
(
|
||||||
|
transaction_id integer,
|
||||||
|
account text,
|
||||||
|
amount real,
|
||||||
|
cleared integer,
|
||||||
|
tags text,
|
||||||
|
comment text
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_posting_amount(posting: Posting) -> float:
|
||||||
|
# TODO: Is there a way to denote that these fields are required and avoid all the None checking?
|
||||||
|
|
||||||
|
amount = posting.get("pamount")
|
||||||
|
if amount is None or len(amount) != 1:
|
||||||
|
print("Unexpected number of amounts in posting")
|
||||||
|
print(posting)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
quantity = amount[0].get("aquantity")
|
||||||
|
if quantity is None:
|
||||||
|
print("Unexpected number of amounts in posting")
|
||||||
|
print(posting)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
val = quantity.get("floatingPoint")
|
||||||
|
if val is None:
|
||||||
|
print("Unexpected number of amounts in posting")
|
||||||
|
print(posting)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
return val
|
||||||
|
|
||||||
|
|
||||||
def write_transactions(cursor: sqlite3.Cursor, txs: List[Transaction]) -> None:
|
def write_transactions(cursor: sqlite3.Cursor, txs: List[Transaction]) -> None:
|
||||||
"""
|
"""
|
||||||
@ -56,4 +92,45 @@ def write_transactions(cursor: sqlite3.Cursor, txs: List[Transaction]) -> None:
|
|||||||
and then store it.
|
and then store it.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
print(txs[0])
|
for transaction in txs:
|
||||||
|
cursor.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO transactions
|
||||||
|
(
|
||||||
|
id, date, comment, cleared, tags
|
||||||
|
) VALUES (
|
||||||
|
?, ?, ?, ?, ?
|
||||||
|
)
|
||||||
|
""",
|
||||||
|
[
|
||||||
|
transaction.get("tindex"),
|
||||||
|
transaction.get("tdate"),
|
||||||
|
transaction.get("tcomment"),
|
||||||
|
transaction.get("tstatus"),
|
||||||
|
",".join(transaction.get("ttags") or []),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
postings = transaction.get("tpostings")
|
||||||
|
if postings is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for posting in postings:
|
||||||
|
cursor.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO postings
|
||||||
|
(
|
||||||
|
transaction_id, account, amount, cleared, tags, comment
|
||||||
|
) VALUES (
|
||||||
|
?, ?, ?, ?, ?, ?
|
||||||
|
)
|
||||||
|
""",
|
||||||
|
[
|
||||||
|
transaction.get("tindex"),
|
||||||
|
posting.get("paccount"),
|
||||||
|
get_posting_amount(posting),
|
||||||
|
posting.get("pstatus"),
|
||||||
|
",".join(posting.get("ptags") or []),
|
||||||
|
posting.get("pcomment"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
@ -7,7 +7,7 @@ import typing
|
|||||||
class AmountQuantity(typing.TypedDict):
|
class AmountQuantity(typing.TypedDict):
|
||||||
decimalMantissa: int
|
decimalMantissa: int
|
||||||
decimalPlaces: int
|
decimalPlaces: int
|
||||||
floatingPoint: int
|
floatingPoint: float
|
||||||
|
|
||||||
|
|
||||||
class AmountStyle(typing.TypedDict):
|
class AmountStyle(typing.TypedDict):
|
||||||
|
@ -1,3 +1,8 @@
|
|||||||
|
"""
|
||||||
|
Convert the provided ledger file into a SQLite database.
|
||||||
|
Configure input and output files with environment variables.
|
||||||
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import sys
|
import sys
|
||||||
|
Reference in New Issue
Block a user