server/hook.py
branchstable
changeset 5060 ee3b856e1406
parent 5019 72734c210836
child 5067 adc2122eed03
--- a/server/hook.py	Mon Mar 29 13:28:41 2010 +0200
+++ b/server/hook.py	Mon Mar 29 13:34:06 2010 +0200
@@ -450,6 +450,25 @@
 set_log_methods(Operation, getLogger('cubicweb.session'))
 
 
+def set_operation(session, datakey, value, opcls, **opkwargs):
+    """Search for session.transaction_data[`datakey`] (expected to be a set):
+
+    * if found, simply append `value`
+
+    * else, initialize it to set([`value`]) and instantiate the given `opcls`
+      operation class with additional keyword arguments.
+
+    You should use this instead of creating on operation for each `value`,
+    since handling operations becomes coslty on massive data import.
+    """
+    try:
+        session.transaction_data[datakey].add(value)
+    except KeyError:
+        print 'init', datakey
+        opcls(session, *opkwargs)
+        session.transaction_data[datakey] = set((value,))
+
+
 class LateOperation(Operation):
     """special operation which should be called after all possible (ie non late)
     operations