Exemplo n.º 1
0
    def open(self,
             path,
             mode='r',
             buffering=-1,
             encoding=None,
             errors=None,
             newline=None,
             line_buffering=False,
             **kwargs):
        if self.isdir(path):
            raise ResourceInvalidError(path)
        if 'w' in mode and not self.isdir(dirname(path)):
            raise ParentDirectoryMissingError(path)
        if 'r' in mode and not self.isfile(path):
            raise ResourceNotFoundError(path)
            if not self.isdir(dirname(path)):
                raise ParentDirectoryMissingError(path)
        if 'w' in mode and '+' not in mode and self.isfile(path):
            self.remove(path)

        data = ''
        if 'r' in mode:
            data = self.getcontents(path,
                                    mode=mode,
                                    encoding=encoding,
                                    errors=errors,
                                    newline=newline)
        rfile = StringIO(data=data, mode=mode)
        return RemoteFileBuffer(self, path, mode=mode, rfile=rfile)
Exemplo n.º 2
0
    def copy(self, src, dst, overwrite=False, chunk_size=65536):
        if self.isdir(src):
            raise ResourceInvalidError(src)
        if not self.isfile(src):
            if not self.isdir(dirname(src)):
                raise ParentDirectoryMissingError(src)
            raise ResourceNotFoundError(src)

        if self.isdir(dst):
            raise ResourceInvalidError(dst)
        if self.isfile(dst):
            if overwrite:
                self.remove(dst)
            else:
                raise DestinationExistsError(dst)
        else:
            if not self.isdir(dirname(dst)):
                raise ParentDirectoryMissingError(dst)

        parent_path = self._ids[dirname(dst)]
        copy_fh = {'title': basename(dst), 'parents': [{'id': parent_path}]}
        copy_fh = self.client.auth.service.files() \
                                  .copy(fileId=self._ids[src], body=copy_fh) \
                                  .execute()
        self._ids[dst] = copy_fh['id']
Exemplo n.º 3
0
    def listdir(self,
                path="/",
                wildcard=None,
                full=False,
                absolute=False,
                dirs_only=False,
                files_only=False):
        if self.isfile(path):
            raise ResourceInvalidError(path)
        if not self.isdir(path):
            if not self.isdir(dirname(path)):
                raise ParentDirectoryMissingError(path)
            raise ResourceNotFoundError(path)

        query = "'{0}' in parents and trashed=false"\
                .format(self._ids[dirname(path)])

        if dirs_only:
            query += " and mimeType = '{0}'".format(self._folder_mimetype)
        if files_only:
            query += " and mimeType != '{0}'".format(self._folder_mimetype)
        self._ids = self._map_ids_to_paths()
        entries = self._ids.names(path)
        # entries = self.client.ListFile({"q": query,
        #                                "fields": "items(title,id,"
        #                                "parents(id,isRoot))"}).GetList()
        # We don't want the _listdir_helper to perform dirs_only
        # and files_only filtering again
        return self._listdir_helper(path,
                                    entries,
                                    wildcard=wildcard,
                                    full=full,
                                    absolute=absolute,
                                    dirs_only=dirs_only,
                                    files_only=files_only)
Exemplo n.º 4
0
    def makedir(self, path, recursive=False, allow_recreate=False):
        """Creates a file with mimeType _folder_mimetype
        which acts as a folder in GoogleDrive."""
        if self.isdir(path):
            if allow_recreate:
                return
            else:
                raise DestinationExistsError(path)
        if self.isfile(path):
            raise ResourceInvalidError(path)
        if not recursive and not self.isdir(dirname(path)):
            raise ParentDirectoryMissingError(path)

        if recursive:
            self.makedir(dirname(path),
                         recursive=recursive,
                         allow_recreate=True)

        parent_id = self._ids[dirname(path)]
        fh = self.client.CreateFile({
            'title': basename(path),
            'mimeType': self._folder_mimetype,
            'parents': [{
                'id': parent_id
            }]
        })
        fh.Upload()
        self._ids[path] = fh['id']
