Commit 62d662d3 authored by Jim Fulton's avatar Jim Fulton

The Blob open method now supports a new mode, 'c', to open committed

data for reading as an ordinary file, rather than as a blob file.
The ordinary file may be used outside the current transaction and
even after the blob's database connection has been closed.
parent 901af395
...@@ -22,7 +22,18 @@ New Features ...@@ -22,7 +22,18 @@ New Features
XXX There are known issues with this implementation that need to be XXX There are known issues with this implementation that need to be
sorted out before it is "released". sorted out before it is "released".
3.9.0a6 (2008-11-??) 3.9.0a6 (2008-12-??)
====================
New Features
------------
- The Blob open method now supports a new mode, 'c', to open committed
data for reading as an ordinary file, rather than as a blob file.
The ordinary file may be used outside the current transaction and
even after the blob's database connection has been closed.
3.9.0a6 (2008-11-30)
==================== ====================
New Features New Features
......
...@@ -45,7 +45,7 @@ SAVEPOINT_SUFFIX = ".spb" ...@@ -45,7 +45,7 @@ SAVEPOINT_SUFFIX = ".spb"
LAYOUT_MARKER = '.layout' LAYOUT_MARKER = '.layout'
LAYOUTS = {} LAYOUTS = {}
valid_modes = 'r', 'w', 'r+', 'a' valid_modes = 'r', 'w', 'r+', 'a', 'c'
# Threading issues: # Threading issues:
# We want to support closing blob files when they are destroyed. # We want to support closing blob files when they are destroyed.
...@@ -119,6 +119,9 @@ class Blob(persistent.Persistent): ...@@ -119,6 +119,9 @@ class Blob(persistent.Persistent):
if mode not in valid_modes: if mode not in valid_modes:
raise ValueError("invalid mode", mode) raise ValueError("invalid mode", mode)
if mode == 'c':
return open(self.committed(), 'rb')
if self.writers: if self.writers:
raise BlobError("Already opened for writing.") raise BlobError("Already opened for writing.")
......
...@@ -978,7 +978,12 @@ class IBlob(Interface): ...@@ -978,7 +978,12 @@ class IBlob(Interface):
Returns a file(-like) object for handling the blob data. Returns a file(-like) object for handling the blob data.
mode: Mode to open the file with. Possible values: r,w,r+,a mode: Mode to open the file with. Possible values: r,w,r+,a,c
The mode 'c' is similar to 'r', except that an orinary file
object is returned and may be used in a separate transaction
and after the blob's database connection has been closed.
""" """
def committed(): def committed():
......
...@@ -306,6 +306,27 @@ file that can be opened. ...@@ -306,6 +306,27 @@ file that can be opened.
>>> open(blob.committed()).read() >>> open(blob.committed()).read()
"I'm a happy blob." "I'm a happy blob."
We can also read committed data by calling open with a 'c' flag:
>>> f = blob.open('c')
This just returns a regular file object:
>>> type(f)
<type 'file'>
and doesn't prevent us from opening the blob for writing:
>>> blob.open('w').write('x')
>>> blob.open().read()
'x'
>>> f.read()
"I'm a happy blob."
>>> f.close()
>>> transaction.abort()
An exception is raised if we call committed on a blob that has An exception is raised if we call committed on a blob that has
uncommitted changes: uncommitted changes:
...@@ -315,6 +336,11 @@ uncommitted changes: ...@@ -315,6 +336,11 @@ uncommitted changes:
... ...
BlobError: Uncommitted changes BlobError: Uncommitted changes
>>> blob.open('c')
Traceback (most recent call last):
...
BlobError: Uncommitted changes
>>> blob.open('w').write("I'm a happy blob.") >>> blob.open('w').write("I'm a happy blob.")
>>> root6['blob6'] = blob >>> root6['blob6'] = blob
>>> blob.committed() >>> blob.committed()
...@@ -322,12 +348,22 @@ uncommitted changes: ...@@ -322,12 +348,22 @@ uncommitted changes:
... ...
BlobError: Uncommitted changes BlobError: Uncommitted changes
>>> blob.open('c')
Traceback (most recent call last):
...
BlobError: Uncommitted changes
>>> s = transaction.savepoint() >>> s = transaction.savepoint()
>>> blob.committed() >>> blob.committed()
Traceback (most recent call last): Traceback (most recent call last):
... ...
BlobError: Uncommitted changes BlobError: Uncommitted changes
>>> blob.open('c')
Traceback (most recent call last):
...
BlobError: Uncommitted changes
>>> transaction.commit() >>> transaction.commit()
>>> open(blob.committed()).read() >>> open(blob.committed()).read()
"I'm a happy blob." "I'm a happy blob."
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment