Skip to content

Commit

Permalink
Extract _walk_separator from _walk
Browse files Browse the repository at this point in the history
  • Loading branch information
c0llab0rat0r authored and ntninja committed Apr 14, 2021
1 parent 7b6c490 commit b3a7a3d
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions ipfshttpclient/filescanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,25 @@ def _join_dirs_and_files(dirnames: ty.List[AnyStr], filenames: ty.List[AnyStr])
for filename in filenames:
yield filename, False

@staticmethod
def _walk_separator(
matcher: Matcher[AnyStr],
directory_str: ty.Optional[AnyStr]
) -> ty.Union[bytes, str]:
"""
Determine which separator to use.
Because os.fsencode can return a byte array, we must allow returning a byte array,
regardless of AnyType.
"""

if directory_str is not None:
return utils.maybe_fsencode(os.path.sep, directory_str)
elif matcher is not None and matcher.is_binary:
return os.fsencode(os.path.sep)
else:
return os.path.sep

@staticmethod
def _walk_wide(
dot: AnyStr,
Expand All @@ -655,12 +674,11 @@ def _walk(
follow_symlinks: bool,
intermediate_dirs: bool
) -> ty.Generator[FSNodeEntry, ty.Any, None]:
if directory_str is not None:
sep = utils.maybe_fsencode(os.path.sep, directory_str)
elif matcher is not None and matcher.is_binary:
sep = os.fsencode(os.path.sep) # type: ignore[assignment]
else:
sep = os.path.sep # type: ignore[assignment]
separator = self._walk_separator(matcher=matcher, directory_str=directory_str)

# TODO: Because os.fsencode can return a byte array, we need to refactor how we use 'sep'
sep: AnyStr = separator # type: ignore[assignment]

dot = utils.maybe_fsencode(".", sep)

# Identify the leading portion of the `dirpath` returned by `os.walk`
Expand Down

0 comments on commit b3a7a3d

Please sign in to comment.