1111
1212
1313class FileHandler :
14+ """Handle file operations for reading and writing."""
15+
1416 def __init__ (self , file_path : str ) -> None :
1517 self .file_path = self ._get_file_path (file_path )
1618
1719 def is_handler_of_file (self , name : str ) -> bool :
20+ """Check if this handler manages the specified file."""
1821 return self ._get_file_path (name ) == self .file_path
1922
2023 def read (self ) -> str :
24+ """Read file content as string."""
2125 self ._raise_for_file_exists ()
2226
2327 content = self .file_path .read_text ()
@@ -27,30 +31,35 @@ def read(self) -> str:
2731
2832 @contextmanager
2933 def open (self , mode = "r" ) -> Iterator [TextIO ]:
34+ """Open file with context manager."""
3035 self ._raise_for_file_exists ()
3136
3237 with self .file_path .open (mode ) as fp :
3338 yield fp
3439 logger .debug ("Successfully read content from local dependencies file" )
3540
3641 def exists (self ) -> bool :
42+ """Check if file exists and is a valid file."""
3743 try :
3844 self ._raise_for_file_exists ()
3945 except (PathNotFoundError , PathIsNotFileError ):
4046 return False
4147 return True
4248
4349 def _raise_for_file_exists (self ) -> None :
50+ """Raise appropriate exception if file doesn't exist or isn't a file."""
4451 if not self .file_path .exists ():
4552 raise PathNotFoundError
4653
4754 if not self .file_path .is_file ():
4855 raise PathIsNotFileError
4956
5057 def write (self , data : str ) -> None :
58+ """Write data to file."""
5159 self .file_path .write_text (data )
5260
5361 def delete (self , delete_parent_dir : bool = False ) -> None :
62+ """Delete file and optionally its parent directory."""
5463 if not self .exists ():
5564 logger .info ("File does not exist, nothing to delete" )
5665 return
@@ -68,4 +77,5 @@ def delete(self, delete_parent_dir: bool = False) -> None:
6877 )
6978
7079 def _get_file_path (self , file_path : str ) -> Path :
80+ """Convert string path to absolute Path object."""
7181 return Path (os .path .abspath (os .path .join (os .getcwd (), file_path )))
0 commit comments