def walk(self, path): """ Walk a filesystem path Implements the :func:`os.walk` interface. """ path = util.sanitize(path) entries = [] inspect = [path] while True: dirstack = [] for entry in inspect: dirent = self._direntry(entry) dirs = [] files = [] if dirent: for e in dirent: if type(dirent[e]) is dict: dirs.append(e) else: files.append(e) yield (entry, dirs, files) dirstack.extend([os.path.join(entry, d) for d in dirs]) inspect = dirstack if not inspect: break raise StopIteration
def isdir(self, path): """ Return True if path is a directory Implements the :func:`os.path.isdir` interface. """ path = util.sanitize(path) return type(self._direntry(path)) is dict
def islink(self, path): """ Return True if path is a symlink .. note:: Currently hard-wired to return False """ path = util.sanitize(path) return False
def exists(self, path): """ Return True if path exists Implements the :func:`os.path.exists` interface. """ path = util.sanitize(path) dirent = self._direntry(os.path.dirname(path)) if path == '/': return bool(dirent) return bool(dirent) and os.path.basename(path) in dirent
def listdir(self, path): """ Return the directory contents of 'path' Implements the :func:`os.listdir` interface. :param path: filesystem path """ path = util.sanitize(path) direntry = self._direntry(path) if direntry: entries = list(direntry.keys()) entries.sort() return entries return []
def _direntry(self, path): """Return the directory "dict" entry for a path""" path = util.sanitize(path) if path == '/': return self._entries elts = path.split('/')[1:] current = self._entries retval = None for elt in elts: if elt in current: retval = current[elt] current = current[elt] else: return None return retval
def test_sanitize(self): """Test sanitize() with rogue paths""" self.assertEqual(util.sanitize("///"), "/") self.assertEqual(util.sanitize("///usr//bin///"), "/usr/bin") self.assertEqual(util.sanitize("///usr//bin"), "/usr/bin")
def test_sanitize(self): """Test sanitize() with rogue paths""" self.assertEqual(util.sanitize('///'), '/') self.assertEqual(util.sanitize('///usr//bin///'), '/usr/bin') self.assertEqual(util.sanitize('///usr//bin'), '/usr/bin')