From 23ee609c50081ea417b253dda5d5bc5fffb00c81 Mon Sep 17 00:00:00 2001 From: Robert Smallshire Date: Sun, 7 Jun 2015 22:12:36 +0200 Subject: [PATCH] Improves handling of SEG Y data loaded from file-like objects without filenames (e.g. BytesIO) in respect of Reader caching --- segpy/reader.py | 15 ++++++++++----- segpy/util.py | 6 ++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/segpy/reader.py b/segpy/reader.py index 4007be6..9eee0d8 100644 --- a/segpy/reader.py +++ b/segpy/reader.py @@ -6,7 +6,7 @@ from segpy import __version__ from segpy.encoding import ASCII from segpy.packer import make_header_packer from segpy.trace_header import TraceHeaderRev1 -from segpy.util import file_length, filename_from_handle, make_sorted_distinct_sequence, hash_for_file +from segpy.util import file_length, filename_from_handle, make_sorted_distinct_sequence, hash_for_file, UNKNOWN_FILENAME from segpy.datatypes import DATA_SAMPLE_FORMAT_TO_SEG_Y_TYPE, SEG_Y_TYPE_DESCRIPTION, SEG_Y_TYPE_TO_CTYPE, size_in_bytes from segpy.toolkit import (extract_revision, bytes_per_sample, @@ -108,7 +108,8 @@ def create_reader(fh, encoding=None, trace_header_format=TraceHeaderRev1, endian sha1 = hash_for_file(fh, encoding, trace_header_format, endian) seg_y_path = filename_from_handle(fh) cache_file_path = _locate_cache_file(seg_y_path, cache_directory, sha1) - reader = _load_reader_from_cache(cache_file_path, seg_y_path) + if cache_file_path is not None: + reader = _load_reader_from_cache(cache_file_path, seg_y_path) if reader is None: reader = _make_reader(fh, encoding, trace_header_format, endian, progress) @@ -131,15 +132,19 @@ def _locate_cache_file(seg_y_path, cache_directory, sha1): sha1: The SHA1 hash corresponding to the file. Returns: - A Path object containing the absolute path of the cache file. + A Path object containing the absolute path of the cache file or None + if the cache file path could not be determined. """ cache_dir_path = Path(cache_directory) cache_filename = (sha1 + '.p') if cache_dir_path.is_absolute(): cache_file_path = cache_dir_path / cache_filename else: - normalized_seg_y_path = Path(seg_y_path).resolve() - cache_file_path = normalized_seg_y_path.parent / cache_directory / cache_filename + if seg_y_path != UNKNOWN_FILENAME: + normalized_seg_y_path = Path(seg_y_path).resolve() + cache_file_path = normalized_seg_y_path.parent / cache_directory / cache_filename + else: + cache_file_path = None return cache_file_path diff --git a/segpy/util.py b/segpy/util.py index 2bc5a1a..01dbac2 100644 --- a/segpy/util.py +++ b/segpy/util.py @@ -6,6 +6,8 @@ import sys from itertools import (islice, cycle, tee, chain, repeat) from segpy.sorted_set import SortedFrozenSet +UNKNOWN_FILENAME = '' + NATIVE_ENDIANNESS = '<' if sys.byteorder == 'little' else '>' EMPTY_BYTE_STRING = b'' @@ -256,13 +258,13 @@ def filename_from_handle(fh): fh: A file-like object. Returns: - A string containing the file name, or '' if it could not + A string containing the file name, or UNKNOWN_FILENAME if it could not be determined. """ try: return fh.name except AttributeError: - return '' + return UNKNOWN_FILENAME def now_millis():