Skip to content

Commit

Permalink
Merge pull request #234 from cleanlab/fix/filenotfound-handler
Browse files Browse the repository at this point in the history
Fix/filenotfound handler
  • Loading branch information
kat-wicks authored Jun 18, 2024
2 parents 6905042 + 8dcf052 commit f38a984
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
3 changes: 0 additions & 3 deletions cleanlab_studio/cli/dataset/upload.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pathlib
from typing import cast, List, Optional
import os

from cleanlab_studio.internal.types import SchemaOverride
from cleanlab_studio.internal.upload_helpers import upload_dataset
Expand Down Expand Up @@ -42,8 +41,6 @@ def upload(

if filepath is None:
filepath = click_helpers.prompt_for_filepath("Specify your dataset filepath")
if not os.path.exists(filepath):
abort(f"cannot upload '{filepath}': no such file or directory")

dataset_source = FilepathDatasetSource(filepath=pathlib.Path(filepath))

Expand Down
9 changes: 9 additions & 0 deletions cleanlab_studio/errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from asyncio import Handle
import pathlib
from typing import Union


class HandledError(Exception):
Expand Down Expand Up @@ -139,3 +141,10 @@ def __str__(self) -> str:

class InvalidProjectConfiguration(HandledError):
pass


class InvalidFilepathError(HandledError):
def __init__(self, filepath: Union[str, pathlib.Path] = "") -> None:
if isinstance(filepath, pathlib.Path):
filepath = str(filepath)
super().__init__(f"File could not be found at {filepath}. Please check the file path.")
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import mimetypes
import pathlib
from typing import Any, Optional
import os

from .dataset_source import DatasetSource
from cleanlab_studio.errors import InvalidDatasetError
from cleanlab_studio.errors import InvalidDatasetError, InvalidFilepathError


class FilepathDatasetSource(DatasetSource):
Expand All @@ -15,6 +16,9 @@ def __init__(
**kwargs: Any,
):
super().__init__(*args, **kwargs)
if not os.path.exists(filepath):
raise InvalidFilepathError(filepath=filepath)

self.dataset_name = dataset_name if dataset_name is not None else filepath.name
self.file_size = filepath.stat().st_size
maybe_file_type = mimetypes.guess_type(filepath)[0]
Expand Down

0 comments on commit f38a984

Please sign in to comment.