Merge branch 'master' into 5197-collection-name-owner-unique
[arvados.git] / sdk / python / arvados / arvfile.py
1 import functools
2
3 class ArvadosFileBase(object):
4     def __init__(self, name, mode):
5         self.name = name
6         self.mode = mode
7         self.closed = False
8
9     @staticmethod
10     def _before_close(orig_func):
11         @functools.wraps(orig_func)
12         def wrapper(self, *args, **kwargs):
13             if self.closed:
14                 raise ValueError("I/O operation on closed stream file")
15             return orig_func(self, *args, **kwargs)
16         return wrapper
17
18     def __enter__(self):
19         return self
20
21     def __exit__(self, exc_type, exc_value, traceback):
22         try:
23             self.close()
24         except Exception:
25             if exc_type is None:
26                 raise
27
28     def close(self):
29         self.closed = True