Commit 3ba417e1 authored by Julien Chaumond's avatar Julien Chaumond
Browse files

[cli] ls: Tabular formatting

parent 96fa9a8a
...@@ -25,6 +25,17 @@ class UserCommands(BaseTransformersCLICommand): ...@@ -25,6 +25,17 @@ class UserCommands(BaseTransformersCLICommand):
class ANSI:
"""
Helper for en.wikipedia.org/wiki/ANSI_escape_code
"""
_bold = u"\u001b[1m"
_reset = u"\u001b[0m"
@classmethod
def bold(cls, s):
return "{}{}{}".format(cls._bold, s, cls._reset)
class BaseUserCommand: class BaseUserCommand:
def __init__(self, args): def __init__(self, args):
self.args = args self.args = args
...@@ -80,6 +91,28 @@ class LogoutCommand(BaseUserCommand): ...@@ -80,6 +91,28 @@ class LogoutCommand(BaseUserCommand):
class ListObjsCommand(BaseUserCommand): class ListObjsCommand(BaseUserCommand):
def tabulate(self, rows, headers):
# type: (List[List[Union[str, int]]], List[str]) -> str
"""
Inspired by:
stackoverflow.com/a/8356620/593036
stackoverflow.com/questions/9535954/printing-lists-as-tabular-data
"""
col_widths = [max(len(str(x)) for x in col) for col in zip(*rows, headers)]
row_format = ("{{:{}}} " * len(headers)).format(*col_widths)
lines = []
lines.append(
row_format.format(*headers)
)
lines.append(
row_format.format(*["-" * w for w in col_widths])
)
for row in rows:
lines.append(
row_format.format(*row)
)
return "\n".join(lines)
def run(self): def run(self):
token = HfFolder.get_token() token = HfFolder.get_token()
if token is None: if token is None:
...@@ -92,12 +125,15 @@ class ListObjsCommand(BaseUserCommand): ...@@ -92,12 +125,15 @@ class ListObjsCommand(BaseUserCommand):
exit(1) exit(1)
if len(objs) == 0: if len(objs) == 0:
print("No shared file yet") print("No shared file yet")
for obj in objs: exit()
print( rows = [ [
obj.filename, obj.filename,
obj.LastModified, obj.LastModified,
obj.ETag, obj.ETag,
obj.Size obj.Size
] for obj in objs ]
print(
self.tabulate(rows, headers=["Filename", "LastModified", "ETag", "Size"])
) )
...@@ -109,12 +145,19 @@ class UploadCommand(BaseUserCommand): ...@@ -109,12 +145,19 @@ class UploadCommand(BaseUserCommand):
exit(1) exit(1)
filepath = os.path.join(os.getcwd(), self.args.file) filepath = os.path.join(os.getcwd(), self.args.file)
filename = self.args.filename if self.args.filename is not None else os.path.basename(filepath) filename = self.args.filename if self.args.filename is not None else os.path.basename(filepath)
print("About to upload file {} to S3 under filename {}".format(filepath, filename)) print(
"About to upload file {} to S3 under filename {}".format(
ANSI.bold(filepath), ANSI.bold(filename)
)
)
choice = input("Proceed? [Y/n] ").lower() choice = input("Proceed? [Y/n] ").lower()
if not(choice == "" or choice == "y" or choice == "yes"): if not(choice == "" or choice == "y" or choice == "yes"):
print("Abort") print("Abort")
exit() exit()
print("Uploading...") print(
ANSI.bold("Uploading... This might take a while if file is large")
)
access_url = self._api.presign_and_upload( access_url = self._api.presign_and_upload(
token=token, filename=filename, filepath=filepath token=token, filename=filename, filepath=filepath
) )
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment