def compose_request(args, method, cmd): try: exec("response = client.{}(args, cmd)".format(method)) except Exception as e: exit_with_stderr(str(e)) if cmd == "login": data = { "EMAIL": response["data"]["email"], "AUTH_TOKEN": response["data"]["auth_token"] } write_to_file(".env", data) elif cmd == "list": for k, v in response["data"]["tosts"].iteritems(): sys.stdout.write(k + ": " + v + "\n") elif cmd == "view": access_token = response["data"]["tost"]["access-token"] body = response["data"]["tost"]["body"] exit_with_stdout(access_token + ": " + body) elif cmd == "access": for k, v in response["data"]["propagations"].iteritems(): sys.stdout.write(v["access-token"] + ": " + k + "\n") exit_with_stdout(response["msg"])
def parse_arguments(): try: port = int(sys.argv[1]) except IndexError: exit_with_stderr("please specify a port number!") except ValueError: exit_with_stderr("please specify an integer!") return port
def validate_argv(cmd, args): if cmd == "list": return check_args_length(args, 0) elif cmd in set(["signup", "login", "create", "view", "access"]): return check_args_length(args, 1) elif cmd in set(["edit", "upgrade", "disable"]): return check_args_length(args, 2) exit_with_stderr("invalid command")
def resolve_argv(cmd, args): if cmd == "signup": if not validate_email(args[0]): exit_with_stderr("invalid e-mail") return {"email": args[0], "signup_token": create_token(8)} elif cmd == "login": if not validate_auth_token(args[0]): exit_with_stderr("invalid auth token") return {"auth_token": args[0]} auth = get_auth() if cmd == "list": return auth elif cmd == "create": data = { "body": urllib.unquote(args[0]), "creation_token": create_token(8) } return add_content(auth, data=data) ppgn_token = args[0] if cmd in set(["view", "access"]): return add_content(auth, ppgn_token=ppgn_token) elif cmd == "edit": data = {"body": urllib.unquote(args[1])} return add_content(auth, ppgn_token=ppgn_token, data=data) elif cmd in set(["upgrade", "disable"]): data = {"src-access-token": args[1]} return add_content(auth, ppgn_token=ppgn_token, data=data)
def check_args_length(args, length): if len(args) < length: exit_with_stderr("too few command line arguments!") if len(args) > length: exit_with_stderr("too many command line arguments!") return args