Beispiel #1
0
    def save(self, name, content, metadata=None):
        """Save `content` in a file named `name`.

        :parameters:
          - `name`: The name of the file.
          - `content`: A file-like object, string, or bytes.
          - `metadata`: Metadata dictionary to be saved with the file.

        :returns: The id of the saved file.

        """
        gridin_opts = {'filename': name, 'encoding': 'utf8'}
        if metadata is not None:
            gridin_opts['metadata'] = metadata
        gridin = GridIn(self.gridfs._collection, **gridin_opts)

        try:
            content.seek(0)
        except (AttributeError, UnsupportedOperation):
            pass

        if PY3 and hasattr(content, 'mode') and 'b' not in content.mode:
            # File opened in text mode.
            gridin.writelines(content)
        else:
            # File in binary mode, bytes, or text.
            gridin.write(content)

        # Finish writing the file.
        gridin.close()
        return gridin._id
Beispiel #2
0
    def save(self, name, content, metadata=None):
        """Save `content` in a file named `name`.

        :parameters:
          - `name`: The name of the file.
          - `content`: A file-like object, string, or bytes.
          - `metadata`: Metadata dictionary to be saved with the file.

        :returns: The id of the saved file.

        """
        gridin_opts = {'filename': name, 'encoding': 'utf8'}
        if metadata is not None:
            gridin_opts['metadata'] = metadata
        gridin = GridIn(self.gridfs._collection, **gridin_opts)

        try:
            content.seek(0)
        except (AttributeError, UnsupportedOperation):
            pass

        if PY3 and hasattr(content, 'mode') and 'b' not in content.mode:
            # File opened in text mode.
            gridin.writelines(content)
        else:
            # File in binary mode, bytes, or text.
            gridin.write(content)

        # Finish writing the file.
        gridin.close()
        return gridin._id
Beispiel #3
0
    def test_write_lines(self):
        a = GridIn(self.db.fs)
        a.writelines([b"hello ", b"world"])
        a.close()

        self.assertEqual(b"hello world", GridOut(self.db.fs, a._id).read())
    def test_write_lines(self):
        a = GridIn(self.db.fs)
        a.writelines(["hello ", "world"])
        a.close()

        self.assertEqual("hello world", GridOut(self.db.fs, a._id).read())