X-Git-Url: http://dolda2000.com/gitweb/?p=fulbank.git;a=blobdiff_plain;f=netbank;h=a1d35666c4ea8d0e7af9ad3eec76526a1b034920;hp=5e7c4742fe87b1bb0154504e2385fd0700aabf2d;hb=HEAD;hpb=a094b3b5d20430beee75635ada3be6b04ae5fb74 diff --git a/netbank b/netbank index 5e7c474..7f52628 100755 --- a/netbank +++ b/netbank @@ -1,46 +1,13 @@ #!/usr/bin/python3 -import sys, os, getopt, pwd, operator -from fulbank import auth +import sys, os, getopt, pwd +from fulbank import auth, data, util -sesstype = None +sessname = data.defaultsess() sess = None -def pfxmatch(pfx, item): - return str(item)[:len(pfx)] == pfx - -class ambiguous(LookupError): - def __init__(self, a, b): - super().__init__("ambigous match: %s and %s" % (a, b)) - self.a = a - self.b = b - -def find(seq, *, item=None, test=None, match=None, key=None, default=LookupError): - if key is None: - key = lambda o: o - if match is None and item is not None: - match = lambda o: test(item, o) - if test is None: - test = operator.eq - found = None - for thing in seq: - if match(key(thing)): - if found is None: - found = thing - else: - if default is LookupError: - raise ambiguous(key(found), key(thing)) - else: - return default - if found is not None: - return found - if default is LookupError: - raise LookupError() - else: - return default - def usage(out): - out.write("usage: netbank [-h] BANK-ID COMMAND [ARGS...]\n") + out.write("usage: netbank [-h] [-s SESSION-ID] COMMAND [ARGS...]\n") def requiresess(fn): def wrap(cmd, args): @@ -54,23 +21,23 @@ commands = {} def cmd_login(cmd, args): global sess - if len(args) < 1: - sys.stderr.write("usage: login TYPE\n") + if len(args) < 2: + sys.stderr.write("usage: login BANK-ID TYPE [ARGS...]\n") sys.exit(1) - sess = sesstype.create() - if args[0] == "bankid": + sess = data.getsessnam(args[0]).create() + if args[1] == "bankid": authfun = sess.auth_bankid - elif args[0] == "token": + elif args[1] == "token": authfun = sess.auth_token else: - sys.stderr.write("netbank: %s: unknown authentication type\n" % (args[0])) + sys.stderr.write("netbank: %s: unknown authentication type\n" % (args[1])) sys.exit(1) - if len(args) < 2: + if len(args) < 3: sys.stderr.write("usage: login bankid USER-ID\n") sys.exit(1) with auth.ttyconv() as conv: try: - authfun(args[1], conv) + authfun(args[2], conv) except auth.autherror as err: sys.stderr.write("netbank: authentication failed: %s\n" % err) sys.exit(1) @@ -106,8 +73,8 @@ def cmd_lstxn(cmd, args): sys.stderr.write("usage: lstxn [-n NUM] ACCOUNT\n") sys.exit(1) try: - acct = find(sess.accounts, item=args[0], key=lambda acct: acct.number, test=pfxmatch) - except ambiguous as exc: + acct = util.find(sess.accounts, item=args[0], key=lambda acct: acct.number, test=util.pfxmatch) + except util.ambiguous as exc: sys.stderr.write("netbank: %s: ambiguous match between %s and %s\n" % (args[0], exc.a, exc.b)) sys.exit(1) except LookupError: @@ -118,44 +85,29 @@ def cmd_lstxn(cmd, args): commands["lstxn"] = cmd_lstxn def main(): - global sess, sesstype + global sess, sessname - opts, args = getopt.getopt(sys.argv[1:], "h") + opts, args = getopt.getopt(sys.argv[1:], "hs:") for o, a in opts: if o == "-h": usage(sys.stdout) sys.exit(0) - if len(args) < 2: + if o == "-s": + sessname = a + if len(args) < 1: usage(sys.stderr) sys.exit(1) - if args[0] == "fsb": - import fulbank.fsb - sesstype = fulbank.fsb.session - else: - sys.stderr.write("netbank: %s: unknown bank id\n" % (args[0])) - sys.exit(1) - sesspath = os.path.join(pwd.getpwuid(os.getuid()).pw_dir, ".cache/fulbank", args[0]) - cmd = args[1] - args = args[2:] + cmd = args[0] + args = args[1:] - if os.path.exists(sesspath): - sess = sesstype.load(sesspath) - else: - sess = None + sess = data.loadsess(sessname, None) if cmd in commands: commands[cmd](cmd, args) else: sys.stderr.write("netbank: %s: unknown command\n" % (cmd)) sys.exit(1) - if sess is not None: - sessdir = os.path.dirname(sesspath) - if not os.path.isdir(sessdir): - os.makedirs(sessdir) - sess.save(sesspath) - else: - if os.path.exists(sesspath): - os.unlink(sesspath) + data.savesess(sess, sessname) try: if __name__ == "__main__":