/journals
2024.journal
/scripts
journalcleanup.py
/workdir
justfile
Last active
October 22, 2024 18:22
-
-
Save amelandri/1619377dc9382bb1bb7fb60782e5f9c4 to your computer and use it in GitHub Desktop.
Hledger journal cleanup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import shutil | |
alignColumn = 100 | |
decimalSeparator = ',' | |
addMissingDecimals = True | |
# ------------------------------------------------------------ | |
def cleanup(year): | |
backupFile = f'workdir/{year}_backup.journal' | |
inputFile = f'workdir/{year}_reorder.journal' | |
outputFile = f'journals/{year}.journal' | |
# Let's do a backup of the original file | |
if os.path.exists(backupFile): | |
os.remove(backupFile) | |
print(f"{backupFile} has been deleted.") | |
shutil.copy(outputFile, backupFile) | |
with open(inputFile, 'r') as file_input, open(outputFile, 'w') as file_output: | |
for line in file_input: | |
if line.strip().startswith(("assets:", "expenses:", "revenues:", "liabilities:")): | |
account, amount_comment = line.rsplit(' ', 1) | |
amount_comment = amount_comment.strip(); | |
comment = '' | |
if ';' not in amount_comment: | |
amount = amount_comment.strip() | |
else: | |
amount_comment_split = amount_comment.split(';') | |
amount = amount_comment_split[0].strip() | |
comment = ' ; ' + amount_comment_split[1].strip() | |
if addMissingDecimals and amount != '' and decimalSeparator not in amount: | |
amount += decimalSeparator + '00' | |
spaces = alignColumn - 1 - len(account) - len(amount) | |
newline = f"{account}{' ' * spaces}{amount}{comment}\n" | |
file_output.write(newline) | |
else: | |
file_output.write(line) | |
if os.path.exists(inputFile): | |
os.remove(inputFile) | |
print(f"{inputFile} has been deleted.") | |
else: | |
print(f"{inputFile} does not exist.") | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
print("Usage: python journalcleanup.py year") | |
sys.exit(1) | |
year = sys.argv[1] | |
cleanup(year) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLEAN: | |
hledger print -f journals/2024.journal >> workdir/2024_reorder.journal | |
python scripts/journalcleanup.py 2024 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment