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