MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / depth_walk

Function depth_walk

src/commoncode/resource.py:150–186  ·  view source on GitHub ↗

Yield a (top, dirs, files) tuple at each step of walking the ``root_location`` directory recursively up to ``max_depth`` path segments extending from the ``root_location``. The behaviour is similar of ``os.walk``. - root_location: Absolute, normalized path for the directory to be w

(
    root_location,
    max_depth,
    skip_ignored=skip_ignored,
    error_handler=lambda: None,
)

Source from the content-addressed store, hash-verified

148
149
150def depth_walk(
151 root_location,
152 max_depth,
153 skip_ignored=skip_ignored,
154 error_handler=lambda: None,
155):
156 """
157 Yield a (top, dirs, files) tuple at each step of walking the ``root_location``
158 directory recursively up to ``max_depth`` path segments extending from the
159 ``root_location``. The behaviour is similar of ``os.walk``.
160
161 - root_location: Absolute, normalized path for the directory to be walked
162 - max_depth: positive integer for fixed depth limit. 0 for no limit.
163 - skip_ignored: Callback function that takes a location as argument and
164 returns a boolean indicating whether to ignore files in that location.
165 - error_handler: Error handler callback. No action taken by default.
166 """
167
168 if max_depth < 0:
169 raise Exception("ERROR: `max_depth` must be a positive integer or 0.")
170
171 # Find root directory depth using path separator's count
172 root_dir_depth = root_location.count(os.path.sep)
173
174 for top, dirs, files in os_walk(root_location, topdown=True, onerror=error_handler):
175 # If depth is limited (non-zero)
176 if max_depth:
177 current_depth = top.count(os.path.sep) - root_dir_depth
178
179 if skip_ignored(top) or (max_depth and current_depth >= max_depth):
180 # we clear out `dirs` and `files` to prevent `os_walk` from visiting
181 # the files and subdirectories of directories we are ignoring or
182 # are not in the specified nesting level
183 dirs[:] = []
184 files[:] = []
185 continue
186 yield top, dirs, files
187
188
189@attr.s(slots=True)

Calls 1

skip_ignoredFunction · 0.85