Skip to content

Commit

Permalink
Standardize string quotations to double quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
jake authored and jake committed May 2, 2024
1 parent 6ed3f8a commit 971e2c1
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 119 deletions.
24 changes: 12 additions & 12 deletions internetarchive/cli/ia_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand All @@ -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:
Expand All @@ -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,
Expand All @@ -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)
24 changes: 12 additions & 12 deletions internetarchive/cli/ia_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
66 changes: 33 additions & 33 deletions internetarchive/cli/ia_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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):
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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))

Expand All @@ -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)
46 changes: 23 additions & 23 deletions internetarchive/cli/ia_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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))

Expand All @@ -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)
4 changes: 2 additions & 2 deletions internetarchive/cli/ia_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
Loading

0 comments on commit 971e2c1

Please sign in to comment.