From 971e2c11406511329d91c72cd05c936b37a584ee Mon Sep 17 00:00:00 2001 From: jake Date: Thu, 2 May 2024 15:33:35 -0700 Subject: [PATCH] Standardize string quotations to double quotes --- internetarchive/cli/ia_copy.py | 24 +++++----- internetarchive/cli/ia_download.py | 24 +++++----- internetarchive/cli/ia_metadata.py | 66 +++++++++++++------------- internetarchive/cli/ia_move.py | 46 +++++++++---------- internetarchive/cli/ia_search.py | 4 +- internetarchive/cli/ia_tasks.py | 74 +++++++++++++++--------------- 6 files changed, 119 insertions(+), 119 deletions(-) diff --git a/internetarchive/cli/ia_copy.py b/internetarchive/cli/ia_copy.py index cd978476..abbb8650 100644 --- a/internetarchive/cli/ia_copy.py +++ b/internetarchive/cli/ia_copy.py @@ -100,8 +100,8 @@ def main(args: argparse.Namespace, """ SRC_FILE = None - args.header = prepare_args_dict(args.header, parser=parser, arg_type='header') - args.metadata = prepare_args_dict(args.metadata, parser=parser, arg_type='metadata') + args.header = prepare_args_dict(args.header, parser=parser, arg_type="header") + args.metadata = prepare_args_dict(args.metadata, parser=parser, arg_type="metadata") if args.source == args.destination: parser.error("error: The source and destination files cannot be the same!") @@ -116,13 +116,13 @@ def main(args: argparse.Namespace, "does not exist. Please check the " "identifier and filepath and retry.") - args.header['x-amz-copy-source'] = f'/{quote(args.source)}' + args.header["x-amz-copy-source"] = f"/{quote(args.source)}" # Copy the old metadata verbatim if no additional metadata is supplied, # else combine the old and the new metadata in a sensible manner. if args.metadata or args.replace_metadata: - args.header['x-amz-metadata-directive'] = 'REPLACE' + args.header["x-amz-metadata-directive"] = "REPLACE" else: - args.header['x-amz-metadata-directive'] = 'COPY' + args.header["x-amz-metadata-directive"] = "COPY" # New metadata takes precedence over old metadata. if not args.replace_metadata: @@ -133,13 +133,13 @@ def main(args: argparse.Namespace, file_metadata = None if args.ignore_file_metadata else SRC_FILE.metadata # type: ignore # Add keep-old-version by default. - if not args.header.get('x-archive-keep-old-version') and not args.no_backup: - args.header['x-archive-keep-old-version'] = '1' + if not args.header.get("x-archive-keep-old-version") and not args.no_backup: + args.header["x-archive-keep-old-version"] = "1" - url = f'{args.session.protocol}//s3.us.archive.org/{quote(args.destination)}' + url = f"{args.session.protocol}//s3.us.archive.org/{quote(args.destination)}" queue_derive = not args.no_derive req = ia.iarequest.S3Request(url=url, - method='PUT', + method="PUT", metadata=args.metadata, file_metadata=file_metadata, headers=args.header, @@ -153,10 +153,10 @@ def main(args: argparse.Namespace, msg = get_s3_xml_text(r.text) except Exception as e: msg = r.text - print(f'error: failed to {cmd} "{args.source}" to "{args.destination}" - {msg}', + print(f"error: failed to {cmd} '{args.source}' to '{args.destination}' - {msg}", file=sys.stderr) sys.exit(1) - elif cmd == 'copy': - print(f'success: copied "{args.source}" to "{args.destination}".', + elif cmd == "copy": + print(f"success: copied '{args.source}' to '{args.destination}'.", file=sys.stderr) return (r, SRC_FILE) diff --git a/internetarchive/cli/ia_download.py b/internetarchive/cli/ia_download.py index 95434ae6..640c1813 100644 --- a/internetarchive/cli/ia_download.py +++ b/internetarchive/cli/ia_download.py @@ -155,24 +155,24 @@ def main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: params=args.search_parameters) total_ids = _search.num_found if total_ids == 0: - print(f'error: the query "{args.search}" returned no results', file=sys.stderr) + print(f"error: the query '{args.search}' returned no results", file=sys.stderr) sys.exit(1) ids = _search except ValueError as e: - print(f'error: {e}', file=sys.stderr) + print(f"error: {e}", file=sys.stderr) sys.exit(1) # Download specific files. - if args.identifier and args.identifier != '-': - if '/' in args.identifier: - identifier = args.identifier.split('/')[0] - files = ['/'.join(args.identifier.split('/')[1:])] + if args.identifier and args.identifier != "-": + if "/" in args.identifier: + identifier = args.identifier.split("/")[0] + files = ["/".join(args.identifier.split("/")[1:])] else: identifier = args.identifier files = args.file total_ids = 1 ids = [identifier] - elif args.identifier == '-': + elif args.identifier == "-": total_ids = 1 ids = sys.stdin files = None @@ -184,18 +184,18 @@ def main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: try: identifier = identifier.strip() except AttributeError: - identifier = identifier.get('identifier') + identifier = identifier.get("identifier") if total_ids > 1: - item_index = f'{i + 1}/{total_ids}' + item_index = f"{i + 1}/{total_ids}" else: item_index = None try: item = args.session.get_item(identifier) except Exception as exc: - print(f'{identifier}: failed to retrieve item metadata - errors', file=sys.stderr) - if 'You are attempting to make an HTTPS' in str(exc): - print(f'\n{exc}', file=sys.stderr) + print(f"{identifier}: failed to retrieve item metadata - errors", file=sys.stderr) + if "You are attempting to make an HTTPS" in str(exc): + print(f"\n{exc}", file=sys.stderr) sys.exit(1) else: continue diff --git a/internetarchive/cli/ia_metadata.py b/internetarchive/cli/ia_metadata.py index 419812f4..fe030554 100644 --- a/internetarchive/cli/ia_metadata.py +++ b/internetarchive/cli/ia_metadata.py @@ -130,14 +130,14 @@ def modify_metadata(item: item.Item, insert=insert, timeout=args.timeout) assert isinstance(r, Response) # mypy: modify_metadata() -> Request | Response except ItemLocateError as exc: - print(f'{item.identifier} - error: {exc}', file=sys.stderr) + print(f"{item.identifier} - error: {exc}", file=sys.stderr) sys.exit(1) - if not r.json()['success']: - error_msg = r.json()['error'] - etype = 'warning' if 'no changes' in r.text else 'error' - print(f'{item.identifier} - {etype} ({r.status_code}): {error_msg}', file=sys.stderr) + if not r.json()["success"]: + error_msg = r.json()["error"] + etype = "warning" if "no changes" in r.text else "error" + print(f"{item.identifier} - {etype} ({r.status_code}): {error_msg}", file=sys.stderr) return r - print(f'{item.identifier} - success: {r.json()["log"]}', file=sys.stderr) + print(f"{item.identifier} - success: {r.json()['log']}", file=sys.stderr) return r @@ -154,7 +154,7 @@ def remove_metadata(item: item.Item, if not src_md: continue - if key == 'collection': + if key == "collection": _col = copy(metadata[key]) _src_md = copy(src_md) if not isinstance(_col, list): @@ -163,30 +163,30 @@ def remove_metadata(item: item.Item, _src_md = [_src_md] for c in _col: if c not in _src_md: - r = item.remove_from_simplelist(c, 'holdings') + r = item.remove_from_simplelist(c, "holdings") j = r.json() - if j.get('success'): - print(f'{item.identifier} - success: {item.identifier} no longer in {c}', + if j.get("success"): + print(f"{item.identifier} - success: {item.identifier} no longer in {c}", file=sys.stderr) sys.exit(0) - elif j.get('error', '').startswith('no row to delete for'): - print(f'{item.identifier} - success: {item.identifier} no longer in {c}', + elif j.get("error", "").startswith("no row to delete for"): + print(f"{item.identifier} - success: {item.identifier} no longer in {c}", file=sys.stderr) sys.exit(0) else: - print(f'{item.identifier} - error: {j.get("error")}', file=sys.stderr) + print(f"{item.identifier} - error: {j.get('error')}", file=sys.stderr) sys.exit(1) if not isinstance(src_md, list): - if key == 'subject': - src_md = src_md.split(';') - elif key == 'collection': - print(f'{item.identifier} - error: all collections would be removed, ' - 'not submitting task.', file=sys.stderr) + if key == "subject": + src_md = src_md.split(";") + elif key == "collection": + print(f"{item.identifier} - error: all collections would be removed, " + "not submitting task.", file=sys.stderr) sys.exit(1) if src_md == metadata[key]: - md[key] = 'REMOVE_TAG' + md[key] = "REMOVE_TAG" continue for x in src_md: @@ -200,12 +200,12 @@ def remove_metadata(item: item.Item, if len(md[key]) == len(src_md): del md[key] - if md.get('collection') == []: - print(f'{item.identifier} - error: all collections would be removed, not submitting task.', + if md.get("collection") == []: + print(f"{item.identifier} - error: all collections would be removed, not submitting task.", file=sys.stderr) sys.exit(1) elif not md: - print(f'{item.identifier} - warning: nothing needed to be removed.', file=sys.stderr) + print(f"{item.identifier} - warning: nothing needed to be removed.", file=sys.stderr) sys.exit(0) r = modify_metadata(item, md, args, parser) @@ -226,10 +226,10 @@ def main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: if args.exists: if item.exists: responses.append(True) - print(f'{identifier} exists', file=sys.stderr) + print(f"{identifier} exists", file=sys.stderr) else: responses.append(False) - print(f'{identifier} does not exist', file=sys.stderr) + print(f"{identifier} does not exist", file=sys.stderr) if (i + 1) == len(args.identifier): if all(r is True for r in responses): sys.exit(0) @@ -259,7 +259,7 @@ def main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: metadata = prepare_args_dict(args.remove, parser=parser, arg_type="remove") - if any('/' in k for k in metadata): + if any("/" in k for k in metadata): metadata = get_args_dict_many_write(metadata) if args.remove: @@ -276,7 +276,7 @@ def main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: continue # We still want to exit 0 if the non-200 is a # "no changes to xml" error. - elif 'no changes' in r.text: + elif "no changes" in r.text: continue else: sys.exit(1) @@ -286,7 +286,7 @@ def main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: for f in item.get_files(): formats.add(f.format) if (i + 1) == len(args.identifier): - print('\n'.join(formats)) + print("\n".join(formats)) # Dump JSON to stdout. else: @@ -297,15 +297,15 @@ def main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: if args.spreadsheet: if not args.priority: args.priority = -5 - with open(args.spreadsheet, newline='', encoding='utf-8') as csvfp: + with open(args.spreadsheet, newline="", encoding="utf-8") as csvfp: spreadsheet = csv.DictReader(csvfp) responses = [] for row in spreadsheet: - if not row['identifier']: + if not row["identifier"]: continue - item = args.session.get_item(row['identifier']) - if row.get('file'): - del row['file'] + item = args.session.get_item(row["identifier"]) + if row.get("file"): + del row["file"] metadata = {k.lower(): v for k, v in row.items() if v} responses.append(modify_metadata(item, metadata, args, parser)) @@ -318,7 +318,7 @@ def main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: continue # We still want to exit 0 if the non-200 is a # "no changes to xml" error. - elif 'no changes' in r.text: + elif "no changes" in r.text: continue else: sys.exit(1) diff --git a/internetarchive/cli/ia_move.py b/internetarchive/cli/ia_move.py index 7095446a..2a321385 100644 --- a/internetarchive/cli/ia_move.py +++ b/internetarchive/cli/ia_move.py @@ -38,22 +38,22 @@ def setup(subparsers): help="Move and rename files in archive.org items") # Positional arguments - parser.add_argument('source', - metavar='SOURCE', + parser.add_argument("source", + metavar="SOURCE", help="Source file formatted as: identifier/file") - parser.add_argument('destination', - metavar='DESTINATION', + parser.add_argument("destination", + metavar="DESTINATION", help="Destination file formatted as: identifier/file") # Options - parser.add_argument('-m', '--metadata', - metavar='KEY:VALUE', - action='append', + parser.add_argument("-m", "--metadata", + metavar="KEY:VALUE", + action="append", help=("Metadata to add to your new item, " "if you are moving the file to a new item")) - parser.add_argument('-H', '--header', - metavar='KEY:VALUE', - action='append', + parser.add_argument("-H", "--header", + metavar="KEY:VALUE", + action="append", help="S3 HTTP headers to send with your request") parser.add_argument("--replace-metadata", action="store_true", @@ -62,13 +62,13 @@ def setup(subparsers): parser.add_argument("--ignore-file-metadata", action="store_true", help="Do not copy file metadata") - parser.add_argument('-n', '--no-derive', - action='store_true', + parser.add_argument("-n", "--no-derive", + action="store_true", help="Do not derive uploaded files") - parser.add_argument('--no-backup', - action='store_true', + parser.add_argument("--no-backup", + action="store_true", help=("Turn off archive.org backups, " - 'clobbered files will not be saved to "history/files/$key.~N~"')) + "clobbered files will not be saved to 'history/files/$key.~N~'")) parser.set_defaults(func=lambda args: main(args, parser)) @@ -77,21 +77,21 @@ def main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: """ Main entry point for ia move command. """ - args.header = prepare_args_dict(args.header, parser=parser, arg_type='header') - args.metadata = prepare_args_dict(args.metadata, parser=parser, arg_type='metadata') + args.header = prepare_args_dict(args.header, parser=parser, arg_type="header") + args.metadata = prepare_args_dict(args.metadata, parser=parser, arg_type="metadata") # Add keep-old-version by default. - if not args.header.get('x-archive-keep-old-version') and not args.no_backup: - args.header['x-archive-keep-old-version'] = '1' + if not args.header.get("x-archive-keep-old-version") and not args.no_backup: + args.header["x-archive-keep-old-version"] = "1" # Call ia_copy. - _, src_file = ia_copy.main(args, cmd='move', parser=parser) + _, src_file = ia_copy.main(args, cmd="move", parser=parser) if src_file: dr = src_file.delete(headers=args.header, cascade_delete=True) else: - print(f'error: {src_file} does not exist', file=sys.stderr) + print(f"error: {src_file} does not exist", file=sys.stderr) sys.exit(1) if dr.status_code == 204: - print(f'success: moved {args.source} to {args.destination}', file=sys.stderr) + print(f"success: moved '{args.source}' to '{args.destination}'", file=sys.stderr) sys.exit(0) - print(f'error: {dr.content}', file=sys.stderr) + print(f"error: {dr.content}", file=sys.stderr) diff --git a/internetarchive/cli/ia_search.py b/internetarchive/cli/ia_search.py index aceaf542..c840cda4 100644 --- a/internetarchive/cli/ia_search.py +++ b/internetarchive/cli/ia_search.py @@ -119,9 +119,9 @@ def handle_search_results(args, search): for result in search: if args.itemlist: if args.fts or args.dsl_fts: - print('\n'.join(result.get('fields', {}).get('identifier'))) + print("\n".join(result.get("fields", {}).get("identifier"))) else: - print(result.get('identifier', '')) + print(result.get("identifier", "")) else: print(json.dumps(result)) if result.get("error"): diff --git a/internetarchive/cli/ia_tasks.py b/internetarchive/cli/ia_tasks.py index 64e6437c..a93da3fc 100644 --- a/internetarchive/cli/ia_tasks.py +++ b/internetarchive/cli/ia_tasks.py @@ -39,17 +39,17 @@ def setup(subparsers): help="Retrieve information about your archive.org catalog tasks") parser.add_argument("-t", "--task", - nargs='*', + nargs="*", help="Return information about the given task.") parser.add_argument("-G", "--get-task-log", help="Return the given tasks task log.") parser.add_argument("-p", "--parameter", nargs="+", metavar="KEY:VALUE", - action='append', + action="append", help="URL parameters passed to catalog.php.") parser.add_argument("-T", "--tab-output", - action='store_true', + action="store_true", help="Output task info in tab-delimited columns.") parser.add_argument("-c", "--cmd", type=str, @@ -60,22 +60,22 @@ def setup(subparsers): parser.add_argument("-a", "--task-args", nargs="+", metavar="KEY:VALUE", - action='append', + action="append", help="Args to submit to the Tasks API.") parser.add_argument("-d", "--data", nargs="+", metavar="KEY:VALUE", - action='append', + action="append", help="Additional data to send when submitting a task.") parser.add_argument("-r", "--reduced-priority", - action='store_true', + action="store_true", help="Submit task at a reduced priority.") parser.add_argument("-l", "--get-rate-limit", - action='store_true', + action="store_true", help="Get rate limit info.") parser.add_argument("identifier", type=str, - nargs='?', + nargs="?", help="Identifier for tasks specific operations.") parser.set_defaults(func=lambda args: main(args, parser)) @@ -85,14 +85,14 @@ def handle_task_submission_result(result, cmd): """ Handle the result of a task submission. """ - if result.get('success'): - task_log_url = result.get('value', {}).get('log') - print(f'success: {task_log_url}', file=sys.stderr) - elif 'already queued/running' in result.get('error', ''): - print(f'success: {cmd} task already queued/running', file=sys.stderr) + if result.get("success"): + task_log_url = result.get("value", {}).get("log") + print(f"success: {task_log_url}", file=sys.stderr) + elif "already queued/running" in result.get("error", ""): + print(f"success: {cmd} task already queued/running", file=sys.stderr) else: - print(f'error: {result.get("error")}', file=sys.stderr) - sys.exit(0 if result.get('success') else 1) + print(f"error: {result.get('error')}", file=sys.stderr) + sys.exit(0 if result.get("success") else 1) def main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: @@ -110,11 +110,11 @@ def main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: r = args.session.get_tasks_api_rate_limit(args.cmd) print(json.dumps(r)) sys.exit(0) - args.data['args'] = args.task_args + args.data["args"] = args.task_args r = args.session.submit_task(args.identifier, args.cmd, comment=args.comment, - priority=int(args.data.get('priority', 0)), + priority=int(args.data.get("priority", 0)), reduced_priority=args.reduced_priority, data=args.data) handle_task_submission_result(r.json(), args.cmd) @@ -122,49 +122,49 @@ def main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None: # Tasks read API. if args.identifier: - _params = {'identifier': args.identifier, 'catalog': 1, 'history': 1} + _params = {"identifier": args.identifier, "catalog": 1, "history": 1} _params.update(args.parameter) args.parameter = _params elif args.get_task_log: log = args.session.get_task_log(args.get_task_log, **args.parameter) - print(log.encode('utf-8', errors='surrogateescape') - .decode('utf-8', errors='replace')) + print(log.encode("utf-8", errors="surrogateescape") + .decode("utf-8", errors="replace")) sys.exit(0) queryable_params = [ - 'identifier', - 'task_id', - 'server', - 'cmd', - 'args', - 'submitter', - 'priority', - 'wait_admin', - 'submittime', + "identifier", + "task_id", + "server", + "cmd", + "args", + "submitter", + "priority", + "wait_admin", + "submittime", ] if not (args.identifier - or args.parameter.get('task_id')): - _params = {'catalog': 1, 'history': 0} + or args.parameter.get("task_id")): + _params = {"catalog": 1, "history": 0} _params.update(args.parameter) args.parameter = _params if not any(x in args.parameter for x in queryable_params): - _params = {'submitter': args.session.user_email, 'catalog': 1, 'history': 0, 'summary': 0} + _params = {"submitter": args.session.user_email, "catalog": 1, "history": 0, "summary": 0} _params.update(args.parameter) args.parameter = _params if args.tab_output: - warn_msg = ('tab-delimited output will be removed in a future release. ' - 'Please switch to the default JSON output.') + warn_msg = ("tab-delimited output will be removed in a future release. " + "Please switch to the default JSON output.") warnings.warn(warn_msg, stacklevel=2) for t in args.session.get_tasks(params=args.parameter): # Legacy support for tab-delimited output. # Mypy is confused by CatalogTask members being created from kwargs if args.tab_output: - color = t.color if t.color else 'done' - task_args = '\t'.join([f'{k}={v}' for k, v in t.args.items()]) # type: ignore - output = '\t'.join([str(x) for x in [ + color = t.color if t.color else "done" + task_args = "\t".join([f"{k}={v}" for k, v in t.args.items()]) # type: ignore + output = "\t".join([str(x) for x in [ t.identifier, t.task_id, t.server,