@@ -50,7 +50,19 @@ def resolve_resource_path(path: str | Path) -> Path:
5050 return resolve (path )
5151
5252
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 :
5466 """
5567 Attempts to resolve a path to a resource including resource handles.
5668
@@ -67,6 +79,7 @@ def resolve(path: str | Path) -> Path:
6779
6880 Args:
6981 path: A Path or string
82+ create: If True, create the path if it doesn't exist.
7083 """
7184 # Convert to a Path object and resolve resource handle
7285 if isinstance (path , str ):
@@ -91,18 +104,24 @@ def resolve(path: str | Path) -> Path:
91104 if path .exists ():
92105 break
93106 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+ )
100116
101117 # Always convert into a Path object
102118 path = Path (handle_path / resource )
103119 else :
104120 path = Path (path )
105121
122+ if create :
123+ create_path (path )
124+
106125 try :
107126 path = Path (path .resolve (strict = True ))
108127 except AttributeError :
0 commit comments