Esempio n. 1
0
    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
Esempio n. 2
0
    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
Esempio n. 3
0
    def islink(self, path):
        """
        Return True if path is a symlink

        .. note::

            Currently hard-wired to return False

        """
        path = util.sanitize(path)
        return False
Esempio n. 4
0
    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
Esempio n. 5
0
    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 []
Esempio n. 6
0
 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
Esempio n. 7
0
 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")
Esempio n. 8
0
 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')
Esempio n. 9
0
 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')