3 import sys, os, getopt, pwd, operator
4 from fulbank import auth
9 def pfxmatch(pfx, item):
10 return str(item)[:len(pfx)] == pfx
12 class ambiguous(LookupError):
13 def __init__(self, a, b):
14 super().__init__("ambigous match: %s and %s" % (a, b))
18 def find(seq, *, item=None, test=None, match=None, key=None, default=LookupError):
21 if match is None and item is not None:
22 match = lambda o: test(item, o)
31 if default is LookupError:
32 raise ambiguous(key(found), key(thing))
37 if default is LookupError:
43 out.write("usage: netbank [-h] BANK-ID COMMAND [ARGS...]\n")
48 sys.stderr.write("netbank: %s: no current session\n" % (cmd))
55 def cmd_login(cmd, args):
58 sys.stderr.write("usage: login TYPE\n")
60 sess = sesstype.create()
61 if args[0] == "bankid":
63 sys.stderr.write("usage: login bankid USER-ID\n")
65 with auth.ttyconv() as conv:
66 sess.auth_bankid(args[1], conv)
68 sys.stderr.write("netbank: %s: unknown authentication type\n" % (args[0]))
70 commands["login"] = cmd_login
73 def cmd_logout(cmd, args):
78 commands["logout"] = cmd_logout
81 def cmd_ping(cmd, args):
83 commands["ping"] = cmd_ping
86 def cmd_lsacct(cmd, args):
87 for acct in sess.accounts:
88 sys.stdout.write("%s (%s): %s\n" % (acct.number, acct.name, acct.balance))
89 commands["lsacct"] = cmd_lsacct
92 def cmd_lstxn(cmd, args):
93 opts, args = getopt.getopt(args, "n:")
99 sys.stderr.write("usage: lstxn [-n NUM] ACCOUNT\n")
102 acct = find(sess.accounts, item=args[0], key=lambda acct: acct.number, test=pfxmatch)
103 except ambiguous as exc:
104 sys.stderr.write("netbank: %s: ambiguous match between %s and %s\n" % (args[0], exc.a, exc.b))
107 sys.stderr.write("netbank: %s: no such account\n" % (args[0]))
109 for i, txn in zip(range(num), acct.transactions()):
110 sys.stdout.write("%s %s: %s\n" % (txn.date.isoformat(), txn.value, txn.message))
111 commands["lstxn"] = cmd_lstxn
114 global sess, sesstype
116 opts, args = getopt.getopt(sys.argv[1:], "h")
127 sesstype = fulbank.fsb.session
129 sys.stderr.write("netbank: %s: unknown bank id\n" % (args[0]))
131 sesspath = os.path.join(pwd.getpwuid(os.getuid()).pw_dir, ".cache/fulbank", args[0])
135 if os.path.exists(sesspath):
136 sess = sesstype.load(sesspath)
140 commands[cmd](cmd, args)
142 sys.stderr.write("netbank: %s: unknown command\n" % (cmd))
145 sessdir = os.path.dirname(sesspath)
146 if not os.path.isdir(sessdir):
150 if os.path.exists(sesspath):
154 if __name__ == "__main__":
156 except KeyboardInterrupt: