From 89628de519ad4858ee969e809f177abebcdf8a3d Mon Sep 17 00:00:00 2001 From: "willmcgugan@gmail.com" Date: Fri, 18 Oct 2013 15:29:35 +0000 Subject: getinfokeys method git-svn-id: http://pyfilesystem.googlecode.com/svn/trunk@881 67cdc799-7952-0410-af00-57a81ceafa0f --- fs/base.py | 21 +++++++++++++++++++++ fs/opener.py | 2 +- fs/osfs/__init__.py | 29 ++++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/fs/base.py b/fs/base.py index 8e2686e..fed68fb 100644 --- a/fs/base.py +++ b/fs/base.py @@ -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") diff --git a/fs/opener.py b/fs/opener.py index 49fef9b..54cebe3 100644 --- a/fs/opener.py +++ b/fs/opener.py @@ -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 diff --git a/fs/osfs/__init__.py b/fs/osfs/__init__.py index 47ce406..5785f0e 100644 --- a/fs/osfs/__init__.py +++ b/fs/osfs/__init__.py @@ -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 -- cgit v1.2.1