fpathlib 0.1.1.post0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
fpathlib/__init__.py ADDED
@@ -0,0 +1,11 @@
1
+
2
+ from pathlib import *
3
+
4
+ from fpathlib.path import Path, PathPattern, ExpandedPathPattern
5
+
6
+ __all__ = [
7
+ "Path",
8
+ "PathPattern",
9
+ "ExpandedPathPattern",
10
+ "expand_path_pattern",
11
+ ]
fpathlib/_version.py ADDED
@@ -0,0 +1,24 @@
1
+ # file generated by vcs-versioning
2
+ # don't change, don't track in version control
3
+ from __future__ import annotations
4
+
5
+ __all__ = [
6
+ "__version__",
7
+ "__version_tuple__",
8
+ "version",
9
+ "version_tuple",
10
+ "__commit_id__",
11
+ "commit_id",
12
+ ]
13
+
14
+ version: str
15
+ __version__: str
16
+ __version_tuple__: tuple[int | str, ...]
17
+ version_tuple: tuple[int | str, ...]
18
+ commit_id: str | None
19
+ __commit_id__: str | None
20
+
21
+ __version__ = version = '0.1.1.post0'
22
+ __version_tuple__ = version_tuple = (0, 1, 1, 'post0')
23
+
24
+ __commit_id__ = commit_id = None
fpathlib/path.py ADDED
@@ -0,0 +1,121 @@
1
+ from glob import glob
2
+ import parse
3
+ import pathlib
4
+ import re
5
+
6
+
7
+ class Path(pathlib.Path):
8
+ """
9
+ :obj:`Path` is simply :obj:`pathlib.Path` with metadata.
10
+
11
+ Parameters
12
+ ----------
13
+ *pathsegments
14
+ """
15
+
16
+ def __init__(self, *pathsegments, metadata=None):
17
+ super().__init__(*pathsegments)
18
+ self.metadata = metadata
19
+
20
+ def match_any(self, path_patterns):
21
+ """
22
+ Perform :meth:`pathlib.Path.match` on several `path_patterns`.
23
+
24
+ Returns
25
+ -------
26
+ :obj:`bool`
27
+ """
28
+
29
+ if isinstance(path_patterns, str):
30
+ path_patterns = [path_patterns]
31
+
32
+ for path_pattern in path_patterns:
33
+ if self.match(path_pattern):
34
+ return True
35
+
36
+ return False
37
+
38
+
39
+ class FPath:
40
+ """
41
+ :obj:`FPath` is an f-string version of :obj:`Path`. This does not behave like a typical Path, because typically there are several matches to the f-string Path. Therefore, the :meth:`.FPath.expand` method must be used to create an :obj:`ExpandedFPath` that lists all the possibilities.
42
+
43
+ Example:
44
+
45
+
46
+ Parameters
47
+ ----------
48
+ fpath : :obj:`str`
49
+
50
+ Returns
51
+ -------
52
+ :obj:`.FPath`
53
+ """
54
+
55
+ def __init__(self, fpath):
56
+ self.fpath = fpath
57
+
58
+ def expand(self, exclude_path_patterns=None, require_metadata=True):
59
+ """
60
+ Use an f-string to extract out a collection of paths, where the f-string variables are captured and stored along the path name.
61
+
62
+ Parameters
63
+ ----------
64
+ exclude_path_patterns : :obj:`str` or :obj:`Iterable`[:obj:`str`]
65
+ Exclude paths that match the supplied pattern. (Default: None).
66
+ require_metadata : :obj:`bool`
67
+ Require that all paths identified must have found metadata. (Default: True).
68
+
69
+ Returns
70
+ -------
71
+ :obj:`.ExpandedFPath`
72
+ """
73
+
74
+ parser = parse.compile(self.fpath)
75
+
76
+ paths = []
77
+ for fname in glob(re.sub(r"\{.*?\}", "*", self.fpath)):
78
+ path = Path(fname)
79
+ if exclude_path_patterns and path.match_any(exclude_path_patterns):
80
+ continue
81
+ path.metadata = getattr(parser.parse(fname), "named", None)
82
+ if require_metadata and path.metadata is None:
83
+ msg = f"metadata not found for '{fname}' with fstring '{fstring}'"
84
+ raise AttributeError(msg)
85
+ paths.append(path)
86
+
87
+ return ExpandedFPath(paths)
88
+
89
+
90
+ # TODO does scan_csv belong here?
91
+ class ExpandedFPath:
92
+ def __init__(self, paths, path_pattern):
93
+ if len(paths) != len(set(paths)):
94
+ raise AttributeError("paths are not unique")
95
+ self.paths = paths
96
+ self.path_pattern = path_pattern
97
+
98
+ def __getitem__(self, item):
99
+ return self.paths[item]
100
+
101
+ def __len__(self):
102
+ return len(self.paths)
103
+
104
+ @property
105
+ def metadata(self):
106
+ metadata = {path: path.metadata for path in self.paths}
107
+ return metadata
108
+
109
+ def to_polars(self, lazy=False):
110
+ data = [{"fname": str(key), **value} for key, value in self.metadata.items()]
111
+ df = pl.DataFrame(data)
112
+ if lazy:
113
+ df = df.lazy()
114
+ return df
115
+
116
+
117
+ def expand_fpath(fpath, exclude=None, require_metadata=True):
118
+ """
119
+ """
120
+
121
+ return FPath(fpath).expand(exclude=exclude, require_metadata=require_metadata)
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.4
2
+ Name: fpathlib
3
+ Version: 0.1.1.post0
4
+ Summary: A package to combine paths with metadata
5
+ Author-email: "C. Lockhart" <clockha2@gmu.edu>
6
+ Requires-Python: >=3.9
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: parse
10
+ Provides-Extra: dev
11
+ Requires-Dist: pytest; extra == "dev"
12
+ Requires-Dist: black; extra == "dev"
13
+ Requires-Dist: sphinx; extra == "dev"
14
+ Dynamic: license-file
15
+
16
+ fpathlib
17
+ ========
18
+
19
+ A Python package for adding metadata to file paths.
@@ -0,0 +1,8 @@
1
+ fpathlib/__init__.py,sha256=8hUfYyGLWDAhFsuM9xjKvCVohOur7sx3N9hOaq3f2yY,180
2
+ fpathlib/_version.py,sha256=sIqpOcrwP7UeMcz5uPF6Tnd5RgYb-DnIQQng-hqRGJI,535
3
+ fpathlib/path.py,sha256=eM0o78gk1kqFEV7YBjdrhuci98P3BGuMBOyVrBuEPBY,3377
4
+ fpathlib-0.1.1.post0.dist-info/licenses/LICENSE,sha256=76rZ9i4ZumZ1M4jlYM9ZDOTYEeyzuxdsTKjF2qha3Lc,1069
5
+ fpathlib-0.1.1.post0.dist-info/METADATA,sha256=XB1ZY8wnc7Uz7JnhxxWSBS8gsQZuZZ9G8pct9aLU4DE,488
6
+ fpathlib-0.1.1.post0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
7
+ fpathlib-0.1.1.post0.dist-info/top_level.txt,sha256=QT5pDgO4--AJn7X4RB51xpNTt0pD5_U2mCjsydpQAQw,9
8
+ fpathlib-0.1.1.post0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Lockhart Lab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ fpathlib