Skip to content

Filesharing Service Walk Task¤

task api name: walk

The walk task recursively lists all files from all subdirectories under the specified URL path. This is useful when you need to discover all files available in a directory tree, returning a list of complete nf://... URLs for each file found. The task skips hidden files (starting with .) and special directories (containing __).

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="walk",
        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
root
└── file:    File sharing service
    └── walk:    Walk directory tree recursively

nf#

API Reference¤

Recursively list all files from all subdirectories.

Parameters:

Name Type Description Default
url str

URL path starting with 'nf://' to walk directories

required

Returns:

Type Description
Result

Result containing list of all file paths or error message

Source code in norfab\workers\filesharing_worker\filesharing_worker.py
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
@Task(fastapi={"methods": ["GET"]})
def walk(self, url: str) -> Result:
    """
    Recursively list all files from all subdirectories.

    Args:
        url: URL path starting with 'nf://' to walk directories

    Returns:
        Result containing list of all file paths 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):
        files_list = []
        for root, dirs, files in os.walk(full_path):
            # skip path containing folders like __folders__
            if root.count("__") >= 2:
                continue
            root = root.replace(self.base_dir, "")
            root = root.lstrip("\\")
            root = root.replace("\\", "/")
            for file in files:
                # skip hidden/system files
                if file.startswith("."):
                    continue
                if root:
                    files_list.append(f"nf://{root}/{file}")
                else:
                    files_list.append(f"nf://{file}")
        ret.result = files_list
    else:
        ret.failed = True
        ret.errors = ["Directory Not Found"]
    return ret