openspg/python/knext/component/builder/source_reader.py

48 lines
1.5 KiB
Python
Raw Normal View History

2023-12-06 17:26:39 +08:00
from abc import ABC
from typing import Union, List, Dict
from knext import rest
2023-12-08 11:25:26 +08:00
from knext.component.builder.base import SourceReader
2023-12-06 17:26:39 +08:00
class CsvSourceReader(SourceReader):
"""A source component that reading data from CSV file.
Args:
local_path: The local path of CSV file.
columns: The column names that need to be read from the CSV file.
start_row: The starting number of rows read from the CSV file.
If the CSV file includes a header, it needs to be greater than or equal to 2.
Examples:
source = SourceCsvComponent(
local_path="./builder/job/data/App.csv",
columns=["id", 'riskMark', 'useCert'],
start_row=2
)
"""
"""The local path of CSV file."""
local_path: str
"""The column names that need to be read from the CSV file."""
columns: List[str]
"""The starting number of rows read from the CSV file.
If the CSV file includes a header, it needs to be greater than or equal to 2."""
start_row: int
2023-12-08 11:25:26 +08:00
def invoke(self, input):
2023-12-06 17:26:39 +08:00
pass
def submit(self):
pass
def to_rest(self):
"""Transforms `SourceCsvComponent` to REST model `CsvSourceNodeConfig`."""
config = rest.CsvSourceNodeConfig(
start_row=self.start_row, url=self.local_path, columns=self.columns
)
return rest.Node(**super().to_dict(), node_config=config)
@classmethod
def from_rest(cls, node: rest.Node):
return cls()