-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pass path to the csv parameter interface
- Loading branch information
Showing
9 changed files
with
161 additions
and
125 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
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
61 changes: 61 additions & 0 deletions
61
api/src/opentrons/protocols/parameters/csv_parameter_interface.py
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,61 @@ | ||
import csv | ||
from pathlib import Path | ||
from typing import Optional, TextIO, Any, List | ||
|
||
from . import parameter_file_reader | ||
from .exceptions import ParameterValueError | ||
|
||
|
||
# TODO(jbl 2024-08-02) This is a public facing class and as such should be moved to the protocol_api folder | ||
class CSVParameter: | ||
def __init__(self, csv_path: Optional[Path]) -> None: | ||
self._path = csv_path | ||
self._file: Optional[TextIO] = None | ||
self._contents: Optional[str] = None | ||
|
||
@property | ||
def file(self) -> TextIO: | ||
"""Returns the file handler for the CSV file.""" | ||
if self._file is None: | ||
self._file = parameter_file_reader.open_file_path(self._path) | ||
return self._file | ||
|
||
@property | ||
def contents(self) -> str: | ||
"""Returns the full contents of the CSV file as a single string.""" | ||
if self._contents is None: | ||
self.file.seek(0) | ||
self._contents = self.file.read() | ||
return self._contents | ||
|
||
def parse_as_csv( | ||
self, detect_dialect: bool = True, **kwargs: Any | ||
) -> List[List[str]]: | ||
"""Returns a list of rows with each row represented as a list of column elements. | ||
If there is a header for the CSV that will be the first row in the list (i.e. `.rows()[0]`). | ||
All elements will be represented as strings, even if they are numeric in nature. | ||
""" | ||
rows: List[List[str]] = [] | ||
if detect_dialect: | ||
try: | ||
self.file.seek(0) | ||
dialect = csv.Sniffer().sniff(self.file.read(1024)) | ||
self.file.seek(0) | ||
reader = csv.reader(self.file, dialect, **kwargs) | ||
except (UnicodeDecodeError, csv.Error): | ||
raise ParameterValueError( | ||
"Cannot parse dialect or contents from provided CSV file." | ||
) | ||
else: | ||
try: | ||
reader = csv.reader(self.file, **kwargs) | ||
except (UnicodeDecodeError, csv.Error): | ||
raise ParameterValueError("Cannot parse provided CSV file.") | ||
try: | ||
for row in reader: | ||
rows.append(row) | ||
except (UnicodeDecodeError, csv.Error): | ||
raise ParameterValueError("Cannot parse provided CSV file.") | ||
self.file.seek(0) | ||
return rows |
26 changes: 26 additions & 0 deletions
26
api/src/opentrons/protocols/parameters/parameter_file_reader.py
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,26 @@ | ||
from pathlib import Path | ||
from tempfile import NamedTemporaryFile | ||
from typing import Optional, TextIO | ||
|
||
from .exceptions import RuntimeParameterRequired | ||
|
||
|
||
def open_file_path(file_path: Optional[Path]) -> TextIO: | ||
"""Ensure file path is set and open up the file in a safe read-only temporary file.""" | ||
if file_path is None: | ||
raise RuntimeParameterRequired( | ||
"CSV parameter needs to be set to a file for full analysis or run." | ||
) | ||
# Read the contents of the actual file | ||
with file_path.open() as fh: | ||
contents = fh.read() | ||
|
||
# Open a temporary file with write permissions and write contents to that | ||
temporary_file = NamedTemporaryFile("r+") | ||
temporary_file.write(contents) | ||
temporary_file.flush() | ||
|
||
# Open a new file handler for the temporary file with read-only permissions and close the other | ||
read_only_temp_file = open(temporary_file.name, "r") | ||
temporary_file.close() | ||
return read_only_temp_file |
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
Oops, something went wrong.