@@ -50,7 +50,19 @@ def resolve_resource_path(path: str | Path) -> Path:
50
50
return resolve (path )
51
51
52
52
53
- def resolve (path : str | Path ) -> Path :
53
+ def create_path (path : Path ) -> None :
54
+ """
55
+ Create a file or directory at the given path.
56
+ If the path has a suffix, it's treated as a file, otherwise, as a directory.
57
+ """
58
+ if path .suffix :
59
+ path .parent .mkdir (parents = True , exist_ok = True )
60
+ path .touch (exist_ok = True )
61
+ else :
62
+ path .mkdir (parents = True , exist_ok = True )
63
+
64
+
65
+ def resolve (path : str | Path , * , create : bool = False ) -> Path :
54
66
"""
55
67
Attempts to resolve a path to a resource including resource handles.
56
68
@@ -67,6 +79,7 @@ def resolve(path: str | Path) -> Path:
67
79
68
80
Args:
69
81
path: A Path or string
82
+ create: If True, create the path if it doesn't exist.
70
83
"""
71
84
# Convert to a Path object and resolve resource handle
72
85
if isinstance (path , str ):
@@ -91,18 +104,24 @@ def resolve(path: str | Path) -> Path:
91
104
if path .exists ():
92
105
break
93
106
else :
94
- searched_paths = "\n " .join (f"-> { p } " for p in reversed (paths ))
95
- raise FileNotFoundError (
96
- f"Cannot locate resource '{ resource } ' using handle "
97
- f"'{ handle } ' in any of the following paths:\n "
98
- f"{ searched_paths } "
99
- )
107
+ if create :
108
+ create_path (paths [- 1 ] / resource )
109
+ else :
110
+ searched_paths = "\n " .join (f"-> { p } " for p in reversed (paths ))
111
+ raise FileNotFoundError (
112
+ f"Cannot locate resource '{ resource } ' using handle "
113
+ f"'{ handle } ' in any of the following paths:\n "
114
+ f"{ searched_paths } "
115
+ )
100
116
101
117
# Always convert into a Path object
102
118
path = Path (handle_path / resource )
103
119
else :
104
120
path = Path (path )
105
121
122
+ if create :
123
+ create_path (path )
124
+
106
125
try :
107
126
path = Path (path .resolve (strict = True ))
108
127
except AttributeError :
0 commit comments