Improves Beancount entry generation and sanitization
Adds a function to sanitize strings for use as Beancount links, ensuring compatibility with Beancount's link restrictions. Refactors the journal entry creation process to use EUR-based postings when fiat currency is provided, improving accuracy and consistency. The legacy SATS-based fallback is retained for cases without fiat currency information. Adjusts reference generation for Beancount entries using the sanitized description.
This commit is contained in:
parent
0e93fc5ffc
commit
a6b67b7416
2 changed files with 57 additions and 11 deletions
|
|
@ -238,6 +238,30 @@ class AccountLookup:
|
|||
|
||||
# ===== CONVERSION FUNCTIONS =====
|
||||
|
||||
def sanitize_link(text: str) -> str:
|
||||
"""
|
||||
Sanitize a string to make it valid for Beancount links.
|
||||
|
||||
Beancount links can only contain: A-Z, a-z, 0-9, -, _, /, .
|
||||
All other characters are replaced with hyphens.
|
||||
|
||||
Examples:
|
||||
>>> sanitize_link("Test (pending)")
|
||||
'Test-pending'
|
||||
>>> sanitize_link("Invoice #123")
|
||||
'Invoice-123'
|
||||
>>> sanitize_link("import-20250623-Action Ressourcerie")
|
||||
'import-20250623-Action-Ressourcerie'
|
||||
"""
|
||||
import re
|
||||
# Replace any character that's not alphanumeric, dash, underscore, slash, or period with a hyphen
|
||||
sanitized = re.sub(r'[^A-Za-z0-9\-_/.]', '-', text)
|
||||
# Remove consecutive hyphens
|
||||
sanitized = re.sub(r'-+', '-', sanitized)
|
||||
# Remove leading/trailing hyphens
|
||||
sanitized = sanitized.strip('-')
|
||||
return sanitized
|
||||
|
||||
def eur_to_sats(eur_amount: Decimal, btc_eur_rate: float) -> int:
|
||||
"""Convert EUR to satoshis using BTC/EUR rate"""
|
||||
btc_amount = eur_amount / Decimal(str(btc_eur_rate))
|
||||
|
|
@ -454,10 +478,13 @@ def convert_to_castle_entry(parsed: dict, btc_eur_rate: float, account_lookup: A
|
|||
"metadata": metadata
|
||||
})
|
||||
|
||||
# Create sanitized reference link
|
||||
desc_part = sanitize_link(parsed['description'][:30])
|
||||
|
||||
return {
|
||||
"description": parsed['description'],
|
||||
"entry_date": parsed['date'].isoformat(),
|
||||
"reference": f"import-{parsed['date'].strftime('%Y%m%d')}-{parsed['description'][:20].replace(' ', '-')}",
|
||||
"reference": f"import-{parsed['date'].strftime('%Y%m%d')}-{desc_part}",
|
||||
"flag": "*",
|
||||
"meta": {
|
||||
"source": "beancount_import",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue