server/session.py
changeset 8766 db80ffb2f71c
parent 8765 9e9029ba2d4e
child 8767 a75670ef2d87
equal deleted inserted replaced
8765:9e9029ba2d4e 8766:db80ffb2f71c
   176       of None (not yet committing), 'precommit' (calling precommit event on
   176       of None (not yet committing), 'precommit' (calling precommit event on
   177       operations), 'postcommit' (calling postcommit event on operations),
   177       operations), 'postcommit' (calling postcommit event on operations),
   178       'uncommitable' (some :exc:`ValidationError` or :exc:`Unauthorized` error
   178       'uncommitable' (some :exc:`ValidationError` or :exc:`Unauthorized` error
   179       has been raised during the transaction and so it must be rollbacked).
   179       has been raised during the transaction and so it must be rollbacked).
   180 
   180 
       
   181     Hooks controls:
       
   182 
       
   183       :attr:`hooks_mode`, may be either `HOOKS_ALLOW_ALL` or `HOOKS_DENY_ALL`.
       
   184 
       
   185       :attr:`enabled_hook_categories`, when :attr:`hooks_mode` is
       
   186       `HOOKS_DENY_ALL`, this set contains hooks categories that are enabled.
       
   187 
       
   188       :attr:`disabled_hook_categories`, when :attr:`hooks_mode` is
       
   189       `HOOKS_ALLOW_ALL`, this set contains hooks categories that are disabled.
       
   190 
   181     """
   191     """
   182 
   192 
   183     def __init__(self, txid, mode='read'):
   193     def __init__(self, txid, mode='read'):
   184         #: transaction unique id
   194         #: transaction unique id
   185         self.transactionid = txid
   195         self.transactionid = txid
   195         self.transaction_data = {}
   205         self.transaction_data = {}
   196         #: ordered list of operations to be processed on commit/rollback
   206         #: ordered list of operations to be processed on commit/rollback
   197         self.pending_operations = []
   207         self.pending_operations = []
   198         #: (None, 'precommit', 'postcommit', 'uncommitable')
   208         #: (None, 'precommit', 'postcommit', 'uncommitable')
   199         self.commit_state = None
   209         self.commit_state = None
       
   210 
       
   211         ### hook control attribute
       
   212         self.hooks_mode = HOOKS_ALLOW_ALL
       
   213         self.disabled_hook_cats = set()
       
   214         self.enabled_hook_cats = set()
       
   215         self.pruned_hooks_cache = {}
       
   216 
   200 
   217 
   201     def clear(self):
   218     def clear(self):
   202         """reset internal data"""
   219         """reset internal data"""
   203         self.transaction_data = {}
   220         self.transaction_data = {}
   204         #: ordered list of operations to be processed on commit/rollback
   221         #: ordered list of operations to be processed on commit/rollback
   205         self.pending_operations = []
   222         self.pending_operations = []
   206         #: (None, 'precommit', 'postcommit', 'uncommitable')
   223         #: (None, 'precommit', 'postcommit', 'uncommitable')
   207         self.commit_state = None
   224         self.commit_state = None
       
   225         self.pruned_hooks_cache = {}
   208 
   226 
   209 
   227 
   210 class Session(RequestSessionBase):
   228 class Session(RequestSessionBase):
   211     """Repository user session
   229     """Repository user session
   212 
   230 
   686     def deny_all_hooks_but(self, *categories):
   704     def deny_all_hooks_but(self, *categories):
   687         return hooks_control(self, HOOKS_DENY_ALL, *categories)
   705         return hooks_control(self, HOOKS_DENY_ALL, *categories)
   688 
   706 
   689     @property
   707     @property
   690     def hooks_mode(self):
   708     def hooks_mode(self):
   691         return getattr(self._threaddata, 'hooks_mode', self.HOOKS_ALLOW_ALL)
   709         return self._threaddata.hooks_mode
   692 
   710 
   693     def set_hooks_mode(self, mode):
   711     def set_hooks_mode(self, mode):
   694         oldmode = getattr(self._threaddata, 'hooks_mode', self.HOOKS_ALLOW_ALL)
       
   695         assert mode is HOOKS_ALLOW_ALL or mode is HOOKS_DENY_ALL
   712         assert mode is HOOKS_ALLOW_ALL or mode is HOOKS_DENY_ALL
       
   713         oldmode = self._threaddata.hooks_mode
   696         self._threaddata.hooks_mode = mode
   714         self._threaddata.hooks_mode = mode
   697         return oldmode
   715         return oldmode
   698 
   716 
   699     def init_hooks_mode_categories(self, mode, categories):
   717     def init_hooks_mode_categories(self, mode, categories):
   700         oldmode = self.set_hooks_mode(mode)
   718         oldmode = self.set_hooks_mode(mode)
   720             finally:
   738             finally:
   721                 self.set_hooks_mode(oldmode)
   739                 self.set_hooks_mode(oldmode)
   722 
   740 
   723     @property
   741     @property
   724     def disabled_hook_categories(self):
   742     def disabled_hook_categories(self):
   725         try:
   743         return self._threaddata.disabled_hook_cats
   726             return getattr(self._threaddata, 'disabled_hook_cats')
       
   727         except AttributeError:
       
   728             cats = self._threaddata.disabled_hook_cats = set()
       
   729             return cats
       
   730 
   744 
   731     @property
   745     @property
   732     def enabled_hook_categories(self):
   746     def enabled_hook_categories(self):
   733         try:
   747         return self._threaddata.enabled_hook_cats
   734             return getattr(self._threaddata, 'enabled_hook_cats')
       
   735         except AttributeError:
       
   736             cats = self._threaddata.enabled_hook_cats = set()
       
   737             return cats
       
   738 
   748 
   739     def disable_hook_categories(self, *categories):
   749     def disable_hook_categories(self, *categories):
   740         """disable the given hook categories:
   750         """disable the given hook categories:
   741 
   751 
   742         - on HOOKS_DENY_ALL mode, ensure those categories are not enabled
   752         - on HOOKS_DENY_ALL mode, ensure those categories are not enabled
  1007         except AttributeError:
  1017         except AttributeError:
  1008             pass
  1018             pass
  1009 
  1019 
  1010     def _clear_tx_storage(self, txstore):
  1020     def _clear_tx_storage(self, txstore):
  1011         txstore.clear()
  1021         txstore.clear()
  1012         for name in ('_rewriter', 'pruned_hooks_cache'):
  1022         try:
  1013             try:
  1023             del txstore._rewriter
  1014                 delattr(txstore, name)
  1024         except AttributeError:
  1015             except AttributeError:
  1025             pass
  1016                 continue
       
  1017 
  1026 
  1018     def commit(self, free_cnxset=True, reset_pool=None):
  1027     def commit(self, free_cnxset=True, reset_pool=None):
  1019         """commit the current session's transaction"""
  1028         """commit the current session's transaction"""
  1020         if reset_pool is not None:
  1029         if reset_pool is not None:
  1021             warn('[3.13] use free_cnxset argument instead for reset_pool',
  1030             warn('[3.13] use free_cnxset argument instead for reset_pool',
  1173     def pending_operations(self):
  1182     def pending_operations(self):
  1174         return self._threaddata.pending_operations
  1183         return self._threaddata.pending_operations
  1175 
  1184 
  1176     @property
  1185     @property
  1177     def pruned_hooks_cache(self):
  1186     def pruned_hooks_cache(self):
  1178         try:
  1187         return self._threaddata.pruned_hooks_cache
  1179             return self._threaddata.pruned_hooks_cache
       
  1180         except AttributeError:
       
  1181             self._threaddata.pruned_hooks_cache = {}
       
  1182             return self._threaddata.pruned_hooks_cache
       
  1183 
  1188 
  1184     def add_operation(self, operation, index=None):
  1189     def add_operation(self, operation, index=None):
  1185         """add an operation"""
  1190         """add an operation"""
  1186         if index is None:
  1191         if index is None:
  1187             self.pending_operations.append(operation)
  1192             self.pending_operations.append(operation)