Path Validators¶
Validator ¶
PathValidationError ¶
Bases: ValidationError
Represents a specialized exception raised for path validation errors.
PathValidationError is raised when a path validator (Validator subclass) fails to validate
a given path(s). Unlike in OSError, the errno is defined as an instance attribute rather
than a class attribute. This is to indicate that each path validation is tied to that path only.
Attributes:
| Name | Type | Description |
|---|---|---|
errno |
Optional[int]
|
The erOSError number associated with the path validation error. |
Source code in src/properpath/validators/validators.py
PathWriteValidator ¶
Bases: Validator
PathWriteValidator does validation for given paths to make sure they are writable.
PathWriteValidator constructor accepts a single path instance or an iterable of path instances.
If the given path is a file (kind == "file"), PathWriteValidator will create the file
(if it doesn't already exist), then write a control character to the file and delete the character immediately after.
If all this passes without errors, the file is considered to be successfully validated and is returned.
If a directory is given, a temporary file is created inside the directory to validate write
permissions. If an iterable of paths is given, the first path that passes validation is returned.
Example
Source code in src/properpath/validators/validators.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
__init__ ¶
__init__(path: Union[Iterable[str | ProperPath | Path], Union[str, ProperPath, Path]], retain_created_file: bool = True, err_logger: Optional[Logger] = None)
Initializes the class with the provided paths, an option to retain the created temporary files, and an optional error logger. The instance maintains these parameters throughout its lifecycle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[Iterable[str | ProperPath | Path], Union[str, ProperPath, Path]]
|
An iterable or individual value representing paths. It can include strings, ProperPath objects, or Path objects. |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
retain_created_file |
bool
|
Flag indicating whether created files (if the given file path already
doesn't exist) during validation should be retained. Defaults to |
err_logger |
Optional[Logger]
|
An optional Logger object used for logging errors. If not provided, a default error logger will be set. |
Source code in src/properpath/validators/validators.py
validate ¶
Returns:
| Type | Description |
|---|---|
ProperPath
|
A validated |
Raises:
| Type | Description |
|---|---|
PathValidationError
|
If no path from the given list of paths can be validated. |