Raise ResourceDenied if an exception is raised while the context manager is in effect that matches the specified exception and attributes.
| 39 | |
| 40 | |
| 41 | class TransientResource(object): |
| 42 | |
| 43 | """Raise ResourceDenied if an exception is raised while the context manager |
| 44 | is in effect that matches the specified exception and attributes.""" |
| 45 | |
| 46 | def __init__(self, exc, **kwargs): |
| 47 | self.exc = exc |
| 48 | self.attrs = kwargs |
| 49 | |
| 50 | def __enter__(self): |
| 51 | return self |
| 52 | |
| 53 | def __exit__(self, type_=None, value=None, traceback=None): |
| 54 | """If type_ is a subclass of self.exc and value has attributes matching |
| 55 | self.attrs, raise ResourceDenied. Otherwise let the exception |
| 56 | propagate (if any).""" |
| 57 | if type_ is not None and issubclass(self.exc, type_): |
| 58 | for attr, attr_value in self.attrs.items(): |
| 59 | if not hasattr(value, attr): |
| 60 | break |
| 61 | if getattr(value, attr) != attr_value: |
| 62 | break |
| 63 | else: |
| 64 | raise ResourceDenied("an optional resource is not available") |
| 65 | |
| 66 | # Context managers that raise ResourceDenied when various issues |
| 67 | # with the internet connection manifest themselves as exceptions. |
no outgoing calls
no test coverage detected
searching dependent graphs…