def ls(args, api): request = {'long': {'flag': '-l'}, 'directory': {'flag': '-d'}} p = ArgParser() params = p.parse_args(args, request) mapping = params[0] long = mapping.get('long', False) directory = mapping.get('directory', False) names = params[1] or [] data = dict(long=long, directory=directory, names=names) res, r = send_request(api, data) if not res: return False # 输出文件的信息 if long: files = [] for block in res['output']: files.extend(list(block.values())[0]) format_output(files) else: for block in res['output']: for key, files in block.items(): if not files: continue for file in files: print(file) # 输出错误信息 if not res['status']: for e in res['errors']: print('error:', e) return False else: return True
def rmdir(args, api): request = {'parents': {'flag': '-p'}, 'verbose': {'flag': '-v'}} p = ArgParser() params = p.parse_args(args, request) mapping = params[0] parents = mapping.get('parents', False) verbose = mapping.get('verbose', False) names = params[1] or [] data = dict(parents=parents, verbose=verbose, names=names) res, r = send_request(api, data) if not res: return False # -v, 输出详细信息 if verbose: for name in res['output']: print('removing directory: %s' % name) # 输出错误信息 if not res['status']: for e in res['errors']: print('error:', e) return False else: return True
def login(args, api): """登入,并保存cookie""" request = { 'username': { 'flag': '-u', 'arg': 1 }, 'password': { 'flag': '-p', 'arg': 1 } } p = ArgParser() params = p.parse_args(args, request) mapping = params[0] username = mapping.get('username') password = mapping.get('password') assert username and password, 'user name and password are required' data = {'username': username, 'password': password} res, r = send_request(api, data, send_cookies=False) if not res: return False if res['status']: sid = r.cookies.get('sessionid') if sid: save_session({'sessionid': sid}) return True else: print(res['errors']) return False
def cp(args, api): """ 上传下载 操作目录,断点续传,秒传。 -o 参数使得不上传服务器上已有的文件 (秒传) -r 参数用于上传下载目录 -v 参数用于显示过程 """ request = { 'one': { 'flag': '-o' }, 'recursive': { 'flag': '-r' }, 'verbose': { 'flag': '-v' } } p = ArgParser() params = p.parse_args(args, request) mapping = params[0] one = mapping.get('one', False) recursive = mapping.get('recursive', False) verbose = mapping.get('verbose', False) names = params[1] or [] src, dst, is_upload = process_names(names) res = True for src_path in src: status, errmsg = upload(src_path, dst, one, recursive, verbose) if not status: print(errmsg) res = False return res