Commit 32a62c41 by willmcgugan@gmail.com

getinfokeys method

parent c2aa5d7a
......@@ -740,6 +740,22 @@ class FS(object):
"""
raise UnsupportedError("get resource info")
def getinfokeys(self, path, *keys):
"""Get specified keys from info dict, as returned from `getinfo`. The returned dictionary may
not contain all the keys that were asked for, if they aren't available.
This method allows a filesystem to potentially provide a faster way of retrieving these info values if you
are only interested in a subset of them.
:param path: a path to retrieve information for
:param keys: the info keys you would like to retrieve
:rtype: dict
"""
info = self.getinfo(path)
return {k: info[k] for k in keys if k in info}
def desc(self, path):
"""Returns short descriptive text regarding a path. Intended mainly as
a debugging aid.
......@@ -760,8 +776,13 @@ class FS(object):
"""Returns the contents of a file as a string.
:param path: A path of file to read
:param mode: Mode to open file with (should be 'rb' for binary or 't' for text)
:param encoding: Encoding to use when reading contents in text mode
:param errors: Unicode errors parameter if text mode is use
:param newline: Newlines parameter for text mode decoding
:rtype: str
:returns: file contents
"""
if 'r' not in mode:
raise ValueError("mode must contain 'r' to be readable")
......
......@@ -271,7 +271,7 @@ class OpenerRegistry(object):
file_object.fs = fs
return file_object
def getcontents(self, fs_url, node='rb', encoding=None, errors=None, newline=None):
def getcontents(self, fs_url, mode='rb', encoding=None, errors=None, newline=None):
"""Gets the contents from a given FS url (if it references a file)
:param fs_url: a FS URL e.g. ftp://ftp.mozilla.org/README
......
......@@ -343,17 +343,40 @@ class OSFS(OSFSXAttrMixin, OSFSWatchMixin, FS):
info = dict((k, getattr(stats, k)) for k in dir(stats) if k.startswith('st_'))
info['size'] = info['st_size']
# TODO: this doesn't actually mean 'creation time' on unix
fromtimestamp = datetime.datetime.fromtimestamp
ct = info.get('st_ctime', None)
if ct is not None:
info['created_time'] = datetime.datetime.fromtimestamp(ct)
info['created_time'] = fromtimestamp(ct)
at = info.get('st_atime', None)
if at is not None:
info['accessed_time'] = datetime.datetime.fromtimestamp(at)
info['accessed_time'] = fromtimestamp(at)
mt = info.get('st_mtime', None)
if mt is not None:
info['modified_time'] = datetime.datetime.fromtimestamp(mt)
info['modified_time'] = fromtimestamp(mt)
return info
@convert_os_errors
def getinfokeys(self, path, *keys):
info = {}
stats = self._stat(path)
fromtimestamp = datetime.datetime.fromtimestamp
for key in keys:
try:
if key == 'size':
info[key] = stats.st_size
elif key == 'modified_time':
info[key] = fromtimestamp(stats.st_mtime)
elif key == 'created_time':
info[key] = fromtimestamp(stats.st_ctime)
elif key == 'accessed_time':
info[key] = fromtimestamp(stats.st_atime)
else:
info[key] = getattr(stats, key)
except AttributeError:
continue
return info
@convert_os_errors
def getsize(self, path):
return self._stat(path).st_size
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment