usawa

Signed, immutable accounting.
Info | Log | Files | Refs | Submodules | LICENSE

create.py (2406B)


      1 import argparse
      2 import datetime
      3 import logging
      4 
      5 import lxml.etree
      6 import confini
      7 import nacl.signing
      8 
      9 from usawa import Entry, EntryPart, DemoWallet, ACL, get_units, init_ledger
     10 
     11 logging.basicConfig(level=logging.DEBUG)
     12 logg = logging.getLogger()
     13 
     14 default_counter = {
     15     'income': 'asset',
     16     'asset': 'income',
     17     'expense': 'liability',
     18     'liability': 'expense',
     19         }
     20 
     21 seed = bytes.fromhex('2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae')
     22 
     23 if __name__ == '__main__':
     24     now = datetime.datetime.now()
     25     argp = argparse.ArgumentParser()
     26     argp.add_argument('amount', type=str, help='value amount in decimal or whole units')
     27     argp.add_argument('-t', type=str, choices=['income', 'expense', 'asset', 'liability'], default='income', help='delta type')
     28     argp.add_argument('--counter', type=str, choices=['income', 'expense', 'asset', 'liability'], help='delta type')
     29     argp.add_argument('-u', type=str, default='USD', help='unit of account')
     30     argp.add_argument('-r', type=str, help='reference')
     31     argp.add_argument('-p', type=str, help='parent')
     32     argp.add_argument('-a', type=str, default='Miscellaneous', help='account')
     33     argp.add_argument('--date', type=lambda d: datetime.date.fromisoformat(d), default=str(now.date()), help='date of transaction')
     34     argp.add_argument('--xml-file', dest='xml_file', type=str, default='empty.xml', help='xml file to manipulate')
     35     arg = argp.parse_args()
     36 
     37     counter = arg.counter
     38     if counter == None:
     39         counter = default_counter[arg.t]
     40     tree = lxml.etree.parse(arg.xml_file)
     41     root = tree.getroot()
     42     units = get_units(root)
     43     acl = ACL()
     44     wallet = DemoWallet(privatekey=seed)
     45     fakepub = bytes.fromhex('7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730')
     46     acl.add(wallet.pubkey(), 1, label='foo')
     47     ledger = init_ledger(root, units, acl=acl)
     48   
     49     amount = units.from_floatstring(arg.u, arg.amount)
     50     src = EntryPart(arg.t, arg.a, amount, src=True)
     51     dst = EntryPart(counter, arg.a, amount)
     52     #amount = units.from_floatstring(arg.u, arg.amount, allow_negative=False)
     53 
     54     entry = Entry(src, dst, arg.u, ledger.serial + 1, arg.date, parent=ledger.base)
     55     entry.sign(wallet)
     56     ledger.add_entry(entry)
     57     tree = ledger.to_tree()
     58     r = lxml.etree.tostring(tree, method='xml', standalone=True, xml_declaration=True, encoding='UTF-8')
     59     print(r.decode('utf-8'))