Skip to content

Commit

Permalink
enhancement: allow passing pathlib.Path object[s] to .utils.norm_paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ssato committed May 15, 2018
1 parent b3a9f8e commit b7a751d
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions anyconfig/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import types

import anyconfig.compat
from anyconfig.compat import pathlib


def get_file_extension(file_path):
Expand Down Expand Up @@ -118,8 +119,7 @@ def is_path_obj(input_):
>>>
>>> assert not is_path_obj(__file__)
"""
return (anyconfig.compat.pathlib is not None and
isinstance(input_, anyconfig.compat.pathlib.Path))
return pathlib is not None and isinstance(input_, pathlib.Path)


def is_file_stream(input_):
Expand Down Expand Up @@ -246,16 +246,24 @@ def _norm_paths_itr(paths, marker='*'):
yield ppath
else:
yield path # a simple file path
elif is_path_obj(path):
if marker in path.as_posix():
for ppath in sglob(path.as_posix()):
yield normpath(ppath)
else:
yield normpath(path.as_posix())
else: # A file or file-like object
yield path


def norm_paths(paths, marker='*'):
"""
:param paths:
A glob path pattern string, or a list consists of path strings or glob
path pattern strings or file objects
A glob path pattern string or pathlib.Path object holding such path, or
a list consists of path strings or glob path pattern strings or
pathlib.Path object holding such ones, or file objects
:param marker: Glob marker character or string, e.g. '*'
:return: List of path strings
>>> norm_paths([])
Expand All @@ -273,6 +281,10 @@ def norm_paths(paths, marker='*'):
if is_path(paths) and marker in paths:
return sglob(paths)

if is_path_obj(paths) and marker in paths.as_posix():
# TBD: Is it better to return [p :: pathlib.Path] instead?
return [normpath(p) for p in sglob(paths.as_posix())]

return list(_norm_paths_itr(paths, marker=marker))


Expand Down

0 comments on commit b7a751d

Please sign in to comment.