Gitlab Community Edition Instance

Commit 84c5b873 authored by mhellka's avatar mhellka
Browse files

Support multiple date formats for 'ls'

parent b52eff36
......@@ -6,6 +6,7 @@ from collections import defaultdict
import iso8601
from pycdstar3.cli import CliError
from pycdstar3.cli._utils import hbytes
......@@ -23,6 +24,13 @@ def register(subparsers):
" (default: name)",
)
parser.add_argument(
"--dates",
choices=["local", "iso", "epoch"],
default="local",
help="Change the way dates are displayed",
)
parser.add_argument(
"--order",
default="name",
......@@ -51,7 +59,7 @@ def register(subparsers):
parser.set_defaults(main=ls)
def ls(ctx, args):
def ls(ctx, args): # noqa: C901
client = ctx.client
vault = ctx.vault
archive = args.ARCHIVE
......@@ -66,8 +74,7 @@ def ls(ctx, args):
opts.setdefault("exclude_glob", []).append(args.exclude)
fmt = (
args.format
.replace("\\t", "\t")
args.format.replace("\\t", "\t")
.replace("\\0", "\0")
.replace("\\n", "\n")
.replace("\\\\", "\\")
......@@ -81,27 +88,37 @@ def ls(ctx, args):
# Load metadata only if requested
opts["meta"] = True
def datefunc(date):
dt = iso8601.parse_date(date)
if args.dates == "epoch":
return str(int(dt.timestamp()))
if args.dates == "local":
return dt.astimezone().replace(microsecond=0, tzinfo=None)
if args.dates == "iso":
return dt.astimezone().strftime("%Y-%m-%dT%H:%M:%S%z")
raise CliError("Unknown date format: " + args.dates)
with client.begin(readonly=True):
n = b = 0
for file in client.iter_files(vault, archive, **opts):
n += 1
b += file["size"]
print(file2str(fmt, file))
print(file2str(fmt, file, datefunc=datefunc))
ctx.print()
ctx.print("Total: {:,} files {:,} bytes", n, b)
def file2str(fmt, file):
def file2str(fmt, file, datefunc):
attrs = defaultdict(lambda: "-")
attrs.update(file)
attrs.update(file["digests"])
if "{hsize" in fmt:
attrs["hsize"] = hbytes(attrs["size"])
if "{created" in fmt:
attrs["created"] = iso8601.parse_date(attrs["created"]).astimezone().replace(
microsecond=0, tzinfo=None)
attrs["created"] = datefunc(attrs["created"])
if "{modified" in fmt:
attrs["modified"] = iso8601.parse_date(attrs["modified"]).astimezone().replace(microsecond=0, tzinfo=None)
attrs["modified"] = datefunc(attrs["modified"])
if "{meta" in fmt:
meta = defaultdict(lambda: "-")
meta.update(attrs["meta"])
......
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