diff --git a/avrokit/url/google.py b/avrokit/url/google.py index ef317fd..f85832c 100644 --- a/avrokit/url/google.py +++ b/avrokit/url/google.py @@ -120,17 +120,12 @@ def open(self) -> IO[Any]: self._current_local = tmpfile self._current_local_stream = self._current_local # Download to file if r/rb mode, or if append mode (to preserve existing content on failure) - if "r" in self.mode or "a" in self.mode: - # N.b. always writes in binary mode + if "r" in self.mode or ("a" in self.mode and blob.exists()): blob.download_to_file(tmpfile) tmpfile.seek(0) - if "a" in self.mode: - tmpfile.seek(0, 2) # Seek to end for append mode - if "b" not in self.mode: - # So if the user wants to read text, we need to decode it - self._current_local_stream = io.TextIOWrapper(tmpfile, encoding="utf-8") - elif ("w" in self.mode or "a" in self.mode) and "b" not in self.mode: - # Same thing when we're writing text + if "a" in self.mode: + tmpfile.seek(0, 2) + if "b" not in self.mode: self._current_local_stream = io.TextIOWrapper(tmpfile, encoding="utf-8") stream = cast(IO[Any], self._current_local_stream) return stream diff --git a/avrokit/url/s3.py b/avrokit/url/s3.py index 2edeee4..1408b1a 100644 --- a/avrokit/url/s3.py +++ b/avrokit/url/s3.py @@ -95,17 +95,12 @@ def open(self) -> IO[Any]: self._current_local = tmpfile self._current_local_stream = self._current_local # Download to file if "r"/"rb" mode, or if append mode (to preserve existing content on failure) - if "r" in self.mode or "a" in self.mode: - # N.b. always writes in binary mode + if "r" in self.mode or ("a" in self.mode and self.exists()): client.download_fileobj(self.bucket, self.path, tmpfile) tmpfile.seek(0) - if "a" in self.mode: - tmpfile.seek(0, 2) # Seek to end for append mode - if "b" not in self.mode: - # So if the user wants to read text, we need to decode it - self._current_local_stream = io.TextIOWrapper(tmpfile, encoding="utf-8") - elif ("w" in self.mode or "a" in self.mode) and "b" not in self.mode: - # Same thing when we're writing text + if "a" in self.mode: + tmpfile.seek(0, 2) + if "b" not in self.mode: self._current_local_stream = io.TextIOWrapper(tmpfile, encoding="utf-8") stream = cast(IO[Any], self._current_local_stream) return stream diff --git a/pyproject.toml b/pyproject.toml index 84ce178..f279b81 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ [tool.poetry] name = "avrokit" -version = "0.0.3" +version = "0.0.4" description = "Python utilities for working with Avro data files" authors = ["Greg Brandt "] license = "Apache-2.0"