Module QFlow.utils

Sub-modules

QFlow.utils.genericFile
QFlow.utils.source

Classes

class GenericFile (filepath)
Expand source code
class GenericFile:
    """
    A generic file handler for reading, writing, and deleting files.
    
    This class provides a simple interface for common file operations with
    configurable read/write modes and encoding.
    
    Attributes:
        filepath (str): The path to the file to be managed.
        readType (str): The mode for reading files. Defaults to 'r'.
        encoding (str): The encoding used for file operations. Defaults to 'utf-8'.
        writeType (str): The mode for writing files. Defaults to 'w'.
    """
    
    def __init__(self, filepath) -> None:
        """
        Initializes the file handler with a specific filepath.
        
        Args:
            filepath (str): The path to the file to be managed.
        """
        self.filepath: str = filepath
        self.readType: str = 'r'
        self.encoding: str = 'utf-8'
        self.writeType: str = 'w'

    def readFile(self, lines: bool = False) -> str | list | None:
        """
        Reads the content of the file.
        
        Args:
            lines (bool, optional): If True, returns content as a list of lines.
                If False, returns content as a single string. Defaults to False.
        
        Returns:
            str | list | None: The file content as a string or list of lines,
                depending on the lines parameter.
        
        Raises:
            Exception: If the file is not found or an unexpected error occurs.
        """
        try:
            with open(file=self.filepath, mode=self.readType, encoding=self.encoding) as file:
                return file.read() if not lines else file.readlines()
        except FileNotFoundError:
            raise Exception(f"Error: File '{self.filepath}' does not exist.")
        except Exception as e:
            raise Exception(f'Unexpected error reading file: {e}')

    def writeFile(self, data: str) -> bool:
        """
        Writes data to the file.
        
        Args:
            data (str): The content to write to the file.
        
        Returns:
            bool: True if the write operation was successful.
        
        Raises:
            Exception: If there are permission issues or an unexpected error occurs.
        """
        try:
            with open(file=self.filepath, mode=self.writeType, encoding=self.encoding) as file:
                file.write(data)

            return True
        except PermissionError:
            raise Exception(f"Error: You do not have permissions to write to '{self.filepath}'.")
        except Exception as e:
            raise Exception(f'Unexpected error writing to file: {e}')

    def deleteFile(self) -> bool:
        """
        Deletes the file if it exists.
        
        Returns:
            bool: True if the file was successfully deleted, False if the file
                does not exist.
        """
        if os.path.exists(self.filepath):
            os.remove(self.filepath)
            return True
        
        return False

A generic file handler for reading, writing, and deleting files.

This class provides a simple interface for common file operations with configurable read/write modes and encoding.

Attributes

filepath : str
The path to the file to be managed.
readType : str
The mode for reading files. Defaults to 'r'.
encoding : str
The encoding used for file operations. Defaults to 'utf-8'.
writeType : str
The mode for writing files. Defaults to 'w'.

Initializes the file handler with a specific filepath.

Args

filepath : str
The path to the file to be managed.

Methods

def deleteFile(self) ‑> bool
Expand source code
def deleteFile(self) -> bool:
    """
    Deletes the file if it exists.
    
    Returns:
        bool: True if the file was successfully deleted, False if the file
            does not exist.
    """
    if os.path.exists(self.filepath):
        os.remove(self.filepath)
        return True
    
    return False

Deletes the file if it exists.

Returns

bool
True if the file was successfully deleted, False if the file does not exist.
def readFile(self, lines: bool = False) ‑> str | list | None
Expand source code
def readFile(self, lines: bool = False) -> str | list | None:
    """
    Reads the content of the file.
    
    Args:
        lines (bool, optional): If True, returns content as a list of lines.
            If False, returns content as a single string. Defaults to False.
    
    Returns:
        str | list | None: The file content as a string or list of lines,
            depending on the lines parameter.
    
    Raises:
        Exception: If the file is not found or an unexpected error occurs.
    """
    try:
        with open(file=self.filepath, mode=self.readType, encoding=self.encoding) as file:
            return file.read() if not lines else file.readlines()
    except FileNotFoundError:
        raise Exception(f"Error: File '{self.filepath}' does not exist.")
    except Exception as e:
        raise Exception(f'Unexpected error reading file: {e}')

