@@ -68,6 +68,7 @@ def __init__(
68
68
encrypt = None ,
69
69
share_access = None ,
70
70
register_session_retries = 5 ,
71
+ auto_mkdir = False ,
71
72
** kwargs ,
72
73
):
73
74
"""
@@ -102,6 +103,10 @@ def __init__(
102
103
- 'r': Allow other handles to be opened with read access.
103
104
- 'w': Allow other handles to be opened with write access.
104
105
- 'd': Allow other handles to be opened with delete access.
106
+ auto_mkdir: bool
107
+ Whether, when opening a file, the directory containing it should
108
+ be created (if it doesn't already exist). This is assumed by pyarrow
109
+ and zarr-python code.
105
110
"""
106
111
super ().__init__ (** kwargs )
107
112
self .host = host
@@ -113,6 +118,7 @@ def __init__(
113
118
self .temppath = kwargs .pop ("temppath" , "" )
114
119
self .share_access = share_access
115
120
self .register_session_retries = register_session_retries
121
+ self .auto_mkdir = auto_mkdir
116
122
self ._connect ()
117
123
118
124
@property
@@ -224,6 +230,8 @@ def _open(
224
230
By specifying 'share_access' in 'kwargs' it is possible to override the
225
231
default shared access setting applied in the constructor of this object.
226
232
"""
233
+ if self .auto_mkdir and "w" in mode :
234
+ self .makedirs (self ._parent (path ), exist_ok = True )
227
235
bls = block_size if block_size is not None and block_size >= 0 else - 1
228
236
wpath = _as_unc_path (self .host , path )
229
237
share_access = kwargs .pop ("share_access" , self .share_access )
@@ -245,6 +253,8 @@ def copy(self, path1, path2, **kwargs):
245
253
"""Copy within two locations in the same filesystem"""
246
254
wpath1 = _as_unc_path (self .host , path1 )
247
255
wpath2 = _as_unc_path (self .host , path2 )
256
+ if self .auto_mkdir :
257
+ self .makedirs (self ._parent (path2 ), exist_ok = True )
248
258
smbclient .copyfile (wpath1 , wpath2 , port = self ._port , ** kwargs )
249
259
250
260
def _rm (self , path ):
0 commit comments