Skip to content

Instantly share code, notes, and snippets.

@artkpv
Last active January 30, 2021 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artkpv/5f682827841763c827db6edef78a608b to your computer and use it in GitHub Desktop.
Save artkpv/5f682827841763c827db6edef78a608b to your computer and use it in GitHub Desktop.
Script for `hledger roi` - creates fake transacations for a period end
#!/bin/python3
from sys import argv, stderr
from os import path, chdir
from datetime import date
from subprocess import check_output
from re import sub, search
chdir(path.dirname(__file__))
HFILE = '../all.ledger'
def export(end, accountfilter):
for account in getaccounts():
if accountfilter and not accountfilter in account:
continue
for isopen in (False, True):
try:
export_account(account, isopen, end)
except:
stderr.write(f"Failed to export {account}\n")
def export_account(account, isopen, end):
closetrn = hl(
'close',
account,
'--open' if isopen else '--close',
'-e', end,
'--show-cost',
f"--close-acc={account}",
'--close-desc=Ложная транзакция для ROI',
'--open-desc=Ложная транзакция для ROI')
#print(search('\s+\d+\.\d+\s+(\w+)', closetrn))
#exit()
currency = search('\@\s+\d+(\.\d+)? (\w+) ', closetrn).group(2)
closetrn = sub(f"\n\s*{account}\s*\n", "", closetrn)
closetrn = sub(r"\n$", '', closetrn)
bal = hl( 'bal', account, '-X', currency, '-N', '-e', end)
bal = bal.replace(account,'').strip()
incomeaccount = account.replace('активы', 'доходы')
outtrn = f"""{closetrn}
{account} {'-' if isopen else '' }{bal}
{incomeaccount}
"""
print(outtrn)
def getaccounts():
hlaccount = hl('account', 'активы:цб:')
for l in hlaccount.split('\n'):
yield l
def hl(*arg):
return check_output(['hledger', '-f', HFILE] + list(arg)).decode('utf-8')
if __name__ == '__main__':
end = date.today()
if len(argv) > 1:
try:
end = date.fromisoformat(argv[1])
except:
pass
account = None
if len(argv) > 2:
try:
account = argv[2]
except:
pass
export(end.isoformat(), account)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment