Skip to content

WorkingDirectory

autogen.environments.working_directory.WorkingDirectory #

WorkingDirectory(path)

Context manager for changing the current working directory.

Initialize with a directory path.

PARAMETER DESCRIPTION
path

The directory path to change to.

TYPE: str

Source code in autogen/environments/working_directory.py
def __init__(self, path: str):
    """
    Initialize with a directory path.

    Args:
        path: The directory path to change to.
    """
    self.path = path
    self.original_path = None
    self.created_tmp = False
    self._token = None

path instance-attribute #

path = path

original_path instance-attribute #

original_path = None

created_tmp instance-attribute #

created_tmp = False

create_tmp classmethod #

create_tmp()

Create a temporary directory and return a WorkingDirectory instance for it.

Source code in autogen/environments/working_directory.py
@classmethod
def create_tmp(cls):
    """Create a temporary directory and return a WorkingDirectory instance for it."""
    tmp_dir = tempfile.mkdtemp(prefix="ag2_work_dir_")
    instance = cls(tmp_dir)
    instance.created_tmp = True
    return instance

get_current_working_directory classmethod #

get_current_working_directory(working_directory=None)

Get the current working directory or the specified one if provided.

Source code in autogen/environments/working_directory.py
@classmethod
def get_current_working_directory(
    cls, working_directory: Optional["WorkingDirectory"] = None
) -> Optional["WorkingDirectory"]:
    """Get the current working directory or the specified one if provided."""
    if working_directory is not None:
        return working_directory
    try:
        return cls._current_working_directory.get()
    except LookupError:
        return None