Skip to content

Commit b3a7a3d

Browse files
c0llab0rat0rntninja
authored andcommitted
Extract _walk_separator from _walk
1 parent 7b6c490 commit b3a7a3d

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

ipfshttpclient/filescanner.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,25 @@ def _join_dirs_and_files(dirnames: ty.List[AnyStr], filenames: ty.List[AnyStr])
629629
for filename in filenames:
630630
yield filename, False
631631

632+
@staticmethod
633+
def _walk_separator(
634+
matcher: Matcher[AnyStr],
635+
directory_str: ty.Optional[AnyStr]
636+
) -> ty.Union[bytes, str]:
637+
"""
638+
Determine which separator to use.
639+
640+
Because os.fsencode can return a byte array, we must allow returning a byte array,
641+
regardless of AnyType.
642+
"""
643+
644+
if directory_str is not None:
645+
return utils.maybe_fsencode(os.path.sep, directory_str)
646+
elif matcher is not None and matcher.is_binary:
647+
return os.fsencode(os.path.sep)
648+
else:
649+
return os.path.sep
650+
632651
@staticmethod
633652
def _walk_wide(
634653
dot: AnyStr,
@@ -655,12 +674,11 @@ def _walk(
655674
follow_symlinks: bool,
656675
intermediate_dirs: bool
657676
) -> ty.Generator[FSNodeEntry, ty.Any, None]:
658-
if directory_str is not None:
659-
sep = utils.maybe_fsencode(os.path.sep, directory_str)
660-
elif matcher is not None and matcher.is_binary:
661-
sep = os.fsencode(os.path.sep) # type: ignore[assignment]
662-
else:
663-
sep = os.path.sep # type: ignore[assignment]
677+
separator = self._walk_separator(matcher=matcher, directory_str=directory_str)
678+
679+
# TODO: Because os.fsencode can return a byte array, we need to refactor how we use 'sep'
680+
sep: AnyStr = separator # type: ignore[assignment]
681+
664682
dot = utils.maybe_fsencode(".", sep)
665683

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

0 commit comments

Comments
 (0)