objectstore: make Object.open a private method

Analogous to `_path`, it is not possible to identify the intended
mode of the i/o operation from using `open` (whether it is a read
or a write operation) and thus make it an internal method and only
use it for read operations.
This commit is contained in:
Christian Kellner 2020-02-18 15:42:43 +01:00 committed by Tom Gundersen
parent 3258bb62d4
commit 9b61f50792

View file

@ -59,7 +59,7 @@ class Object:
@property
def treesum(self) -> str:
"""Calculate the treesum of the object"""
with self.open() as fd:
with self._open() as fd:
m = hashlib.sha256()
treesum.treesum(m, fd)
treesum_hash = m.hexdigest()
@ -73,15 +73,6 @@ class Object:
path = self._tree
return path
@contextlib.contextmanager
def open(self):
"""Open the directory and return the file descriptor"""
try:
fd = os.open(self._path, os.O_DIRECTORY)
yield fd
finally:
os.close(fd)
def write(self) -> str:
"""Return a path that can be written to"""
self.init()
@ -111,6 +102,15 @@ class Object:
self._workdir.cleanup()
self._workdir = None
@contextlib.contextmanager
def _open(self):
"""Open the directory and return the file descriptor"""
try:
fd = os.open(self._path, os.O_DIRECTORY)
yield fd
finally:
os.close(fd)
def __enter__(self):
return self