Skip to content

Commit

Permalink
Temp file (#20)
Browse files Browse the repository at this point in the history
* temp file
  • Loading branch information
salikhovi4 authored and Ramil Nugmanov committed Mar 26, 2019
1 parent 7bb903e commit 273b8d7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
9 changes: 6 additions & 3 deletions CGRtools/files/RDFrw.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ def __init__(self, file, *args, indexable=False, **kwargs):
if indexable and platform != 'win32' and not self._is_buffer:
self.__file = iter(self._file.readline, '')
if next(self._data):
self._shifts = [int(x.split(b':', 1)[0]) for x in
check_output(['grep', '-boE', r'^\$[RM]FMT', self._file.name]).split()]
self._shifts.append(getsize(self._file.name))
self._shifts = self._load_cache()
if self._shifts is None:
self._shifts = [int(x.split(b':', 1)[0]) for x in
check_output(['grep', '-boE', r'^\$[RM]FMT', self._file.name]).split()]
self._shifts.append(getsize(self._file.name))
self._dump_cache(self._shifts)
else:
self.__file = self._file
next(self._data)
Expand Down
11 changes: 7 additions & 4 deletions CGRtools/files/SDFrw.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ def __init__(self, file, *args, indexable=False, **kwargs):

if indexable and platform != 'win32' and not self._is_buffer:
self.__file = iter(self._file.readline, '')
self._shifts = [0]
for x in BytesIO(check_output(['grep', '-bE', r'\$\$\$\$', self._file.name])):
_pos, _line = x.split(b':', 1)
self._shifts.append(int(_pos) + len(_line))
self._shifts = self._load_cache()
if self._shifts is None:
self._shifts = [0]
for x in BytesIO(check_output(['grep', '-bE', r'\$\$\$\$', self._file.name])):
_pos, _line = x.split(b':', 1)
self._shifts.append(int(_pos) + len(_line))
self._dump_cache(self._shifts)
else:
self.__file = self._file

Expand Down
23 changes: 23 additions & 0 deletions CGRtools/files/_MDLrw.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, see <https://www.gnu.org/licenses/>.
#
from base64 import urlsafe_b64encode
from csv import reader
from logging import warning
from itertools import count, chain, islice
from os.path import abspath, join
from pickle import dump, load, UnpicklingError
from tempfile import gettempdir
from ._CGRrw import CGRwrite, cgr_keys
from ..exceptions import EmptyMolecule
from ..periodictable import common_isotopes
Expand Down Expand Up @@ -435,6 +439,25 @@ def getvalue(self):


class MDLread:
def _load_cache(self):
try:
with open(self.__cache_path, 'rb') as f:
return load(f)
except FileNotFoundError:
return
except IsADirectoryError as e:
raise IsADirectoryError(f'Please delete {self.__cache_path} directory') from e
except (UnpicklingError, EOFError) as e:
raise UnpicklingError(f'Invalid cache file {self.__cache_path}. Please delete it') from e

@property
def __cache_path(self):
return abspath(join(gettempdir(), 'cgrtools_' + urlsafe_b64encode(abspath(self._file.name).encode()).decode()))

def _dump_cache(self, _shifts):
with open(self.__cache_path, 'wb') as f:
dump(_shifts, f)

def read(self):
"""
parse whole file
Expand Down

0 comments on commit 273b8d7

Please sign in to comment.