Esempio n. 1
0
    def _execute_file_request(self, req):
        if not req.resumable:
            resp = req.execute()
            if "files" in resp:
                return [File(f, client=self) for f in resp["files"]]
            if "file" in resp:
                return File(resp["file"], client=self)
            return File(resp, client=self)
        else:
            progressless_iters = 0
            response = None
            while response is None:
                error = None
                try:
                    progress, response = req.next_chunk()
                    if progress:
                        print_with_carriage_return('Upload %d%%' %
                                                   (100 * progress.progress()))
                except HttpError as err:
                    error = err
                    if err.resp.status < 500:
                        raise
                except RETRYABLE_ERRORS as err:
                    error = err

                if error:
                    progressless_iters += 1
                    handle_progressless_iter(error, progressless_iters)
                else:
                    progressless_iters = 0

            if progress:
                print_with_carriage_return('Upload %d%%' %
                                           (100 * progress.progress()))
Esempio n. 2
0
    def _execute_file_request(self, req) -> Union[List[File], File]:
        """

        :param req:
        :return:
        """
        if not req.resumable:
            # extract supportAllDrives from uri
            supports_all_drives = False
            if req.uri.find("supportsAllDrives=true") != -1:
                supports_all_drives = True

            resp = req.execute()
            if "files" in resp:
                return [
                    File(f,
                         client=self,
                         supports_all_drives=supports_all_drives)
                    for f in resp["files"]
                ]
            if "file" in resp:
                return File(resp["file"],
                            client=self,
                            supports_all_drives=supports_all_drives)
            return File(resp,
                        client=self,
                        supports_all_drives=supports_all_drives)
        else:
            progressless_iters = 0
            response = None
            progress = None
            while response is None:
                error = None
                try:
                    progress, response = req.next_chunk()
                    if progress:
                        print_with_carriage_return('Upload %d%%' %
                                                   (100 * progress.progress()))
                except HttpError as err:
                    error = err
                    if err.resp.status < 500:
                        raise
                except RETRYABLE_ERRORS as err:
                    error = err

                if error:
                    progressless_iters += 1
                    handle_progressless_iter(error, progressless_iters)
                else:
                    progressless_iters = 0

            if progress:
                print_with_carriage_return('Upload %d%%' %
                                           (100 * progress.progress()))
Esempio n. 3
0
    def get_or_create_folder(self,
                             folder_name: str,
                             parent_id: Optional[str] = None,
                             supports_all_drives: bool = False):
        """
        Get the ID for the folder with name folder_name.
        :param folder_name:
        :param parent_id:
        :return: drive.File
        """

        folder_list = self.list_files(name_equals=folder_name,
                                      mimetype=mimetypes.GOOGLE_DRIVE_FOLDER,
                                      parents_in=parent_id,
                                      n=1,
                                      supports_all_drives=supports_all_drives)
        if folder_list:
            if len(folder_list) == 1:
                return File(folder_list[0],
                            client=self,
                            supports_all_drives=supports_all_drives)

            raise NameError("Unable to find folder %s" % folder_name)

        return self.create_folder(folder_name, parent_id)
Esempio n. 4
0
 def get_file(self,
              file_id: str,
              raise_if_not_found=True) -> Optional[File]:
     """
     Get a file by its id.
     :param file_id:
     :param raise_if_not_found: if ``True`` (default), raise an exception if the file doesn’t exist
     :return:
     """
     fm = self.get_file_metadata(file_id, raise_if_not_found)
     if fm:
         return File(fm, client=self)
Esempio n. 5
0
    def get_or_create_folder(self, folder_name, parent_id=None):
        """ Get the ID for the folder with name folder_name."""

        folder_list = self.list_files(name_equals=folder_name,
                                      mimetype=mimetypes.GOOGLE_DRIVE_FOLDER,
                                      parents_in=parent_id,
                                      n=1)
        if folder_list:
            if len(folder_list) == 1:
                return File(folder_list[0], client=self)

            raise NameError("Unable to find folder %s" % folder_name)

        return self.create_folder(folder_name, parent_id)
Esempio n. 6
0
 def get_file(self,
              file_id: str,
              raise_if_not_found=True,
              supports_all_drives: bool = False) -> Optional[File]:
     """
     Get a file by its id.
     :param file_id:
     :param raise_if_not_found: if ``True`` (default), raise an exception if the file doesn’t exist
     :return:
     """
     fm = self.get_file_metadata(file_id,
                                 raise_if_not_found,
                                 supportsAllDrives=supports_all_drives)
     if fm:
         return File(fm,
                     client=self,
                     supports_all_drives=supports_all_drives)
Esempio n. 7
0
 def get_file(self, file_id, raise_if_not_found=True):
     fm = self.get_file_metadata(file_id, raise_if_not_found)
     if fm:
         return File(fm, client=self)
Esempio n. 8
0
 def test_name(self):
     name = "Foo Bar"
     file = File({"id": "xx", "name": name})
     self.assertEqual(name, file.name)
Esempio n. 9
0
 def test_exists(self):
     self.assertIsNone(File({"id": "xx"}).exists())
Esempio n. 10
0
 def test_human_type(self):
     self.assertEqual("?", File({"id": "xx"}).human_type)
     self.assertEqual("?", File({"id": "xx", "name": "foo.json"}).human_type)
     self.assertEqual("JSONS", File({"id": "xx", "name": "foo.jsons"}).human_type)
     self.assertEqual("JSON", File({"id": "xx", "name": "foo.jsons", "mimeType": mimetypes.JSON}).human_type)
     self.assertEqual("folder", File({"id": "xx", "mimeType": mimetypes.GOOGLE_DRIVE_FOLDER}).human_type)
Esempio n. 11
0
 def test_is_directory(self):
     self.assertIsNone(File({"id": "xx"}).is_directory)
     self.assertFalse(File({"id": "xx", "mimeType": "something"}).is_directory)
     self.assertFalse(File({"id": "xx", "mimeType": mimetypes.GOOGLE_DRIVE_FILE}).is_directory)
     self.assertTrue(File({"id": "xx", "mimeType": mimetypes.GOOGLE_DRIVE_FOLDER}).is_directory)