Skip to content

Commit

Permalink
Enable timeout in URI (#173)
Browse files Browse the repository at this point in the history
* 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

* Enable `timeout` in URI

I almost exclusively use the URI syntax for filesystems, and I have at least 2 FTP servers that are occasionally slow to respond.

This allows me to specify a timeout in the URI for those servers.

* Fixed open_ftp* tests and added test

Fixed the open_ftp* tests and added an additional test for the `timeout` parameter

* Renamed test to conform to standard

* Added URL Parameter decode test
  • Loading branch information
geoffjukes authored and willmcgugan committed May 28, 2018
1 parent 8694573 commit 62e5e75
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
3 changes: 2 additions & 1 deletion fs/opener/ftpfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def open_fs(self,
port=ftp_port,
user=parse_result.username,
passwd=parse_result.password,
proxy=parse_result.params.get('proxy')
proxy=parse_result.params.get('proxy'),
timeout=int(parse_result.params.get('timeout', '10'))
)
if dir_path:
if create:
Expand Down
34 changes: 32 additions & 2 deletions tests/test_opener.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ def test_parse_params_multiple(self):
)
self.assertEqual(expected, parsed)

def test_parse_params_timeout(self):
parsed = opener.parse('ftp://ftp.example.org?timeout=30')
expected = ParseResult(
'ftp',
None,
None,
'ftp.example.org',
{
'timeout':'30'
},
None
)
self.assertEqual(expected, parsed)

def test_parse_user_password_proxy(self):
parsed = opener.parse('ftp://user:password@ftp.example.org?proxy=ftp.proxy.org')
expected = ParseResult(
Expand Down Expand Up @@ -140,6 +154,20 @@ def test_parse_resource_decode(self):
)
self.assertEqual(expected, parsed)

def test_parse_params_decode(self):
parsed = opener.parse('ftp://ftp.example.org?decode=is%20working')
expected = ParseResult(
'ftp',
None,
None,
'ftp.example.org',
{
'decode':'is working'
},
None
)
self.assertEqual(expected, parsed)


class TestRegistry(unittest.TestCase):

Expand Down Expand Up @@ -309,10 +337,12 @@ def test_user_data_opener(self):
@mock.patch("fs.ftpfs.FTPFS")
def test_open_ftp(self, mock_FTPFS):
open_fs('ftp://foo:bar@ftp.example.org')
mock_FTPFS.assert_called_once_with('ftp.example.org', passwd='bar', port=21, user='foo', proxy=None)
mock_FTPFS.assert_called_once_with('ftp.example.org', passwd='bar', port=21, user='foo', proxy=None,
timeout=10)

@mock.patch("fs.ftpfs.FTPFS")
def test_open_ftp_proxy(self, mock_FTPFS):
open_fs('ftp://foo:bar@ftp.example.org?proxy=ftp.proxy.org')
mock_FTPFS.assert_called_once_with('ftp.example.org', passwd='bar', port=21, user='foo', proxy='ftp.proxy.org')
mock_FTPFS.assert_called_once_with('ftp.example.org', passwd='bar', port=21, user='foo', proxy='ftp.proxy.org',
timeout=10)

0 comments on commit 62e5e75

Please sign in to comment.