Changeset - 7ae8c359e0b9
[Not reviewed]
0 1 0
Brett Smith - 7 years ago 2017-05-17 16:52:37
brettcsmith@brettcsmith.org
CacheFile opens the file immediately.

This is necessary to work correctly with the error-catching logic of LoaderChain.
1 file changed with 5 insertions and 7 deletions:
0 comments (0 inline, 0 general)
oxrlib/cache.py
Show inline comments
...
 
@@ -3,27 +3,25 @@ import json
 

	
 
from . import errors
 

	
 
class CacheFileBase:
 
    def __init__(self, path, *args, **kwargs):
 
        self.path = path
 
        self.open = functools.partial(path.open, *args, **kwargs)
 
        try:
 
            self.open_file = path.open(*args, **kwargs)
 
        except OSError as error:
 
            self._translate_error(error, 'init')
 

	
 
    def _translate_error(self, error, when):
 
        for orig_type, mapped_type in self.ERRORS_MAP:
 
            if isinstance(error, orig_type):
 
                raise mapped_type(self.path) from error
 
        raise error
 

	
 
    def __enter__(self):
 
        try:
 
            self.open_file = self.open()
 
        except OSError as error:
 
            self._translate_error(error, 'enter')
 
        else:
 
            return self.open_file
 
        return self.open_file
 

	
 
    def __exit__(self, exc_type, exc_value, exc_tb):
 
        try:
 
            self.open_file.close()
 
        except OSError as error:
 
            self._translate_error(error, 'exit')
0 comments (0 inline, 0 general)