Reads the content of the file.

Args

lines : bool, optional
If True, returns content as a list of lines. If False, returns content as a single string. Defaults to False.

Returns

str | list | None
The file content as a string or list of lines, depending on the lines parameter.

Raises

Exception
If the file is not found or an unexpected error occurs.
def writeFile(self, data: str) ‑> bool
Expand source code
def writeFile(self, data: str) -> bool:
    """
    Writes data to the file.
    
    Args:
        data (str): The content to write to the file.
    
    Returns:
        bool: True if the write operation was successful.
    
    Raises:
        Exception: If there are permission issues or an unexpected error occurs.
    """
    try:
        with open(file=self.filepath, mode=self.writeType, encoding=self.encoding) as file:
            file.write(data)

        return True
    except PermissionError:
        raise Exception(f"Error: You do not have permissions to write to '{self.filepath}'.")
    except Exception as e:
        raise Exception(f'Unexpected error writing to file: {e}')

Writes data to the file.

Args

data : str
The content to write to the file.

Returns

bool
True if the write operation was successful.

Raises

Exception
If there are permission issues or an unexpected error occurs.
class Source (path: str | pathlib._local.Path, frozen: bool)
Expand source code
class Source:
    """
    A helper class to resolve file paths correctly in both
    development mode and when the application is bundled with PyInstaller.

    This class automatically detects whether the code is running
    in a frozen (PyInstaller) environment or in a normal Python environment,
    and returns the correct absolute path to the requested resource.
    
    All paths are resolved from the project root, just like normal Python behavior.
    """

    def __init__(self, path: str | Path, frozen: bool):
        """
        Initialize a Source object.

        Args:
            path (str | Path): The path of the resource from project root.
                Examples: "QFlow/resources/icon.png", "config/settings.yaml"
        """
        self.frozen = frozen
        self.inputPath = Path(path)
        
        # Check if it's an absolute path
        if self.inputPath.is_absolute():
            self.resolvedPath = str(self.inputPath)
        else:
            # Always resolve from project root
            if self.frozen:
                # In frozen mode, project root is sys._MEIPASS
                projectRoot = Path(sys._MEIPASS)
            else:
                # In dev mode, go up from QFlow package to project root
                packagePath = Path(res.files(QFlow))
                projectRoot = packagePath.parent
            
            self.resolvedPath = str(projectRoot / self.inputPath)

    def get(self) -> str:
        """
        Get the resolved absolute path of the resource.

        Returns:
            str: The absolute path to the requested resource.
        """
        return self.resolvedPath
    
    def exists(self) -> bool:
        """
        Check if the resolved path exists.

        Returns:
            bool: True if the path exists, False otherwise.
        """
        return Path(self.resolvedPath).exists()

A helper class to resolve file paths correctly in both development mode and when the application is bundled with PyInstaller.

This class automatically detects whether the code is running in a frozen (PyInstaller) environment or in a normal Python environment, and returns the correct absolute path to the requested resource.

All paths are resolved from the project root, just like normal Python behavior.

Initialize a Source object.

Args

path : str | Path
The path of the resource from project root. Examples: "QFlow/resources/icon.png", "config/settings.yaml"

Methods

def exists(self) ‑> bool
Expand source code
def exists(self) -> bool:
    """
    Check if the resolved path exists.

    Returns:
        bool: True if the path exists, False otherwise.
    """
    return Path(self.resolvedPath).exists()

Check if the resolved path exists.

Returns

bool
True if the path exists, False otherwise.
def get(self) ‑> str
Expand source code
def get(self) -> str:
    """
    Get the resolved absolute path of the resource.

    Returns:
        str: The absolute path to the requested resource.
    """
    return self.resolvedPath

Get the resolved absolute path of the resource.

Returns

str
The absolute path to the requested resource.