Skip to content

Commit

Permalink
fix for openening file in missing directory (#21)
Browse files Browse the repository at this point in the history
* fix for openening file in missing directory

* tweak
  • Loading branch information
willmcgugan authored Feb 2, 2018
1 parent 579b33c commit ecff749
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
11 changes: 10 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ As a PyFilesystem concrete class,
`S3FS <http://fs-s3fs.readthedocs.io/en/latest/>`__ allows you to work
with S3 in the same way as any other supported filesystem.

Installing
----------

You can install S3FS from pip as follows:

::

pip install fs-s3fs

Opening a S3FS
--------------

Open an S3FS by explicitly using the constructor:

.. code:: python
from s3_s3fs import s3FS
from s3_s3fs import S3FS
s3fs = S3FS('mybucket')
Or with a FS URL:
Expand Down
56 changes: 48 additions & 8 deletions fs_s3fs/_s3fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from fs.info import Info
from fs import errors
from fs.mode import Mode
from fs.subfs import SubFS
from fs.path import basename, dirname, forcedir, join, normpath, relpath
from fs.time import datetime_to_epoch

Expand Down Expand Up @@ -336,8 +337,6 @@ def _get_object(self, path, key):
)
obj.load()
return obj
except Exception:
raise
else:
return obj

Expand Down Expand Up @@ -410,6 +409,13 @@ def _info_from_object(self, obj, namespaces):
}
return info

def isdir(self, path):
_path = self.validatepath(path)
try:
return self._getinfo(_path).is_dir
except errors.ResourceNotFound:
return False

def getinfo(self, path, namespaces=None):
self.check()
namespaces = namespaces or ()
Expand All @@ -420,7 +426,11 @@ def getinfo(self, path, namespaces=None):
dir_path = dirname(_path)
if dir_path != '/':
_dir_key = self._path_to_dir_key(dir_path)
self._get_object(dir_path, _dir_key)
with s3errors(path):
obj = self.s3.Object(
self._bucket_name, _dir_key
)
obj.load()
except errors.ResourceNotFound:
raise errors.ResourceNotFound(path)

Expand All @@ -441,6 +451,28 @@ def getinfo(self, path, namespaces=None):
info = self._info_from_object(obj, namespaces)
return Info(info)

def _getinfo(self, path, namespaces=None):
"""Gets info without checking for parent dir."""
namespaces = namespaces or ()
_path = self.validatepath(path)
_key = self._path_to_key(_path)
if _path == '/':
return Info({
"basic":
{
"name": "",
"is_dir": True
},
"details":
{
"type": int(ResourceType.directory)
}
})

obj = self._get_object(path, _key)
info = self._info_from_object(obj, namespaces)
return Info(info)

def listdir(self, path):
_path = self.validatepath(path)
_s3_key = self._path_to_dir_key(_path)
Expand Down Expand Up @@ -481,7 +513,7 @@ def makedir(self, path, permissions=None, recreate=False):
raise errors.ResourceNotFound(path)

try:
self.getinfo(path)
self._getinfo(path)
except errors.ResourceNotFound:
pass
else:
Expand All @@ -491,7 +523,7 @@ def makedir(self, path, permissions=None, recreate=False):
raise errors.DirectoryExists(path)
with s3errors(path):
self.s3.Object(self._bucket_name, _key).put()
return self.opendir(path)
return SubFS(self, path)

def openbin(self, path, mode="r", buffering=-1, **options):
_mode = Mode(mode)
Expand All @@ -514,7 +546,15 @@ def on_close_create(s3file):
s3file.raw.close()

try:
info = self.getinfo(path)
dir_path = dirname(_path)
if dir_path != '/':
_dir_key = self._path_to_dir_key(dir_path)
self._get_object(dir_path, _dir_key)
except errors.ResourceNotFound:
raise errors.ResourceNotFound(path)

try:
info = self._getinfo(path)
except errors.ResourceNotFound:
pass
else:
Expand Down Expand Up @@ -709,7 +749,7 @@ def setbytes(self, path, contents):
if not self.isdir(dirname(path)):
raise errors.ResourceNotFound(path)
try:
info = self.getinfo(path)
info = self._getinfo(path)
if info.is_dir:
raise errors.FileExpected(path)
except errors.ResourceNotFound:
Expand All @@ -729,7 +769,7 @@ def setbinfile(self, path, file):
if not self.isdir(dirname(path)):
raise errors.ResourceNotFound(path)
try:
info = self.getinfo(path)
info = self._getinfo(path)
if info.is_dir:
raise errors.FileExpected(path)
except errors.ResourceNotFound:
Expand Down
2 changes: 1 addition & 1 deletion fs_s3fs/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.6"
__version__ = "0.1.7"

0 comments on commit ecff749

Please sign in to comment.