59 self.categories = categories |
59 self.categories = categories |
60 |
60 |
61 def __enter__(self): |
61 def __enter__(self): |
62 self.oldmode = self.session.set_hooks_mode(self.mode) |
62 self.oldmode = self.session.set_hooks_mode(self.mode) |
63 if self.mode is self.session.HOOKS_DENY_ALL: |
63 if self.mode is self.session.HOOKS_DENY_ALL: |
64 self.changes = self.session.enable_hooks_category(*self.categories) |
64 self.changes = self.session.enable_hook_categories(*self.categories) |
65 else: |
65 else: |
66 self.changes = self.session.disable_hooks_category(*self.categories) |
66 self.changes = self.session.disable_hook_categories(*self.categories) |
67 |
67 |
68 def __exit__(self, exctype, exc, traceback): |
68 def __exit__(self, exctype, exc, traceback): |
69 if self.changes: |
69 if self.changes: |
70 if self.mode is self.session.HOOKS_DENY_ALL: |
70 if self.mode is self.session.HOOKS_DENY_ALL: |
71 self.session.disable_hooks_category(*self.changes) |
71 self.session.disable_hook_categories(*self.changes) |
72 else: |
72 else: |
73 self.session.enable_hooks_category(*self.changes) |
73 self.session.enable_hook_categories(*self.changes) |
74 self.session.set_hooks_mode(self.oldmode) |
74 self.session.set_hooks_mode(self.oldmode) |
75 |
75 |
76 INDENT = '' |
76 INDENT = '' |
77 class security_enabled(object): |
77 class security_enabled(object): |
78 """context manager to control security w/ session.execute, since by |
78 """context manager to control security w/ session.execute, since by |
375 oldmode = getattr(self._threaddata, 'hooks_mode', self.HOOKS_ALLOW_ALL) |
375 oldmode = getattr(self._threaddata, 'hooks_mode', self.HOOKS_ALLOW_ALL) |
376 self._threaddata.hooks_mode = mode |
376 self._threaddata.hooks_mode = mode |
377 return oldmode |
377 return oldmode |
378 |
378 |
379 @property |
379 @property |
380 def disabled_hooks_categories(self): |
380 def disabled_hook_categories(self): |
381 try: |
381 try: |
382 return getattr(self._threaddata, 'disabled_hooks_cats') |
382 return getattr(self._threaddata, 'disabled_hook_cats') |
383 except AttributeError: |
383 except AttributeError: |
384 cats = self._threaddata.disabled_hooks_cats = set() |
384 cats = self._threaddata.disabled_hook_cats = set() |
385 return cats |
385 return cats |
386 |
386 |
387 @property |
387 @property |
388 def enabled_hooks_categories(self): |
388 def enabled_hook_categories(self): |
389 try: |
389 try: |
390 return getattr(self._threaddata, 'enabled_hooks_cats') |
390 return getattr(self._threaddata, 'enabled_hook_cats') |
391 except AttributeError: |
391 except AttributeError: |
392 cats = self._threaddata.enabled_hooks_cats = set() |
392 cats = self._threaddata.enabled_hook_cats = set() |
393 return cats |
393 return cats |
394 |
394 |
395 def disable_hooks_category(self, *categories): |
395 def disable_hook_categories(self, *categories): |
|
396 """disable the given hook categories: |
|
397 |
|
398 - on HOOKS_DENY_ALL mode, ensure those categories are not enabled |
|
399 - on HOOKS_ALLOW_ALL mode, ensure those categories are disabled |
|
400 """ |
396 changes = set() |
401 changes = set() |
397 if self.hooks_mode is self.HOOKS_DENY_ALL: |
402 if self.hooks_mode is self.HOOKS_DENY_ALL: |
398 enablecats = self.enabled_hooks_categories |
403 enablecats = self.enabled_hook_categories |
399 for category in categories: |
404 for category in categories: |
400 if category in enablecats: |
405 if category in enablecats: |
401 enablecats.remove(category) |
406 enablecats.remove(category) |
402 changes.add(category) |
407 changes.add(category) |
403 else: |
408 else: |
404 disablecats = self.disabled_hooks_categories |
409 disablecats = self.disabled_hook_categories |
405 for category in categories: |
410 for category in categories: |
406 if category not in disablecats: |
411 if category not in disablecats: |
407 disablecats.add(category) |
412 disablecats.add(category) |
408 changes.add(category) |
413 changes.add(category) |
409 return tuple(changes) |
414 return tuple(changes) |
410 |
415 |
411 def enable_hooks_category(self, *categories): |
416 def enable_hook_categories(self, *categories): |
|
417 """enable the given hook categories: |
|
418 |
|
419 - on HOOKS_DENY_ALL mode, ensure those categories are enabled |
|
420 - on HOOKS_ALLOW_ALL mode, ensure those categories are not disabled |
|
421 """ |
412 changes = set() |
422 changes = set() |
413 if self.hooks_mode is self.HOOKS_DENY_ALL: |
423 if self.hooks_mode is self.HOOKS_DENY_ALL: |
414 enablecats = self.enabled_hooks_categories |
424 enablecats = self.enabled_hook_categories |
415 for category in categories: |
425 for category in categories: |
416 if category not in enablecats: |
426 if category not in enablecats: |
417 enablecats.add(category) |
427 enablecats.add(category) |
418 changes.add(category) |
428 changes.add(category) |
419 else: |
429 else: |
420 disablecats = self.disabled_hooks_categories |
430 disablecats = self.disabled_hook_categories |
421 for category in categories: |
431 for category in categories: |
422 if category in self.disabled_hooks_categories: |
432 if category in self.disabled_hook_categories: |
423 disablecats.remove(category) |
433 disablecats.remove(category) |
424 changes.add(category) |
434 changes.add(category) |
425 return tuple(changes) |
435 return tuple(changes) |
426 |
436 |
427 def is_hooks_category_activated(self, category): |
437 def is_hook_category_activated(self, category): |
|
438 """return a boolean telling if the given category is currently activated |
|
439 or not |
|
440 """ |
428 if self.hooks_mode is self.HOOKS_DENY_ALL: |
441 if self.hooks_mode is self.HOOKS_DENY_ALL: |
429 return category in self.enabled_hooks_categories |
442 return category in self.enabled_hook_categories |
430 return category not in self.disabled_hooks_categories |
443 return category not in self.disabled_hook_categories |
431 |
444 |
432 def is_hook_activated(self, hook): |
445 def is_hook_activated(self, hook): |
433 return self.is_hooks_category_activated(hook.category) |
446 """return a boolean telling if the given hook class is currently |
|
447 activated or not |
|
448 """ |
|
449 return self.is_hook_category_activated(hook.category) |
434 |
450 |
435 # connection management ################################################### |
451 # connection management ################################################### |
436 |
452 |
437 def keep_pool_mode(self, mode): |
453 def keep_pool_mode(self, mode): |
438 """set pool_mode, e.g. how the session will keep its pool: |
454 """set pool_mode, e.g. how the session will keep its pool: |