def determine_cache_directory(entity): """Uses the properties of the Entity to determine where it would be cached by default.""" if is_locationable(entity): return os.path.join(CACHE_DIR, entity['id'], str(entity['versionNumber'])) fileHandle = entity['dataFileHandleId'] return os.path.join(CACHE_DIR, str(int(fileHandle) % CACHE_FANOUT), fileHandle)
def determine_cache_directory(entity): """Uses a file handle to determine the cache folder.""" if is_locationable(entity): return os.path.join(CACHE_DIR, entity['id'], str(entity['versionNumber'])) fileHandle = entity['dataFileHandleId'] return os.path.join(CACHE_DIR, str(int(fileHandle) % CACHE_FANOUT), fileHandle)
def determine_local_file_location(entityBundle): """ Uses information from an Entity bundle to derive the cache directory and cached file location. Also returns the first unmodified file in the cache (or None) :param entityBundle: A dictionary with 'fileHandles' and 'entity'. Typically created via:: syn._getEntityBundle() :returns: A 2-tuple (cache directory, file location) File location may be None if there is no file associated with the Entity """ cacheDir = determine_cache_directory(entityBundle['entity']) # Find the first unmodified file if any unmodifiedFile = None for file, cacheTime, fileMTime in iterator_over_cache_map(cacheDir): if fileMTime == cacheTime: unmodifiedFile = file break # Generate and return the default location of the cached file if is_locationable(entityBundle['entity']): if 'locations' not in entityBundle['entity']: # This Locationable does not have an associated file return cacheDir, None, unmodifiedFile url = entityBundle['entity']['locations'][0]['path'] filename = urlparse.urlparse(url).path.split('/')[-1] path = os.path.join(cacheDir, filename) return cacheDir, path, unmodifiedFile else: for handle in entityBundle['fileHandles']: if handle['id'] == entityBundle['entity']['dataFileHandleId']: path = os.path.join(cacheDir, handle['fileName']) return cacheDir, path, unmodifiedFile raise Exception( "Invalid parameters: the entityBundle does not contain matching file handle IDs" )
def determine_local_file_location(entityBundle): """ Uses information from an Entity bundle to derive the cache directory and cached file location. Also returns the first unmodified file in the cache (or None) :param entityBundle: A dictionary with 'fileHandles' and 'entity'. Typically created via:: syn._getEntityBundle() :returns: A 3-tuple (cache directory, default file location, first pristine cached file location) The file locations may be None if there is no file associated with the Entity or cache """ cacheDir = determine_cache_directory(entityBundle['entity']) # Find the first unmodified file if any unmodifiedFile = None for file, cacheTime, fileMTime in iterator_over_cache_map(cacheDir): if fileMTime == cacheTime: unmodifiedFile = file break # Generate and return the default location of the cached file if is_locationable(entityBundle['entity']): if 'locations' not in entityBundle['entity']: # This Locationable does not have an associated file return cacheDir, None, unmodifiedFile url = entityBundle['entity']['locations'][0]['path'] filename = urlparse.urlparse(url).path.split('/')[-1] path = os.path.join(cacheDir, filename) return cacheDir, path, unmodifiedFile else: for handle in entityBundle['fileHandles']: if handle['id'] == entityBundle['entity']['dataFileHandleId']: path = os.path.join(cacheDir, handle['fileName']) return cacheDir, path, unmodifiedFile # Note: fileHandles will be empty if there are unmet access requirements return None, None, None