1adc62f51695d523c139aefab0ab4a6226567de0
[hooke.git] / hooke / util / util.py
1 # Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 class Closing (object):
20     """Add .__enter__() .__exit__() for `with` statements.
21
22     See :pep:`343`.
23     """
24     def __init__(self, obj):
25         self.obj = obj
26
27     def __enter__(self):
28         return self.obj
29
30     def __exit__(self, *exc_info):
31         try:
32             close_it = self.obj.close
33         except AttributeError:
34             pass
35         else:
36             close_it()