diff --git a/robotools/evotools/test_worklist.py b/robotools/evotools/test_worklist.py index 8a58f69..22e997b 100644 --- a/robotools/evotools/test_worklist.py +++ b/robotools/evotools/test_worklist.py @@ -1,6 +1,7 @@ import logging import os import tempfile +from pathlib import Path import numpy as np import pytest @@ -268,6 +269,17 @@ def test_aspirate_systemliquid(self) -> None: assert wl[-1] == "A;Systemliquid;;;1;;200.00;;;;" return + def test_accepts_path(self): + fp = Path(tempfile.gettempdir(), os.urandom(24).hex() + ".gwl") + try: + with Worklist(fp) as wl: + wl.comment("Test") + assert isinstance(wl._filepath, Path) + assert fp.exists() + finally: + fp.unlink(missing_ok=True) + return + def test_save(self) -> None: tf = tempfile.mktemp() + ".gwl" error = None diff --git a/robotools/evotools/worklist.py b/robotools/evotools/worklist.py index 5e46d7a..90810fa 100644 --- a/robotools/evotools/worklist.py +++ b/robotools/evotools/worklist.py @@ -3,9 +3,9 @@ import collections import logging import math -import os import typing import warnings +from pathlib import Path from typing import Dict, List, Optional, Tuple, Union import numpy @@ -277,7 +277,7 @@ class Worklist(list): def __init__( self, - filepath: Optional[str] = None, + filepath: Optional[Union[str, Path]] = None, max_volume: typing.Union[int, float] = 950, auto_split: bool = True, ) -> None: @@ -285,7 +285,7 @@ def __init__( Parameters ---------- - filepath : str + filepath Optional filename/filepath to write when the context is exited (must include a .gwl extension) max_volume : int Maximum aspiration volume in µL @@ -293,7 +293,9 @@ def __init__( If `True`, large volumes in transfer operations are automatically splitted. If set to `False`, `InvalidOperationError` is raised when a pipetting volume exceeds `max_volume`. """ - self._filepath = filepath + self._filepath: Optional[Path] = None + if filepath is not None: + self._filepath = Path(filepath) if max_volume is None: raise ValueError("The `max_volume` parameter is required.") self.max_volume = max_volume @@ -309,17 +311,17 @@ def __exit__(self, exc_type, exc_val, exc_tb) -> None: self.save(self._filepath) return - def save(self, filepath: str) -> None: + def save(self, filepath: Union[str, Path]) -> None: """Writes the worklist to the filepath. Parameters ---------- - filepath : str + filepath File name or path to write (must include a .gwl extension) """ - assert ".gwl" in filepath.lower(), "The filename did not contain the .gwl extension." - if os.path.exists(filepath): - os.remove(filepath) + filepath = Path(filepath) + assert ".gwl" in filepath.name.lower(), "The filename did not contain the .gwl extension." + filepath.unlink(missing_ok=True) with open(filepath, "w", newline="\r\n", encoding="latin_1") as file: file.write("\n".join(self)) return