kevinjqliu commented on code in PR #4380:
URL: https://github.com/apache/iceberg-python/pull/4380#discussion_r4633167705


##########
pyiceberg/encryption/io.py:
##########
@@ -48,3 +50,55 @@ def open(self, seekable: bool = True) -> InputStream:
     def _decrypt(self) -> bytes:
         with self._input_file.open(seekable=False) as stream:
             return _decrypt_ags1(stream.read(), self._key_metadata)
+
+
+class EncryptedOutputFile(OutputFile):
+    """Encrypts a file, such as a manifest, into an AGS1 stream when its 
output stream is closed."""
+
+    def __init__(self, output_file: OutputFile, key_metadata: 
StandardKeyMetadata) -> None:
+        super().__init__(output_file.location)
+        self._output_file = output_file
+        self._key_metadata = key_metadata
+        self._file_length: int | None = None
+
+    @property
+    def key_metadata(self) -> StandardKeyMetadata:
+        """The key metadata to record for the file, including the length of 
the written file."""
+        if self._file_length is None:
+            raise ValueError(f"Cannot get key metadata before the file is 
written: {self.location}")
+        return replace(self._key_metadata, file_length=self._file_length)

Review Comment:
   note that we proactively update the `_key_metadata`'s file_length after 
writing



##########
pyiceberg/encryption/io.py:
##########
@@ -48,3 +50,55 @@ def open(self, seekable: bool = True) -> InputStream:
     def _decrypt(self) -> bytes:
         with self._input_file.open(seekable=False) as stream:
             return _decrypt_ags1(stream.read(), self._key_metadata)
+
+
+class EncryptedOutputFile(OutputFile):
+    """Encrypts a file, such as a manifest, into an AGS1 stream when its 
output stream is closed."""
+
+    def __init__(self, output_file: OutputFile, key_metadata: 
StandardKeyMetadata) -> None:
+        super().__init__(output_file.location)
+        self._output_file = output_file
+        self._key_metadata = key_metadata
+        self._file_length: int | None = None
+
+    @property
+    def key_metadata(self) -> StandardKeyMetadata:
+        """The key metadata to record for the file, including the length of 
the written file."""
+        if self._file_length is None:
+            raise ValueError(f"Cannot get key metadata before the file is 
written: {self.location}")
+        return replace(self._key_metadata, file_length=self._file_length)
+
+    def __len__(self) -> int:
+        """Return the length of the encrypted file, in bytes."""
+        return self._file_length if self._file_length is not None else 
len(self._output_file)
+
+    def exists(self) -> bool:
+        """Check whether the file exists."""
+        return self._output_file.exists()
+
+    def to_input_file(self) -> InputFile:
+        """Return an input file that decrypts the written file."""
+        return EncryptedInputFile(self._output_file.to_input_file(), 
self.key_metadata)
+
+    def create(self, overwrite: bool = False) -> OutputStream:
+        """Return a stream that buffers the file, then encrypts and writes it 
when closed."""
+        return 
_EncryptingOutputStream(self._output_file.create(overwrite=overwrite), 
self._key_metadata, self._set_file_length)
+
+    def _set_file_length(self, file_length: int) -> None:
+        self._file_length = file_length

Review Comment:
   `_file_length` is a callback, passed to `_EncryptingOutputStream`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to