Skip to content

Commit

Permalink
datamodel: types: files: handle PermissionError
Browse files Browse the repository at this point in the history
  • Loading branch information
alesmrazek committed Jan 13, 2025
1 parent 4e3b58d commit 1d07d54
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions python/knot_resolver/datamodel/types/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ class Dir(UncheckedPath):
def __init__(
self, source_value: Any, parents: Tuple["UncheckedPath", ...] = tuple(), object_path: str = "/"
) -> None:
super().__init__(source_value, parents=parents, object_path=object_path)
if self.strict_validation and not self._value.is_dir():
raise ValueError(f"path '{self._value}' does not point to an existing directory")
try:
super().__init__(source_value, parents=parents, object_path=object_path)
if self.strict_validation and not self._value.is_dir():
raise ValueError(f"path '{self._value}' does not point to an existing directory")
except PermissionError as e:
raise ValueError(str(e)) from e


class AbsoluteDir(Dir):
Expand All @@ -118,11 +121,14 @@ class File(UncheckedPath):
def __init__(
self, source_value: Any, parents: Tuple["UncheckedPath", ...] = tuple(), object_path: str = "/"
) -> None:
super().__init__(source_value, parents=parents, object_path=object_path)
if self.strict_validation and not self._value.exists():
raise ValueError(f"file '{self._value}' does not exist")
if self.strict_validation and not self._value.is_file():
raise ValueError(f"path '{self._value}' is not a file")
try:
super().__init__(source_value, parents=parents, object_path=object_path)
if self.strict_validation and not self._value.exists():
raise ValueError(f"file '{self._value}' does not exist")
if self.strict_validation and not self._value.is_file():
raise ValueError(f"path '{self._value}' is not a file")
except PermissionError as e:
raise ValueError(str(e)) from e


class FilePath(UncheckedPath):
Expand All @@ -135,13 +141,15 @@ class FilePath(UncheckedPath):
def __init__(
self, source_value: Any, parents: Tuple["UncheckedPath", ...] = tuple(), object_path: str = "/"
) -> None:
super().__init__(source_value, parents=parents, object_path=object_path)
p = self._value.parent
if self.strict_validation and (not p.exists() or not p.is_dir()):
raise ValueError(f"path '{self._value}' does not point inside an existing directory")

if self.strict_validation and self._value.is_dir():
raise ValueError(f"path '{self._value}' points to a directory when we expected a file")
try:
super().__init__(source_value, parents=parents, object_path=object_path)
p = self._value.parent
if self.strict_validation and (not p.exists() or not p.is_dir()):
raise ValueError(f"path '{self._value}' does not point inside an existing directory")
if self.strict_validation and self._value.is_dir():
raise ValueError(f"path '{self._value}' points to a directory when we expected a file")
except PermissionError as e:
raise ValueError(str(e)) from e


class _PermissionMode(Flag):
Expand Down

0 comments on commit 1d07d54

Please sign in to comment.