Kpath
File: filesystem/kpath.py
The pathlib.Path class is commonly used for file and directory operations in Python. To provide a consistent interface for file system operations, that does not use exceptions, a wrapper class is created called KPath. This class wraps the pathlib.Path class and provides methods for file and directory operations, such as creating directories, reading and writing files, and checking file existence. The KPath class also provides a way to set file permissions using the FilesystemModes enum.
The interface of the KPath class is kept very similar to the pathlib.Path class to enable a drop-in replacement. Therefore, the migration to the KPath class is straightforward.
The class uses the DCHECK_ functions from the base.log.check
module to do any
argument checks. To disable the checks, the -O flag needs to be set when running
the Python interpreter.
Classes:
-
FilesystemModes
–Enum for file and directory permissions.
-
KPath
–Path class that wraps pathlib.Path and returns status objects instead of raising exceptions.
FilesystemModes
Bases: IntEnum
Enum for file and directory permissions.
Attributes:
-
ALL_RWX
–Full permissions (read, write, execute) for owner, group, and others
-
ALL_RW
–Read and write permissions for owner, group, and others
-
OWNER_RWX_OTHERS_RX
–Owner has full permissions, group and others can read/execute
-
ONLY_OWNER_RWX
–Owner has full permissions, no permissions for group or others
-
OWNER_RW_OTHERS_R
–Owner can read/write, group and others can only read
-
ONLY_OWNER_RW
–Owner can read/write, no permissions for group or others
-
ALL_R
–Read-only permissions for owner, group, and others
-
ONLY_OWNER_R
–Read-only permissions for owner only
-
OWNER_RWX_GROUP_RX
–Owner has full permissions, group can read/execute, others have no permissions
-
OWNER_GROUP_RWX
–Owner and group have full permissions, others have no permissions
Source code in kern_comm_lib/filesystem/kpath.py
ALL_RWX
class-attribute
instance-attribute
Full permissions (read, write, execute) for owner, group, and others
ALL_RW
class-attribute
instance-attribute
Read and write permissions for owner, group, and others
OWNER_RWX_OTHERS_RX
class-attribute
instance-attribute
Owner has full permissions, group and others can read/execute
ONLY_OWNER_RWX
class-attribute
instance-attribute
Owner has full permissions, no permissions for group or others
OWNER_RW_OTHERS_R
class-attribute
instance-attribute
Owner can read/write, group and others can only read
ONLY_OWNER_RW
class-attribute
instance-attribute
Owner can read/write, no permissions for group or others
ALL_R
class-attribute
instance-attribute
Read-only permissions for owner, group, and others
ONLY_OWNER_R
class-attribute
instance-attribute
Read-only permissions for owner only
OWNER_RWX_GROUP_RX
class-attribute
instance-attribute
Owner has full permissions, group can read/execute, others have no permissions
KPath
Path class that wraps pathlib.Path and returns status objects instead of raising exceptions.
Methods:
-
__init__
–Constructor.
-
__str__
–Returns the string representation of the path.
-
__repr__
–Returns the representation of the path.
-
exists
–Checks if the path exists.
-
is_file
–Checks if the path is a file.
-
is_dir
–Checks if the path is a directory.
-
mkdir
–Creates a directory at this path.
-
rmdir
–Removes this directory.
-
touch
–Creates a file at this path.
-
unlink
–Removes this file or symbolic link.
-
read_bytes
–Reads bytes from this file.
-
read_text
–Reads text from this file.
-
write_bytes
–Writes bytes to this file.
-
write_text
–Writes text to this file.
-
iterdir
–Iterates over the files in this directory.
Attributes:
-
path
(Path
) –Gets the underlying pathlib.Path object.
-
name
(str
) –Gets the name of the file or directory.
-
parent
(KPath
) –Gets the parent directory as KPath.
-
suffix
(str
) –Gets the file extension.
-
stem
(str
) –Gets the filename without extension.
Source code in kern_comm_lib/filesystem/kpath.py
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
|
path
property
Gets the underlying pathlib.Path object.
Returns:
-
Path
–The underlying pathlib.Path object.
name
property
Gets the name of the file or directory.
Returns:
-
str
–The name of the file or directory
parent
property
stem
property
Gets the filename without extension.
Returns:
-
str
–The filename without extension.
__init__
Constructor.
Parameters:
-
a_path
(Union[str, Path, KPath]
) –The path to wrap. Can be a string, pathlib.Path, or another KPath object.
Source code in kern_comm_lib/filesystem/kpath.py
__str__
Returns the string representation of the path.
Returns:
-
str
–The string representation of the path.
__repr__
Returns the representation of the path.
Returns:
-
str
–The representation of the path.
exists
Checks if the path exists.
Returns:
-
AStatusOrElse[bool]
–Either True if the path exists or False if it doesn't, otherwise a Status object containing an error status.
Source code in kern_comm_lib/filesystem/kpath.py
is_file
Checks if the path is a file.
Returns:
-
AStatusOrElse[bool]
–Either True if the path is a file or False if it doesn't, otherwise a Status object containing an error status.
Source code in kern_comm_lib/filesystem/kpath.py
is_dir
Checks if the path is a directory.
Returns:
-
AStatusOrElse[bool]
–Either True if the path is a directory or False if it doesn't, otherwise a Status object containing an error status.
Source code in kern_comm_lib/filesystem/kpath.py
mkdir
Creates a directory at this path.
Parameters:
-
mode
(default
, default:511
) –0o777): The mode for the new directory (default is 0o777).
-
parents
(default
, default:False
) –False): If True, create parent directories as needed (default is False).
-
exist_ok
(default
, default:False
) –False): If True, do not raise an error if the directory already exists (default is False).
Returns:
-
Status
–A Status object indicating success or failure.
Source code in kern_comm_lib/filesystem/kpath.py
rmdir
Removes this directory.
Parameters:
-
recursive
(default
, default:False
) –False): If True, removes directory with all its contents. If False, directory must be empty.
Returns:
-
Status
–Status object indicating success or failure.
Source code in kern_comm_lib/filesystem/kpath.py
touch
Creates a file at this path.
Parameters:
-
mode
(int
, default:438
) –The mode for the new file (default is 0o666).
-
exist_ok
(default
, default:True
) –True): If True, do not raise an error if the file already exists.
Returns:
-
Status
–Status object indicating success or failure.
Source code in kern_comm_lib/filesystem/kpath.py
unlink
Removes this file or symbolic link.
Parameters:
-
missing_ok
(default
, default:False
) –False): If True, do not raise an error if the file does not exist.
Returns:
-
Status
–Status object indicating success or failure.
Source code in kern_comm_lib/filesystem/kpath.py
read_bytes
Reads bytes from this file.
Returns:
-
AStatusOrElse[bytes]
–Either a bytes object or a Status object containing an error status.
Source code in kern_comm_lib/filesystem/kpath.py
read_text
Reads text from this file.
Parameters:
-
encoding
(default
, default:'utf-8'
) –utf-8): The encoding to use.
-
errors
(default
, default:None
) –None): How to handle encoding errors.
Returns:
-
AStatusOrElse[str]
–Either a string of text or a Status object containing an error status.
Source code in kern_comm_lib/filesystem/kpath.py
write_bytes
Writes bytes to this file.
Parameters:
-
data
(bytes
) –The data in bytes to write.
Returns:
-
Status
–A Status object indicating success or failure.
Source code in kern_comm_lib/filesystem/kpath.py
write_text
Writes text to this file.
Parameters:
-
data
(str
) –The data as text to write.
-
encoding
(default
, default:'utf-8'
) –utf-8): The encoding to use.
-
errors
(default
, default:None
) –None): How to handle encoding errors.
Returns:
-
Status
–A Status object indicating success or failure.
Source code in kern_comm_lib/filesystem/kpath.py
iterdir
Iterates over the files in this directory.
Returns:
-
AStatusOrElse[list[KPath]]
–Either a Python list of KPath objects or a Status object containing an error status.