From 9804a44c502ce2e364c480416720fbb065972973 Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Wed, 2 Jun 2021 11:19:45 -0400 Subject: [PATCH] Port to Python 3. --- src/allmydata/scripts/tahoe_ls.py | 45 +++++++++++++++++++------------ src/allmydata/util/_python3.py | 1 + 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/allmydata/scripts/tahoe_ls.py b/src/allmydata/scripts/tahoe_ls.py index b3e09b699..1ddfa555a 100644 --- a/src/allmydata/scripts/tahoe_ls.py +++ b/src/allmydata/scripts/tahoe_ls.py @@ -1,6 +1,15 @@ +""" +Ported to Python 3. +""" +from __future__ import unicode_literals +from __future__ import absolute_import +from __future__ import division from __future__ import print_function -from past.builtins import unicode +from future.utils import PY2 +if PY2: + from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401 + from six import ensure_text, ensure_str import time @@ -28,7 +37,7 @@ def list(options): e.display(stderr) return 1 - path = unicode(path, "utf-8") + path = str(path, "utf-8") url = nodeurl + "uri/%s" % url_quote(rootcap) if path: # move where.endswith check here? @@ -50,7 +59,7 @@ def list(options): if options['json']: # The webapi server should always output printable ASCII. if is_printable_ascii(data): - data = unicode(data, "ascii") + data = str(data, "ascii") print(data, file=stdout) return 0 else: @@ -87,7 +96,7 @@ def list(options): for name in childnames: child = children[name] - name = unicode(name) + name = str(name) childtype = child[0] # See webapi.txt for a discussion of the meanings of unix local @@ -147,23 +156,18 @@ def list(options): if not options["classify"]: classify = "" - encoding_error = False - try: - line.append(unicode_to_output(name) + classify) - except UnicodeEncodeError: - encoding_error = True - line.append(quote_output(name) + classify) + line.append(name + classify) if options["uri"]: - line.append(ensure_str(uri)) + line.append(ensure_text(uri)) if options["readonly-uri"]: - line.append(quote_output(ensure_str(ro_uri) or "-", quotemarks=False)) + line.append(quote_output(ensure_text(ro_uri) or "-", quotemarks=False)) - rows.append((encoding_error, line)) + rows.append(line) max_widths = [] left_justifys = [] - for (encoding_error, row) in rows: + for row in rows: for i,cell in enumerate(row): while len(max_widths) <= i: max_widths.append(0) @@ -185,12 +189,19 @@ def list(options): fmt = " ".join(fmt_pieces) rc = 0 - for (encoding_error, row) in rows: + for row in rows: + row = (fmt % tuple(row)).rstrip() + encoding_error = False + try: + row = unicode_to_output(row) + except UnicodeEncodeError: + encoding_error = True + row = quote_output(row) if encoding_error: - print((fmt % tuple(row)).rstrip(), file=stderr) + print(row, file=stderr) rc = 1 else: - print((fmt % tuple(row)).rstrip(), file=stdout) + print(row, file=stdout) if rc == 1: print("\nThis listing included files whose names could not be converted to the terminal" \ diff --git a/src/allmydata/util/_python3.py b/src/allmydata/util/_python3.py index 42a050b0a..936879271 100644 --- a/src/allmydata/util/_python3.py +++ b/src/allmydata/util/_python3.py @@ -108,6 +108,7 @@ PORTED_MODULES = [ "allmydata.scripts.tahoe_cp", "allmydata.scripts.tahoe_get", "allmydata.scripts.tahoe_invite", + "allmydata.scripts.tahoe_ls", "allmydata.scripts.types_", "allmydata.stats", "allmydata.storage_client",