예제 #1
0
    def upload_file(self, src_file, path=None):
        """ Upload a file to the server

            :param src_file: File to be sent.
            :type src_file: file or String

        """

        # Is the file a file name or file object?
        if hasattr(src_file, 'read') and not isinstance(src_file, array):
            # It's an object. In order to avoid complications with httplib2,
            # close file and reopen using the file_wrapper object
            file_name = src_file.name
            src_file.close()
        else:
            file_name = src_file
        fileobj = file_wrapper.FileWrapper(file_name, 'rb')

        if not path:
            path = os.path.join(self.connection.path, file_name)

        resp, content = self.connection.send_put(path, fileobj)
        fileobj.close()

        return resp, content
예제 #2
0
    def send_file(self,
                  connection,
                  resource_uri,
                  local_file_path,
                  extra_headers=None):
        """ Send file

            :param connection: Connection object
            :type connection: Connection

            :param resource_uri: the path of the resource / collection minus
                                 the host section
            :type resource_uri: String

            :param local_file_path: the path of the local file
            :type local_file_path: String

            :param extra_headers: Additional headers may be added here
            :type extra_headers: Dict

        """

        # The file_wrapper is a file like object. It's purpose is to allow
        # files to be sent in chunks, rather than read into memory as one
        # whole thing. httplib2 currently does not support sending in parts
        # well so this is a work around.
        if not extra_headers:
            extra_headers = {}

        local_file_fd = file_wrapper.FileWrapper(local_file_path, 'r')
        data = local_file_fd.read()
        resp, contents = connection.send_put(resource_uri, data)
        return resp, contents
예제 #3
0
    def upload_file(self, src_file, path=None):
        """
        Upload a file to the server

        :param src_file: File to be sent.
        :type src_file: file or String

        """

        # Is the file a file name or file object?
        if hasattr(src_file, 'read') and not isinstance(src_file, array):
            # If this is file object - close it in order to use own chunked read
            file_name = src_file.name
            src_file.close()
        else:
            file_name = src_file

        fileobj = file_wrapper.FileWrapper(file_name, 'rb')

        if not path:
            path = os.path.join(self.connection.path, file_name)

        resp, content = self.connection.send_put(path, fileobj)
        fileobj.close()

        return resp, content
    def test_read_too_much(self):
        self.assertTrue(os.path.exists("thing.txt"))
        fd3 = fw.FileWrapper("thing.txt", "r")
        data_first = fd3.read(100)
        tell_pos1 = fd3.tell()

        fd3.close()

        self.assertEquals(data_first, 'Hello World!')
        self.assertEquals(tell_pos1, 0)
    def test_callback_runs_ten_percent(self):
        test_callback_obj = DummyObj()

        fd4 = fw.FileWrapper('thing.txt',
                             'r',
                             callback=test_callback_obj.dummy_callback,
                             callback_size=10)
        fd4.read()
        fd4.close()
        self.assertEqual(test_callback_obj.percent, 100)
    def test_callback_runs_default_percent(self):
        test_callback_obj = DummyObj()

        self.assertTrue(os.path.exists("thing.txt"))
        fd3 = fw.FileWrapper("thing.txt",
                             "r",
                             callback=test_callback_obj.dummy_callback)
        fd3.read()
        fd3.close()
        self.assertEqual(test_callback_obj.percent, 100)
    def test_reads_full_file(self):

        self.assertTrue(os.path.exists("thing.txt"))

        fd2 = fw.FileWrapper("thing.txt", "r")
        data1 = fd2.read()
        tell_pos1 = fd2.tell()
        fd2.close()

        self.assertEquals(data1, "Hello World!")
        self.assertEquals(tell_pos1, 0)
    def test_callback_runs_twenty_percent(self):
        """ Callbacks are made every 20 percent
        """
        test_callback_obj = DummyObj(cb_percent=20)

        fd4 = fw.FileWrapper('thing.txt',
                             'r',
                             callback=test_callback_obj.dummy_callback,
                             callback_size=20)
        fd4.read()
        fd4.close()
        self.assertEqual(test_callback_obj.percent, 100)
    def test_callback_runs_twenty_percent_small(self):
        """ Callbacks are made every 20 percent but keep the dummy object
            at 10 so we can check it does not underrun
        """
        test_callback_obj = DummyObj()

        fd4 = fw.FileWrapper('thing.txt',
                             'r',
                             callback=test_callback_obj.dummy_callback,
                             callback_size=20)
        fd4.read()
        fd4.close()
        self.assertEqual(test_callback_obj.percent, 100)
    def read_start_then_rest(self):
        self.assertTrue(os.path.exists("thing.txt"))
        fd3 = fw.FileWrapper("thing.txt", "r")
        data_first = fd3.read(5)
        tell_pos1 = fd3.tell()
        data_second = fd3.read()
        tell_pos2 = fd3.tell()

        fd3.close()

        self.assertEquals(data_first, 'Hello')
        self.assertEquals(data_second, " World!")
        self.assertEquals(tell_pos1, 0)
        self.assertEquals(tell_pos2, 0)
    def test_read_beyond(self):
        self.assertTrue(os.path.exists("thing.txt"))
        fd3 = fw.FileWrapper("thing.txt", "r")
        data_first = fd3.read()
        tell_pos1 = fd3.tell()
        data_second = fd3.read(7)
        tell_pos2 = fd3.tell()

        fd3.close()

        self.assertEquals(data_first, 'Hello World!')
        self.assertEquals(data_second, "Hello W")
        self.assertEquals(tell_pos1, 0)
        self.assertEquals(tell_pos2, 7)
    def test_reads_two_parts(self):
        self.assertTrue(os.path.exists("thing.txt"))
        fd3 = fw.FileWrapper("thing.txt", "r")
        data_first = fd3.read(5)
        tell_pos1 = fd3.tell()
        data_second = fd3.read(7)
        tell_pos2 = fd3.tell()

        fd3.close()

        self.assertEquals(data_first, 'Hello')
        self.assertEquals(data_second, " World!")
        self.assertEquals(tell_pos1, 5)
        self.assertEquals(tell_pos2, 12)
    def test_read_force_size(self):
        self.assertTrue(os.path.exists("thing.txt"))
        fd3 = fw.FileWrapper("thing.txt", "r", force_size=2)
        data1 = fd3.read(1)
        data2 = fd3.read(2)
        data3 = fd3.read(3)
        data4 = fd3.read()

        fd3.close()

        self.assertEquals(data1, 'He')
        self.assertEquals(data2, 'll')
        self.assertEquals(data3, 'o ')
        self.assertEquals(data4, 'Wo')
 def setUp(self):
     if os.path.exists("thing.txt"):
         os.remove("thing.txt")
     fd1 = fw.FileWrapper("thing.txt", "w")
     fd1.write("Hello World!")
     fd1.close()