Skip to content

fss-cli API Reference

The public surface is small and deliberately so: one class, its policy types, and the context object extensions read.

fss_cli

Library-only POSIX-shaped commands for async fsspec filesystems.

App

Embedded core commands plus explicitly selected extensions.

Source code in libs/fss-cli/src/fss_cli/_app.py
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
class App:
    """Embedded core commands plus explicitly selected extensions."""

    typer_app: typer.Typer

    def __init__(
        self,
        sources: Mapping[str, AsyncFilesystemSource],
        *,
        capabilities: AppCapabilities | None = None,
        extensions: Sequence[CommandCallback] = (),
    ) -> None:
        """Snapshot sources and register the requested command surface."""
        self._sources = MappingProxyType(dict(sources))
        if not self._sources:
            msg = "at least one async filesystem source is required"
            raise ValueError(msg)
        for name in self._sources:
            _validate_source_name(name)
        self._capabilities = _snapshot_capabilities(capabilities)

        self.typer_app = typer.Typer(add_completion=False)
        self._register_commands()
        for extension in extensions:
            self.typer_app.command()(extension)

    # Operand preflight shared by the command callbacks.

    def _mapped(self, command: str, spelling: str) -> _MappedOperand:
        """Parse one ``name:/path`` operand against the configured sources."""
        return _parse_mapped_operand(command, spelling, self._sources)

    def _mapped_all(
        self,
        command: str,
        spellings: Sequence[str],
    ) -> tuple[_MappedOperand, ...]:
        """Parse every ``name:/path`` operand in invocation order."""
        return tuple(self._mapped(command, spelling) for spelling in spellings)

    def _destructive_operand(self, command: str, spelling: str) -> _MappedOperand:
        """Parse one operand and reject roots and final dot segments."""
        operand = self._mapped(command, spelling)
        if _is_root(operand.path) or _has_final_dot_segment(operand.path):
            rendered = _render_diagnostic_value(spelling)
            _usage_error(command, f"{rendered}: rejected path")
        return operand

    # Command registration.

    def _register_commands(self) -> None:
        @self.typer_app.callback()
        def root(ctx: typer.Context) -> None:
            ctx.obj = CommandContext(self._sources)

        self._register_query_commands()
        self._register_listing_commands()
        self._register_read_commands()
        self._register_transfer_commands()
        self._register_mutation_commands()

    def _register_query_commands(self) -> None:
        """Register the metadata and predicate commands."""

        @self.typer_app.command()
        def info(operand: _Operand) -> None:
            """Display normalized file information."""
            mapped = self._mapped("info", operand)
            _run_async_command(
                "info",
                lambda: _run_info("info", mapped, self._sources),
            )

        @self.typer_app.command()
        def size(operands: _Operands) -> None:
            """Display exact file sizes."""
            mapped = self._mapped_all("size", operands)
            _run_async_command(
                "size",
                lambda: _run_size("size", mapped, self._sources),
            )

        @self.typer_app.command()
        def stat(operands: _Operands) -> None:
            """Display file status."""
            mapped = self._mapped_all("stat", operands)
            _run_async_command(
                "stat",
                lambda: _run_stat("stat", mapped, self._sources),
            )

        @self.typer_app.command()
        def test(
            operand: _Operand,
            *,
            exists: _TestExists = False,
            directory: _TestDirectory = False,
            file: _TestFile = False,
        ) -> None:
            """Evaluate a file predicate."""
            mapped = self._mapped("test", operand)
            selected: list[Literal["e", "d", "f"]] = [
                predicate
                for predicate, enabled in (
                    ("e", exists),
                    ("d", directory),
                    ("f", file),
                )
                if enabled
            ]
            if len(selected) != 1:
                _usage_error("test", "exactly one predicate selector is required")
            _run_async_command(
                "test",
                lambda: _run_test("test", selected[0], mapped, self._sources),
            )

    def _register_listing_commands(self) -> None:
        """Register the directory listing and traversal commands."""

        @self.typer_app.command()
        def ls(
            operands: _Operands,
            *,
            include_almost_all: _AlmostAll = False,
            long_listing: _LongListing = False,
            human_readable: _HumanSizes = False,
        ) -> None:
            """List directory contents."""
            self._listing(
                operands,
                include_almost_all=include_almost_all,
                long_listing=long_listing,
                human_readable=human_readable,
            )

        @self.typer_app.command()
        def du(
            operand: _Operand,
            *,
            summarize: _Summarize = False,
            human_readable: _HumanSizes = False,
        ) -> None:
            """Estimate file space usage."""
            mapped = self._mapped("du", operand)
            _run_async_command(
                "du",
                lambda: _run_du(
                    "du",
                    _DuRequest(
                        summarize=summarize,
                        human_readable=human_readable,
                        operand=mapped,
                    ),
                    self._sources,
                ),
            )

        @self.typer_app.command()
        def find(
            operand: _Operand,
            maxdepth: _MaxDepth = None,
            kind: _FindType = "f",
        ) -> None:
            """Find files recursively."""
            mapped = self._mapped("find", operand)
            _run_async_command(
                "find",
                lambda: _run_find(
                    "find",
                    _FindRequest(maxdepth=maxdepth, kind=kind, operand=mapped),
                    self._sources,
                ),
            )

        @self.typer_app.command()
        def tree(operand: _Operand, maxdepth: _MaxDepth = None) -> None:
            """Display a recursive directory tree."""
            mapped = self._mapped("tree", operand)
            _run_async_command(
                "tree",
                lambda: _run_tree(
                    "tree",
                    _TreeRequest(maxdepth=maxdepth, operand=mapped),
                    self._sources,
                ),
            )

    def _listing(
        self,
        operands: Sequence[str],
        *,
        include_almost_all: bool,
        long_listing: bool,
        human_readable: bool,
    ) -> None:
        """Preflight and run one ``ls`` invocation."""
        mapped = self._mapped_all("ls", operands)
        if human_readable and not long_listing:
            _usage_error("ls", "-h: requires long listing")
        _run_async_command(
            "ls",
            lambda: _run_ls(
                "ls",
                _LsRequest(
                    include_almost_all=include_almost_all,
                    long_listing=long_listing,
                    human_readable=human_readable,
                    operands=mapped,
                ),
                self._sources,
            ),
        )

    def _register_read_commands(self) -> None:
        """Register the byte-reading commands."""

        @self.typer_app.command()
        def head(operand: _Operand, count: _ByteCount) -> None:
            """Display leading bytes."""
            mapped = self._mapped("head", operand)
            _run_async_command(
                "head",
                lambda: _run_byte_range(
                    "head",
                    count,
                    mapped,
                    self._sources,
                    _read_head,
                ),
            )

        @self.typer_app.command()
        def tail(operand: _Operand, count: _ByteCount) -> None:
            """Display trailing bytes."""
            mapped = self._mapped("tail", operand)
            _run_async_command(
                "tail",
                lambda: _run_byte_range(
                    "tail",
                    count,
                    mapped,
                    self._sources,
                    _read_tail,
                ),
            )

        @self.typer_app.command()
        def cat(operands: _CatOperands = None) -> None:
            """Concatenate files to standard output."""
            parsed = tuple(
                _StdinOperand() if operand == "-" else _parse_mapped_operand("cat", operand, self._sources)
                for operand in operands or ("-",)
            )
            _run_async_command(
                "cat",
                lambda: _run_cat("cat", parsed, self._sources),
            )

    def _register_transfer_commands(self) -> None:
        """Register ``cp`` in its capability-selected form, plus ``mv``."""
        if self._capabilities.recursive_copy:

            @self.typer_app.command()
            def cp(
                operands: _CpOperands,
                *,
                recursive: _CopyRecursive = False,
                metadata_wait: _MetadataWait = _DEFAULT_METADATA_WAIT_SECONDS,
            ) -> None:
                """Copy files or one directory."""
                self._copy(operands, recursive=recursive, metadata_wait=metadata_wait)

        else:

            @self.typer_app.command()
            def cp(
                operands: _CpOperands,
                *,
                metadata_wait: _MetadataWait = _DEFAULT_METADATA_WAIT_SECONDS,
            ) -> None:
                """Copy one or more files."""
                self._copy(operands, recursive=False, metadata_wait=metadata_wait)

        @self.typer_app.command()
        def mv(operands: _MvOperands) -> None:
            """Move or rename files on one mapped filesystem."""
            if len(operands) < 2:  # noqa: PLR2004 - command arity.
                message = "requires at least one source and one destination"
                raise typer.BadParameter(message, param_hint="name:/path")
            plan = _plan_mv("mv", self._mapped_all("mv", operands))
            _run_async_command(
                "mv",
                lambda: _run_mv("mv", plan, self._sources),
            )

    def _copy(self, operands: Sequence[str], *, recursive: bool, metadata_wait: int) -> None:
        """Preflight and run one shared ``cp`` invocation."""
        plan = _cp_plan(
            "cp",
            tuple(operands),
            self._sources,
            recursive=recursive,
            metadata_wait=metadata_wait,
        )
        _run_async_command("cp", lambda: _run_cp("cp", plan, self._sources))

    def _register_mutation_commands(self) -> None:
        """Register the namespace creation and removal commands."""

        @self.typer_app.command()
        def mkdir(operands: _Operands, *, parents: _CreateParents = False) -> None:
            """Create directories."""
            mapped = self._mapped_all("mkdir", operands)
            _run_async_command(
                "mkdir",
                lambda: _run_mkdir(
                    "mkdir",
                    _MkdirRequest(create_parents=parents, operands=mapped),
                    self._sources,
                ),
            )

        @self.typer_app.command()
        def rmdir(operands: _Operands) -> None:
            """Remove empty directories."""
            mapped = tuple(self._destructive_operand("rmdir", operand) for operand in operands)
            _run_async_command(
                "rmdir",
                lambda: _run_rmdir("rmdir", mapped, self._sources),
            )

        @self.typer_app.command()
        def unlink(operand: _Operand) -> None:
            """Remove a single file."""
            mapped = self._destructive_operand("unlink", operand)
            _run_async_command(
                "unlink",
                lambda: _run_unlink("unlink", mapped, self._sources),
            )

        if self._capabilities.recursive_remove:

            @self.typer_app.command(name="rm")
            def recursive_rm(
                operands: _OptionalOperands = None,
                *,
                directory: _RmDirectory = False,
                force: _RmForce = False,
                verbose: _RmVerbose = 0,
                recursive: _RmRecursive = False,
            ) -> None:
                """Remove files or directories with guarded -R or -r."""
                self._remove(
                    operands,
                    directory=directory,
                    force=force,
                    verbose=verbose,
                    recursive=recursive,
                )

        else:

            @self.typer_app.command(name="rm")
            def rm(
                operands: _OptionalOperands = None,
                *,
                directory: _RmDirectory = False,
                force: _RmForce = False,
                verbose: _RmVerbose = 0,
            ) -> None:
                """Remove files; -d removes empty directories."""
                self._remove(
                    operands,
                    directory=directory,
                    force=force,
                    verbose=verbose,
                )

    def _remove(
        self,
        operands: Sequence[str] | None,
        *,
        directory: bool,
        force: bool,
        verbose: int,
        recursive: bool = False,
    ) -> None:
        """Preflight option combinations and paths, then run one ``rm``."""
        if verbose > 1:
            _usage_error("rm", "-v: may be supplied once")
        if directory and (force or verbose or recursive):
            _usage_error("rm", "-d: cannot combine with other options")
        if not recursive and force and verbose:
            _usage_error("rm", "-f: cannot combine with -v")
        spellings = list(operands or [])
        if not spellings and not force:
            _usage_error("rm", "missing mapped filesystem operand")
        mapped = self._mapped_all("rm", spellings)
        for spelling, operand in zip(spellings, mapped, strict=True):
            rejected = _is_root(operand.path) or (
                _has_dot_segment(operand.path) if recursive else _has_final_dot_segment(operand.path)
            )
            if rejected:
                rendered = _render_diagnostic_value(spelling)
                _usage_error("rm", f"{rendered}: rejected path")
        _run_async_command(
            "rm",
            lambda: _run_rm(
                "rm",
                _RmRequest(
                    force=force,
                    directory=directory,
                    recursive=recursive,
                    verbose=bool(verbose),
                    operands=mapped,
                ),
                self._sources,
            ),
        )

AppCapabilities

Bases: TypedDict

Application-level core command policy.

Source code in libs/fss-cli/src/fss_cli/_app.py
class AppCapabilities(TypedDict, total=False):
    """Application-level core command policy."""

    recursion: RecursionCapabilities

CommandContext dataclass

Immutable application context available to command callbacks.

Source code in libs/fss-cli/src/fss_cli/_app.py
@dataclass(frozen=True)
class CommandContext:
    """Immutable application context available to command callbacks."""

    sources: Mapping[str, AsyncFilesystemSource]

RecursionCapabilities

Bases: TypedDict

Application policy for recursive core commands.

Source code in libs/fss-cli/src/fss_cli/_app.py
class RecursionCapabilities(TypedDict, total=False):
    """Application policy for recursive core commands."""

    copy: bool
    remove: bool