Exemplo n.º 5
0
    def makedir(self, path, recursive=False, allow_recreate=False):
        path = normpath(path)
        if path in ('', '/'):
            return

        if recursive:
            created = False
            for path_part in recursepath(path):
                if not self.isdir(path_part):
                    self.conn.mkdir(self.smb_path(path_part))
                    created = True
                else:
                    if self.isfile(path_part):
                        raise ResourceInvalidError(path_part)

            if not created and not allow_recreate:
                raise DestinationExistsError(path)
        else:
            base = dirname(path)
            if not self.exists(base):
                raise ParentDirectoryMissingError(path)

            if not allow_recreate:
                if self.exists(path):
                    if self.isfile(path):
                        raise ResourceInvalidError(path)
                    raise DestinationExistsError(path)
                self.conn.mkdir(self.smb_path(path))
            else:
                if not self.isdir(path):
                    self.conn.mkdir(self.smb_path(path))
Exemplo n.º 6
0
    def remove(self, path):
        if self.isdir(path):
            raise ResourceInvalidError(path)
        if not self.isfile(path):
            if not self.isdir(dirname(path)):
                raise ParentDirectoryMissingError(path)
            raise ResourceNotFoundError(path)

        self.client.auth.service.files() \
                                .delete(fileId=self._ids[path]) \
                                .execute()
        self._ids.pop(path)
Exemplo n.º 7
0
    def getcontents(self,
                    path,
                    mode='rb',
                    encoding=None,
                    errors=None,
                    newline=None):
        if self.isdir(path):
            raise ResourceInvalidError(path)
        if not self.isfile(path):
            if not self.isdir(dirname(path)):
                raise ParentDirectoryMissingError(path)
            raise ResourceNotFoundError(path)

        fh = self.client.CreateFile({'id': self._ids[path]})
        return fh.GetContentString()
Exemplo n.º 8
0
    def getinfo(self, path):
        if self.isdir(path):
            raise ResourceInvalidError(path)
        if not self.isfile(path):
            if not self.isdir(dirname(path)):
                raise ParentDirectoryMissingError(path)
            raise ResourceNotFoundError(path)

        fh = self.client.CreateFile({
            'id': self._ids[path],
            'title': basename(path)
        })
        return {
            'size': int(fh['fileSize']),
            'created_time': fh['createdDate'],
            'acessed_time': fh['lastViewedByMeDate'],
            'modified_time': fh['modifiedDate']
        }
Exemplo n.º 9
0
 def inner(*args, **kwargs):
     try:
         return outer(*args, **kwargs)
     except DestinationExistsError:
         raise DestinationExistsError(args[2])
     except ResourceNotFoundError:
         # Parent directory does not exist or is not a directory.
         src, dst = args[1:3]
         fs = args[0]
         for p in (src, dst):
             if not fs.exists(p):
                 root = dirname(p)
                 if not fs.isdir(root):
                     if fs.isfile(root):
                         raise ResourceInvalidError(p)
                     else:
                         raise ParentDirectoryMissingError(p)
                 else:
                     raise ResourceNotFoundError(p)
Exemplo n.º 10
0
    def _get_item_by_path(self, path):
        """Returns the item at given path.

        :param path:
            The normalized path of the item.
        :type path:
            `str`

        :returns:
            The item as returned by the API call. Example Response:
                {
                    "type":"file",
                    "id":"2305649799",
                    "sequence_id":"1",
                    "name":"testing.html"
                }
        :rtype:
            `dict`
        """
        if path == '/':
            return {
                'type': _ITEM_TYPE_FOLDER,
                'id': self._root_id,
                # TODO(kunal): find correct value for this field.
                'sequence_id': '1',
                'name': path,
            }

        parent_box_id = self._root_id
        parent_path, item_name = pathsplit(path)
        for name in iteratepath(parent_path):
            items = self._get_children_items(parent_box_id)
            item = items.get(name)
            if not item or item['type'] != _ITEM_TYPE_FOLDER:
                raise ParentDirectoryMissingError(path)

            parent_box_id = item['id']

        items = self._get_children_items(parent_box_id)
        return items.get(item_name)
