Skip to content

Filesharing Service List FIles Task¤

task api name: list_files

The list_files task lists the contents of a directory at the specified URL path in a non-recursive manner. This task returns only the immediate files and subdirectories within the given path, without descending into nested directories. It's useful for browsing the structure of your file sharing repository one level at a time.

Using it from Python¤

from norfab.core.nfapi import NorFab

with NorFab(inventory="./inventory.yaml") as nf:
    client = nf.make_client()

    reply = client.run_job(
        service="filesharing",
        task="list_files",
        workers="any",
        kwargs={"url": "nf://"},
    )
    print(reply)

Using it from nfcli¤

NORFAB CLI exposes File Sharing under the file command group.

nf#man tree file.list
root
└── file:    File sharing service
    └── list:    List files
        └── url:    Directory to list content for, default 'nf://'

nf#

API Reference¤

List files in a directory.

Parameters:

Name Type Description Default
url str

URL path starting with 'nf://' to list files from

required

Returns:

Type Description
Result

Result containing list of files or error message

Source code in norfab\workers\filesharing_worker\filesharing_worker.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
@Task(fastapi={"methods": ["GET"]})
def list_files(self, url: str) -> Result:
    """
    List files in a directory.

    Args:
        url: URL path starting with 'nf://' to list files from

    Returns:
        Result containing list of files or error message
    """
    ret = Result(result=None)
    try:
        full_path = self._safe_path(url)
    except ValueError as e:
        ret.failed = True
        ret.errors = [str(e)]
        return ret

    if os.path.exists(full_path) and os.path.isdir(full_path):
        ret.result = os.listdir(full_path)
    else:
        ret.errors = ["Directory Not Found"]
        ret.failed = True
    return ret