-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[gaarf-py] Add support for remote writes for CSV and JSON writers
Change-Id: I76c0b3bff5051dff1d314426b4361468ea188f99
- Loading branch information
Showing
6 changed files
with
163 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Module for writing data to a file.""" | ||
|
||
import os | ||
|
||
from gaarf.io.writers.abs_writer import AbsWriter | ||
|
||
|
||
class FileWriter(AbsWriter): | ||
"""Writes Gaarf Report to a local or remote file. | ||
Attributes: | ||
destination_folder: Destination where output file is stored. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
destination_folder: str | os.PathLike = os.getcwd(), | ||
**kwargs: str, | ||
) -> None: | ||
"""Initializes FileWriter based on destination folder.""" | ||
super().__init__(**kwargs) | ||
self.destination_folder = str(destination_folder) | ||
|
||
def create_dir(self) -> None: | ||
"""Creates folders if needed or destination is not remote.""" | ||
if ( | ||
not os.path.isdir(self.destination_folder) | ||
and '://' not in self.destination_folder | ||
): | ||
os.makedirs(self.destination_folder) | ||
|
||
def write(self) -> None: | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import pathlib | ||
|
||
import pytest | ||
|
||
from gaarf.io.writers import file_writer | ||
|
||
|
||
class TestFileWriter: | ||
def test_create_dir_from_local_path_creates_folder(self, tmp_path): | ||
destination_folder = tmp_path / 'destination_folder' | ||
writer = file_writer.FileWriter(destination_folder=destination_folder) | ||
writer.create_dir() | ||
assert destination_folder.is_dir() | ||
|
||
def test_create_dir_from_remote_path_does_not_create_folder(self): | ||
destination_folder = 'gs://fake-bucket' | ||
writer = file_writer.FileWriter(destination_folder=destination_folder) | ||
writer.create_dir() | ||
expected_path = pathlib.Path(destination_folder) | ||
assert not expected_path.is_dir() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters