in neuron_explainer/file_utils.py [0:0]
def __enter__(self) -> IOBase:
assert not self.filepath.startswith("az://"), "Azure blob storage is not supported"
if self.filepath.startswith("https://"):
assert self.mode in ["r", "rb"], "Only read mode is supported for remote files"
remote_data = urllib.request.urlopen(self.filepath)
if "b" in self.mode:
# Read the content into a BytesIO object for binary mode
self.file = io.BytesIO(remote_data.read())
else:
# Decode the content and use StringIO for text mode (less common for torch.load)
self.file = io.StringIO(remote_data.read().decode())
else:
# Create the subdirectories if they don't exist
directory = os.path.dirname(self.filepath)
os.makedirs(directory, exist_ok=True)
self.file = open(self.filepath, self.mode)
if "b" in self.mode:
# Ensure the file is seekable; if not, read into a BytesIO object
try:
self.file.seek(0)
except io.UnsupportedOperation:
self.file.close()
with open(self.filepath, self.mode) as f:
self.file = io.BytesIO(f.read())
return self.file