Skip to content

Commit 2f41f0d

Browse files
rahul003Vikas-kum
authored andcommitted
Move file to final location on close (#39)
* move file to final location on close * fix for read mode * change modes
1 parent 230e244 commit 2f41f0d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

tornasole_core/access_layer/file.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
from .base import TSAccessBase
22
import os
3+
import shutil
34

45
def ensure_dir(file_path):
56
directory = os.path.dirname(file_path)
67
if directory and not os.path.exists(directory):
78
os.makedirs(directory)
89

10+
11+
WRITE_MODES = ['w', 'w+', 'wb', 'wb+', 'a', 'a+', 'ab', 'ab+']
12+
13+
914
class TSAccessFile(TSAccessBase):
1015
def __init__(self, path, mode):
1116
super().__init__()
1217
self.path = path
1318
self.mode = mode
1419
ensure_dir(path)
15-
self.open(path, mode)
20+
21+
if mode in WRITE_MODES:
22+
self.temp_path = os.path.join('/tmp', self.path)
23+
ensure_dir(self.temp_path)
24+
self.open(self.temp_path, mode)
25+
else:
26+
self.open(self.path, mode)
1627

1728
def open(self, path, mode):
1829
self._accessor = open(path, mode)
@@ -28,13 +39,14 @@ def flush(self):
2839

2940
def close(self):
3041
self._accessor.close()
42+
if self.mode in WRITE_MODES:
43+
shutil.move(self.temp_path, self.path)
3144

3245
def ingest_all(self):
3346
self._data = self._accessor.read()
3447
self._datalen = len(self._data)
3548
self._position = 0
3649

37-
3850
def read(self, n):
3951
assert self._position + n <= self._datalen
4052
res = self._data[self._position:self._position + n]

0 commit comments

Comments
 (0)