Skip to content

Commit

Permalink
add docstring(part)
Browse files Browse the repository at this point in the history
  • Loading branch information
Southpika committed Dec 22, 2023
1 parent 7bdebf9 commit fbec0a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
9 changes: 5 additions & 4 deletions erniebot-agent/src/erniebot_agent/file/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class File(metaclass=abc.ABCMeta):
"""
Abstract base class representing a generic file.
Attributes:
id (str): Unique identifier for the file.
filename (str): File name.
Expand All @@ -36,7 +36,8 @@ class File(metaclass=abc.ABCMeta):
write_contents_to: Asynchronously write the file contents to a local path.
get_file_repr: Return a string representation for use in specific contexts.
to_dict: Convert the File object to a dictionary.
"""
"""

def __init__(
self,
*,
Expand All @@ -49,15 +50,15 @@ def __init__(
) -> None:
"""
Init method for the File class.
Args:
id (str): Unique identifier for the file.
filename (str): File name.
byte_size (int): Size of the file in bytes.
created_at (str): Timestamp indicating the file creation time.
purpose (str): Purpose or use case of the file. []
metadata (Dict[str, Any]): Additional metadata associated with the file.
Returns:
None
"""
Expand Down
36 changes: 14 additions & 22 deletions erniebot-agent/src/erniebot_agent/file/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,34 @@
FilePath: TypeAlias = Union[str, os.PathLike]


class FileManager(object):
class FileManager(Closeable):
"""
Manages files, providing methods for creating, retrieving, and listing files.
Attributes:
registry(FileRegistry): The file registry.
remote_file_client(RemoteFileClient): The remote file client.
save_dir (Optional[FilePath]): Directory for saving local files.
_file_registry (FileRegistry): Registry for keeping track of files.
Methods:
Methods:
create_file_from_path: Create a file from a specified file path.
create_local_file_from_path: Create a local file from a file path.
create_remote_file_from_path: Create a remote file from a file path.
create_file_from_bytes: Create a file from bytes.
retrieve_remote_file_by_id: Retrieve a remote file by its ID.
look_up_file_by_id: Look up a file by its ID.
list_remote_files: List remote files.
_fs_create_file: Create a file in the file system.
_fs_create_temp_dir: Create a temporary directory in the file system.
_clean_up_temp_dir: Clean up a temporary directory.
"""
_remote_file_client: Optional[RemoteFileClient]

_temp_dir: Optional[tempfile.TemporaryDirectory] = None

def __init__(
self,
remote_file_client: Optional[RemoteFileClient] = None,
*,
auto_register: bool = True,
save_dir: Optional[FilePath] = None,
*,
default_file_type: Optional[Literal["local", "remote"]] = None,
prune_on_close: bool = True,
) -> None:
"""
Initialize the FileManager object.
Expand All @@ -81,11 +78,8 @@ def __init__(
"""
super().__init__()
if remote_file_client is not None:
self._remote_file_client = remote_file_client
else:
self._remote_file_client = None
self._auto_register = auto_register

self._remote_file_client = remote_file_client
if save_dir is not None:
self._save_dir = pathlib.Path(save_dir)
else:
Expand Down Expand Up @@ -159,7 +153,7 @@ async def create_file_from_path(
self,
file_path: FilePath,
*,
file_purpose: FilePurpose = "assistants",
file_purpose: protocol.FilePurpose = "assistants",
file_metadata: Optional[Dict[str, Any]] = None,
file_type: Optional[Literal["local", "remote"]] = None,
) -> Union[LocalFile, RemoteFile]:
Expand All @@ -179,12 +173,10 @@ async def create_file_from_path(
ValueError: If an unsupported file type is provided.
"""
self.ensure_not_closed()
file: Union[LocalFile, RemoteFile]
if file_type is None:
if self._remote_file_client is not None:
file_type = "remote"
else:
file_type = "local"
file_type = self._get_default_file_type()
if file_type == "local":
file = await self.create_local_file_from_path(file_path, file_purpose, file_metadata)
elif file_type == "remote":
Expand All @@ -196,7 +188,7 @@ async def create_file_from_path(
async def create_local_file_from_path(
self,
file_path: FilePath,
file_purpose: FilePurpose,
file_purpose: protocol.FilePurpose,
file_metadata: Optional[Dict[str, Any]],
) -> LocalFile:
"""
Expand All @@ -211,7 +203,7 @@ async def create_local_file_from_path(
LocalFile: The created local file.
"""
file = create_local_file_from_path(
file = await self._create_local_file_from_path(
pathlib.Path(file_path),
file_purpose,
file_metadata or {},
Expand Down

0 comments on commit fbec0a0

Please sign in to comment.