Skip to content

Commit

Permalink
refactor kit path/glob utils
Browse files Browse the repository at this point in the history
try to use pathlib over os
  • Loading branch information
ddkohler committed Feb 19, 2024
1 parent a097b42 commit b8e93a9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 53 deletions.
28 changes: 26 additions & 2 deletions WrightTools/kit/_glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .._open import open


__all__ = ["describe_wt5", "filter_wt5s", "glob_wt5s", "search_for_attrs",]
__all__ = ["describe_wt5", "filter_wt5s", "glob_handler", "glob_wt5s", "search_for_attrs",]


def describe_wt5(path: Union[str, PathLike]) -> dict:
Expand Down Expand Up @@ -37,6 +37,30 @@ def glob_wt5s(directory: Union[str, PathLike], recursive=True) -> Iterator:
return pathlib.Path(directory).glob(pattern)


def glob_handler(extension, folder=None, identifier=None, recursive=True) -> List[pathlib.Path]:
"""Return a list of all files matching specified inputs.
Parameters
----------
extension : string
File extension.
folder : string (optional)
Folder to search within. Default is None (current working
directory).
identifier : string
Unique identifier. Default is None.
recursive : bool
When true, searches folder and all subfolders for identifier
Returns
-------
list of pathlib.Path objects
path objects for matching files.
"""
pattern = f"**/*.{extension}" if recursive else f"*.{extension}"
return [x for x in filter(lambda x: identifier in str(x), pathlib.Path(folder).glob(pattern))]


def search_for_attrs(
directory: Union[str, PathLike],
recursive=True,
Expand All @@ -63,7 +87,7 @@ def search_for_attrs(
Example
-------
To find a scan based on scan parameters in the bluesky-cmds:
To find a scan from scan parameters in the bluesky-cmds:
>>> search_wt5_by_attr(os.environ["WT5_DATA_DIR"], name="primary", shape=[136,101], )
"""
return filter_wt5s(glob_wt5s(directory, recursive), **kwargs)
Expand Down
65 changes: 14 additions & 51 deletions WrightTools/kit/_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,41 @@
# --- import --------------------------------------------------------------------------------------


import os
import glob
import pathlib


# --- define --------------------------------------------------------------------------------------


__all__ = ["get_path_matching", "glob_handler"]
__all__ = ["get_path_matching"]


# --- functions -----------------------------------------------------------------------------------


def get_path_matching(name):
"""Get path matching a name.
def get_path_matching(name:str) -> pathlib.Path:
"""
Non-recursive search for path to the folder "name".
Searches the user directory, then looks up the cwd for a parent folder that matches.
Parameters
----------
name : string
Name to search for.
name of directory to search for.
Returns
-------
string
Full filepath.
pathlib.Path
Full filepath to directory name.
"""
# first try looking in the user folder
p = os.path.join(os.path.expanduser("~"), name)
p = pathlib.Path.home() / name
# then try expanding upwards from cwd
if not os.path.isdir(p):
if not p.is_dir():
p = None
drive, folders = os.path.splitdrive(os.getcwd())
folders = folders.split(os.sep)
folders.insert(0, os.sep)
drive, *folders = pathlib.Path.cwd().parts
if name in folders:
p = os.path.join(drive, *folders[: folders.index(name) + 1])
p = pathlib.Path(drive).joinpath(*folders[:folders.index(name)+1])
# TODO: something more robust to catch the rest of the cases?
return p


def glob_handler(extension, folder=None, identifier=None):
"""Return a list of all files matching specified inputs.
Parameters
----------
extension : string
File extension.
folder : string (optional)
Folder to search within. Default is None (current working
directory).
identifier : string
Unique identifier. Default is None.
Returns
-------
list of strings
Full path of matching files.
"""
filepaths = []
if folder:
# comment out [ and ]...
folder = folder.replace("[", "?")
folder = folder.replace("]", "*")
folder = folder.replace("?", "[[]")
folder = folder.replace("*", "[]]")
glob_str = os.path.join(folder, "*" + extension)
else:
glob_str = "*" + extension + "*"
for filepath in glob.glob(glob_str):
if identifier:
if identifier in filepath:
filepaths.append(filepath)
else:
filepaths.append(filepath)
return filepaths

0 comments on commit b8e93a9

Please sign in to comment.