File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ 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 resolve (path : str | Path , * , create : bool = False ) -> Path :
54
54
"""
55
55
Attempts to resolve a path to a resource including resource handles.
56
56
@@ -67,6 +67,7 @@ def resolve(path: str | Path) -> Path:
67
67
68
68
Args:
69
69
path: A Path or string
70
+ create: If True, create the path if it doesn't exist.
70
71
"""
71
72
# Convert to a Path object and resolve resource handle
72
73
if isinstance (path , str ):
@@ -103,6 +104,15 @@ def resolve(path: str | Path) -> Path:
103
104
else :
104
105
path = Path (path )
105
106
107
+ if create :
108
+ if path .suffix :
109
+ # Path is a file so create a directory and file
110
+ path .parent .mkdir (parents = True , exist_ok = True )
111
+ path .touch (exist_ok = True )
112
+ else :
113
+ # Path is a directory so create it
114
+ path .mkdir (parents = True , exist_ok = True )
115
+
106
116
try :
107
117
path = Path (path .resolve (strict = True ))
108
118
except AttributeError :
Original file line number Diff line number Diff line change @@ -18,6 +18,22 @@ def test_default_handles():
18
18
resources .resolve (":system:images/cards/cardBack_blue1.png" )
19
19
20
20
21
+ def test_handle_create (tmp_path ):
22
+ # Test directory creation
23
+ new_dir = tmp_path / "created_dir"
24
+ assert not new_dir .exists ()
25
+ result_dir = resources .resolve (new_dir , create = True )
26
+ assert result_dir == new_dir .resolve ()
27
+ assert new_dir .exists () and new_dir .is_dir ()
28
+
29
+ # Test file creation
30
+ new_file = tmp_path / "created_file.txt"
31
+ assert not new_file .exists ()
32
+ result_file = resources .resolve (new_file , create = True )
33
+ assert result_file == new_file .resolve ()
34
+ assert new_file .exists () and new_file .is_file ()
35
+
36
+
21
37
def test_add_handles (monkeypatch ):
22
38
monkeypatch .setattr (resources , "handles" , {})
23
39
You can’t perform that action at this time.
0 commit comments