server/hook.py
branchstable
changeset 5538 752bc67064f2
parent 5509 205e708dd5db
child 5744 6b2a987370e8
equal deleted inserted replaced
5537:2889091bd1bf 5538:752bc67064f2
   471     def config(self):
   471     def config(self):
   472         return self.session.repo.config
   472         return self.session.repo.config
   473 
   473 
   474 set_log_methods(Operation, getLogger('cubicweb.session'))
   474 set_log_methods(Operation, getLogger('cubicweb.session'))
   475 
   475 
   476 
   476 def _container_add(container, value):
   477 def set_operation(session, datakey, value, opcls, **opkwargs):
   477     {set: set.add, list: list.append}[container.__class__](container, value)
       
   478 
       
   479 def set_operation(session, datakey, value, opcls, containercls=set, **opkwargs):
   478     """Search for session.transaction_data[`datakey`] (expected to be a set):
   480     """Search for session.transaction_data[`datakey`] (expected to be a set):
   479 
   481 
   480     * if found, simply append `value`
   482     * if found, simply append `value`
   481 
   483 
   482     * else, initialize it to set([`value`]) and instantiate the given `opcls`
   484     * else, initialize it to containercls([`value`]) and instantiate the given
   483       operation class with additional keyword arguments.
   485       `opcls` operation class with additional keyword arguments. `containercls`
       
   486       is a set by default. Give `list` if you want to keep arrival ordering.
   484 
   487 
   485     You should use this instead of creating on operation for each `value`,
   488     You should use this instead of creating on operation for each `value`,
   486     since handling operations becomes coslty on massive data import.
   489     since handling operations becomes coslty on massive data import.
   487     """
   490     """
   488     try:
   491     try:
   489         session.transaction_data[datakey].add(value)
   492         _container_add(session.transaction_data[datakey], value)
   490     except KeyError:
   493     except KeyError:
   491         opcls(session, **opkwargs)
   494         opcls(session, **opkwargs)
   492         session.transaction_data[datakey] = set((value,))
   495         session.transaction_data[datakey] = containercls()
       
   496         _container_add(session.transaction_data[datakey], value)
   493 
   497 
   494 
   498 
   495 class LateOperation(Operation):
   499 class LateOperation(Operation):
   496     """special operation which should be called after all possible (ie non late)
   500     """special operation which should be called after all possible (ie non late)
   497     operations
   501     operations