
    Fd                    T    d Z ddlmZ ddlmZ ddlmZ ddlmZ  G d d          Z	dS )	z
=========
Workspace
=========

The workspace is a directory containing configuration and working files for an AutoGPT
agent.

    )annotations)Path)Config)loggerc                      e Zd ZdZg dZddZedd
            Zed             Ze	dd            Z
ddZe	 	 ddd            Zed             Zed d!d            ZdS )"	Workspacez9A class that represents a workspace for an AutoGPT agent.) r	   r	   z\zr	   z%00workspace_root
str | Pathrestrict_to_workspaceboolc                H    |                      |          | _        || _        d S N)_sanitize_path_root_restrict_to_workspace)selfr
   r   s      OC:\Users\Administrator\Downloads\Auto-GPT-master\autogpt\workspace\workspace.py__init__zWorkspace.__init__   s%    ((88
&;###    returnr   c                    | j         S )z$The root directory of the workspace.)r   r   s    r   rootzWorkspace.root   s     zr   c                    | j         S )z5Whether to restrict generated paths to the workspace.)r   r   s    r   r   zWorkspace.restrict_to_workspace    s     **r   workspace_directoryc                ^    |                      |          }|                    dd           |S )a  Create a workspace directory and return the path to it.

        Parameters
        ----------
        workspace_directory
            The path to the workspace directory.

        Returns
        -------
        Path
            The path to the workspace directory.

        T)exist_okparents)r   mkdir)clsr   argskwargss       r   make_workspacezWorkspace.make_workspace%   s8      "001DEE!!4!>>>""r   relative_pathc                F    |                      || j        | j                  S )a  Get the full path for an item in the workspace.

        Parameters
        ----------
        relative_path
            The relative path to resolve in the workspace.

        Returns
        -------
        Path
            The resolved path relative to the workspace.

        )r   restrict_to_root)r   r   r   )r   r%   s     r   get_pathzWorkspace.get_path9   s/     ""!7 # 
 
 	
r   NTr   r'   c                   t           j        D ]3}|t          |           v s|t          |          v rt          d          4|!t	          |                                           S t          j        d|  d| d           t	          |                                          t	          |           } }t          j        d| d           |                                 r+| 	                    |          st          d|  d| d          |
                    |                                           }t          j        d	| d           |r+|	                    |          st          d
| d| d          |S )a^  Resolve the relative path within the given root if possible.

        Parameters
        ----------
        relative_path
            The relative path to resolve.
        root
            The root path to resolve the relative path within.
        restrict_to_root
            Whether to restrict the path to the root.

        Returns
        -------
        Path
            The resolved path.

        Raises
        ------
        ValueError
            If the path is absolute and a root is provided.
        ValueError
            If the path is outside the root and the root is restricted.

        zembedded null byteNzResolving path 'z' in workspace ''zResolved root as 'z#Attempted to access absolute path 'z'.zJoined paths as 'zAttempted to access path 'z' outside of workspace ')r   
NULL_BYTESstr
ValueErrorr   resolver   debugis_absoluteis_relative_tojoinpath)r%   r   r'   	null_byte	full_paths        r   r   zWorkspace._sanitize_pathM   s   D #- 	7 	7IC....)s4yy2H2H !5666 3I <&&..000NNNtNNNOOO"4jj0022D4G4Gm1$111222 $$&& 	}/K/KD/Q/Q 	]m]]UY]]]   MM-0088::	5555666 	I$<$<T$B$B 	XYXXPTXXX   r   c                    |dz  }|                                 sD|                    dd          5 }|                    d           d d d            n# 1 swxY w Y   t          |          | _        d S )Nzfile_logger.txtwzutf-8)modeencodingzFile Operation Logger )existsopenwriter,   file_logger_path)configr   r<   fs       r   build_file_logger_pathz Workspace.build_file_logger_path   s    .1BB&&(( 	2!&&C'&BB 2a01112 2 2 2 2 2 2 2 2 2 2 2 2 2 2"%&6"7"7s   AAAr=   r   r,   c                    |t          t                    j        dz  }nt          |          }t                              |          }t          |          | _        |S )Nauto_gpt_workspace)r   __file__parentr   r$   r,   workspace_path)r=   r   s     r   get_workspace_directoryz!Workspace.get_workspace_directory   s]    &"&x.."7:N"N"&':";"; (667JKK #$7 8 8""r   )r
   r   r   r   )r   r   )r   r   r   r   )r%   r   r   r   )NT)r%   r   r   r   r'   r   r   r   r   )r=   r   r   r,   )__name__
__module____qualname____doc__r+   r   propertyr   r   classmethodr$   r(   staticmethodr   r?   rE    r   r   r   r      s       CC???J< < < <    X + + X+ # # # [#&
 
 
 
(   !%= = = = \=~ 8 8 \8 	# 	# 	# 	# \	# 	# 	#r   r   N)
rI   
__future__r   pathlibr   autogpt.configr   autogpt.logsr   r   rM   r   r   <module>rR      s     # " " " " "       ! ! ! ! ! !      M# M# M# M# M# M# M# M# M# M#r   