Exemplo n.º 11
0
    def rename(self, src, dst):
        src_path = self._prepare_abspath(src)
        dst_path = self._prepare_abspath(dst)

        if not self.exists(src):
            raise ResourceNotFoundError(src)

        # src and dst pathes should be different
        if src_path == dst_path:
            raise ResourceInvalidError(dst)

        src_is_dir = self.isdir(src)

        if self.exists(dst):
            dst_is_dir = self.isdir(dst)

            if (src_is_dir and not dst_is_dir)\
                or (dst_is_dir and not src_is_dir)\
                    or (src_is_dir and dst_is_dir and src_path.lower() != dst_path.lower()):
                # note about last condition: in reality unix system allow us
                # to rename src directory to dst directory only in case dst directory is empty
                # but for simplicity we don't consider this case
                raise ResourceInvalidError(dst)

        elif not self.exists(dirname(dst)):
            raise ParentDirectoryMissingError(dst)
        else:
            dst_is_dir = src_is_dir

        # check that src isn't a parent of dst
        if src_is_dir and dst_is_dir:
            src_path = ''.join([src_path, '/'])
            dst_path = ''.join([dst_path, '/'])
            if dst_path.startswith(src_path, 0):
                raise ResourceInvalidError(dst)

        try:
            self.conn.rename(self.smb_path(src_path), self.smb_path(dst_path))
        except:
            raise ResourceInvalidError(dst)
Exemplo n.º 12
0
    def makedir(self, path, recursive=False, allow_recreate=False):
        if not path and not allow_recreate:
            raise PathError(path)

        path = normpath(path)
        if path in ('', '/'):
            if allow_recreate:
                return
            raise DestinationExistsError(path)

        parent_path, dirname = pathsplit(path)
        parent_box_id = self._root_id
        for name in iteratepath(parent_path):
            children_items = self._get_children_items(parent_box_id)
            child_item = children_items.get(name)
            if not child_item:
                if recursive:
                    child_item = self._makedir(parent_box_id, name)
                else:
                    raise ParentDirectoryMissingError(path)

            if child_item['type'] != _ITEM_TYPE_FOLDER:
                raise ResourceInvalidError(path)

            parent_box_id = child_item['id']

        # Check if an item with required name already exists.
        children_items = self._get_children_items(parent_box_id)
        child_item = children_items.get(dirname)
        if child_item:
            if allow_recreate and child_item['type'] == _ITEM_TYPE_FOLDER:
                return
            else:
                raise DestinationExistsError(path)

        self._makedir(parent_box_id, dirname)
Exemplo n.º 13
0
    def removedir(self, path, recursive=False, force=False):
        if self.isfile(path):
            raise ResourceInvalidError(path)
        if not self.isdir(path):
            if not self.isdir(dirname(path)):
                raise ParentDirectoryMissingError(path)
            raise ResourceNotFoundError(path)
        if abspath(path) == "/":
            raise RemoveRootError(path)
        if force:
            for child_path in self.listdir(path, full=True, dirs_only=True):
                self.removedir(child_path, force=force)
            for child_path in self.listdir(path, full=True, files_only=True):
                self.remove(child_path)
        elif len(self.listdir(path)) > 0:
            raise DirectoryNotEmptyError(path)

        self.client.auth.service.files() \
                                .delete(fileId=self._ids[path]) \
                                .execute()
        self._ids.pop(path)

        if recursive and len(self.listdir(dirname(path))) == 0:
            self.removedir(dirname(path), recursive=recursive)
Exemplo n.º 14
0
    def makedir(self, path, recursive=False, allow_recreate=False):
        # Create a directory from the top downwards depending upon the flags.
        paths = recursepath(path) if recursive else (path, )
        for p in paths:
            if p == '/':
                continue

            # Try to create a directory first then ask for forgiveness.
            try:
                self._create_dir(p)
            except DestinationExistsError as e:
                if self.isfile(p):
                    raise ResourceInvalidError(path)
                elif self.isdir(p):
                    if not recursive and not allow_recreate:
                        raise DestinationExistsError(path)
            except ResourceNotFoundError as e:
                if not recursive and not self.isdir(dirname(p)):
                    raise ParentDirectoryMissingError(path)
                e.path = path
                raise
            except FSError as e:
                e.path = path
                raise