summaryrefslogtreecommitdiff
path: root/addons/document/nodes.py
diff options
context:
space:
mode:
authorP. Christeas <p_christ@hol.gr>2010-11-03 13:25:35 +0200
committerP. Christeas <p_christ@hol.gr>2010-11-03 13:25:35 +0200
commit169b716aa4541b2cdfd497de9e0bc2c70d38ba99 (patch)
tree29efa2d65e3381e5387270d66a3df25e13538610 /addons/document/nodes.py
parent1c5c26a8afaed89d83fcaaa5aa6fb9a31d26a6c8 (diff)
document: a class of static content node_descriptor, API
bzr revid: p_christ@hol.gr-20101103112535-e24cqzamf1w42219
Diffstat (limited to 'addons/document/nodes.py')
-rw-r--r--addons/document/nodes.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/addons/document/nodes.py b/addons/document/nodes.py
index 1a903614028..84eff0ecf64 100644
--- a/addons/document/nodes.py
+++ b/addons/document/nodes.py
@@ -1460,4 +1460,56 @@ class nodefd_content(StringIO, node_descriptor):
cr.close()
StringIO.close(self)
+class nodefd_static(StringIO, node_descriptor):
+ """ A descriptor to nodes with static data.
+ """
+ def __init__(self, parent, cr, mode, ctx=None):
+ node_descriptor.__init__(self, parent)
+ self._context=ctx
+ self._size = 0L
+
+ if mode in ('r', 'r+'):
+ data = parent.get_data(cr)
+ if data:
+ self._size = len(data)
+ parent.content_length = len(data)
+ StringIO.__init__(self, data)
+ elif mode in ('w', 'w+'):
+ StringIO.__init__(self, None)
+ # at write, we start at 0 (= overwrite), but have the original
+ # data available, in case of a seek()
+ elif mode == 'a':
+ StringIO.__init__(self, None)
+ else:
+ logging.getLogger('document.nodes').error("Incorrect mode %s specified", mode)
+ raise IOError(errno.EINVAL, "Invalid file mode")
+ self.mode = mode
+
+ def size(self):
+ return self._size
+
+ def close(self):
+ # we now open a *separate* cursor, to update the data.
+ # FIXME: this may be improved, for concurrency handling
+ if self.mode == 'r':
+ StringIO.close(self)
+ return
+
+ par = self._get_parent()
+ uid = par.context.uid
+ cr = pooler.get_db(par.context.dbname).cursor()
+ try:
+ if self.mode in ('w', 'w+', 'r+'):
+ data = self.getvalue()
+ par.set_data(cr, data)
+ elif self.mode == 'a':
+ raise NotImplementedError
+ cr.commit()
+ except Exception:
+ logging.getLogger('document.nodes').exception('Cannot update db content #%d for close:', par.cnt_id)
+ raise
+ finally:
+ cr.close()
+ StringIO.close(self)
+
#eof \ No newline at end of file