Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions libs/io/garf_io/writers/csv_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
# 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 with CSV."""
"""Writes GarfReport to CSV.

Writing to remote storage systems (gs, s3, hadoop, stfp) is also supported.
"""

from __future__ import annotations

import csv
import logging
import os
from typing import Literal
import pathlib
from typing import Literal, Union

import smart_open
from garf_core import report as garf_report
Expand All @@ -31,15 +35,17 @@ class CsvWriter(file_writer.FileWriter):
"""Writes Garf Report to CSV.

Attributes:
destination_folder: Destination where CSV files are stored.
delimiter: CSV delimiter.
quotechar: CSV writer quotechar.
quoting: CSV writer quoting.
destination_folder: Destination where CSV files are stored.
delimiter: CSV delimiter.
quotechar: CSV writer quotechar.
quoting: CSV writer quoting.
"""

def __init__(
self,
destination_folder: str | os.PathLike = os.getcwd(),
destination_folder: Union[
str, os.PathLike[str], pathlib.Path
] = pathlib.Path.cwd(),
delimiter: str = ',',
quotechar: str = '"',
quoting: Literal[0] = csv.QUOTE_MINIMAL,
Expand Down Expand Up @@ -68,8 +74,8 @@ def write(self, report: garf_report.GarfReport, destination: str) -> str:
"""Writes Garf report to a CSV file.

Args:
report: Garf report.
destination: Base file name report should be written to.
report: Garf report.
destination: Base file name report should be written to.

Returns:
Full path where data are written.
Expand All @@ -78,7 +84,7 @@ def write(self, report: garf_report.GarfReport, destination: str) -> str:
destination = formatter.format_extension(destination, new_extension='.csv')
self.create_dir()
logging.debug('Writing %d rows of data to %s', len(report), destination)
output_path = os.path.join(self.destination_folder, destination)
output_path = pathlib.Path(self.destination_folder) / destination
with smart_open.open(
output_path,
encoding='utf-8',
Expand Down
12 changes: 8 additions & 4 deletions libs/io/garf_io/writers/file_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
# 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."""
"""Module for writing GarfReport to a file."""

import os
import pathlib
from typing import Union

from garf_io.writers.abs_writer import AbsWriter

Expand All @@ -27,7 +29,9 @@ class FileWriter(AbsWriter):

def __init__(
self,
destination_folder: str | os.PathLike = os.getcwd(),
destination_folder: Union[
str, os.PathLike[str], pathlib.Path
] = pathlib.Path.cwd(),
**kwargs: str,
) -> None:
"""Initializes FileWriter based on destination folder."""
Expand All @@ -37,10 +41,10 @@ def __init__(
def create_dir(self) -> None:
"""Creates folders if needed or destination is not remote."""
if (
not os.path.isdir(self.destination_folder)
not pathlib.Path(self.destination_folder).is_dir()
and '://' not in self.destination_folder
):
os.makedirs(self.destination_folder)
pathlib.Path(self.destination_folder).mkdir(parents=True)

def write(self) -> None:
return
11 changes: 7 additions & 4 deletions libs/io/garf_io/writers/json_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

import logging
import os
from typing import Literal
import pathlib
from typing import Literal, Union

import smart_open
from garf_core import report as garf_report
Expand All @@ -30,12 +31,14 @@ class JsonWriter(file_writer.FileWriter):
"""Writes Garf Report to JSON.

Attributes:
destination_folder: Destination where JSON files are stored.
destination_folder: Destination where JSON files are stored.
"""

def __init__(
self,
destination_folder: str | os.PathLike = os.getcwd(),
destination_folder: Union[
str, os.PathLike[str], pathlib.Path
] = pathlib.Path.cwd(),
format: Literal['json', 'jsonl'] = 'json',
**kwargs: str,
) -> None:
Expand Down Expand Up @@ -66,7 +69,7 @@ def write(self, report: garf_report.GarfReport, destination: str) -> str:
)
self.create_dir()
logging.debug('Writing %d rows of data to %s', len(report), destination)
output_path = os.path.join(self.destination_folder, destination)
output_path = pathlib.Path(self.destination_folder) / destination
with smart_open.open(output_path, 'w', encoding='utf-8') as f:
f.write(report.to_json(output=self.format))
logging.debug('Writing to %s is completed', output_path)
Expand Down
Loading