Skip to content

Commit d41de6e

Browse files
geoffjukeswillmcgugan
authored andcommitted
Honor create option in opener (#170)
* Honor `create` option in opener Ported fix from fs.sshfs All credit to @althonos althonos/fs.sshfs@48664f2 * Switch to CreateFailed.catch_all decorator as suggested
1 parent 59c5933 commit d41de6e

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

fs/opener/ftpfs.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
from __future__ import print_function
77
from __future__ import unicode_literals
88

9+
import six
10+
911
import typing
1012

1113
from .base import Opener
14+
from ..errors import FSError, CreateFailed
1215

1316
if False: # typing.TYPE_CHECKING
1417
from typing import List, Text, Union
@@ -23,6 +26,7 @@ class FTPOpener(Opener):
2326

2427
protocols = ['ftp']
2528

29+
@CreateFailed.catch_all
2630
def open_fs(self,
2731
fs_url, # type: Text
2832
parse_result, # type: ParseResult
@@ -44,5 +48,8 @@ def open_fs(self,
4448
proxy=parse_result.params.get('proxy')
4549
)
4650
if dir_path:
51+
if create:
52+
ftp_fs.makedirs(dir_path, recreate=True)
4753
return ftp_fs.opendir(dir_path, factory=ClosingSubFS)
48-
return ftp_fs
54+
else:
55+
return ftp_fs

tests/test_ftpfs.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,34 @@ def test_opener_path(self):
164164
self.assertEqual(ftp_fs.gettext('bar'), 'baz')
165165
ftp_fs.close()
166166

167+
def test_create(self):
168+
169+
directory = os.path.join("home", self.user, "test", "directory")
170+
base = 'ftp://user:1234@{}:{}/foo'.format(self.server.host, self.server.port)
171+
url = "{}/{}".format(base, directory)
172+
173+
# Make sure unexisting directory raises `CreateFailed`
174+
with self.assertRaises(errors.CreateFailed):
175+
ftp_fs = open_fs(url)
176+
177+
# Open with `create` and try touching a file
178+
with open_fs(url, create=True) as ftp_fs:
179+
ftp_fs.touch("foo")
180+
181+
# Open the base filesystem and check the subdirectory exists
182+
with open_fs(base) as ftp_fs:
183+
self.assertTrue(ftp_fs.isdir(directory))
184+
self.assertTrue(ftp_fs.isfile(os.path.join(directory, "foo")))
185+
186+
# Open without `create` and check the file exists
187+
with open_fs(url) as ftp_fs:
188+
self.assertTrue(ftp_fs.isfile("foo"))
189+
190+
# Open with create and check this does fail
191+
with open_fs(url, create=True) as ftp_fs:
192+
self.assertTrue(ftp_fs.isfile("foo"))
193+
194+
167195
class TestFTPFSNoMLSD(TestFTPFS):
168196

169197
def make_fs(self):
@@ -174,3 +202,4 @@ def make_fs(self):
174202

175203
def test_features(self):
176204
pass
205+

0 commit comments

Comments
 (0)