Skip to content

Commit 3da4b80

Browse files
committed
add docstrings
1 parent 9884643 commit 3da4b80

File tree

1 file changed

+114
-9
lines changed

1 file changed

+114
-9
lines changed

audiostack/content/file.py

Lines changed: 114 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
import os
22
import time
3-
from typing import Any, Optional
3+
from typing import Optional
44
from uuid import UUID
55

66
from audiostack import TIMEOUT_THRESHOLD_S
7-
from audiostack.helpers.api_item import APIResponseItem
8-
from audiostack.helpers.api_list import APIResponseList
97
from audiostack.helpers.request_interface import RequestInterface
108
from audiostack.helpers.request_types import RequestTypes
119

1210

1311
class File:
12+
"""File management class for handling file operations in AudioStack.
13+
14+
This class provides methods for creating, retrieving, and deleting files
15+
in the AudioStack system.
16+
"""
17+
1418
FAMILY = "v3"
1519
interface = RequestInterface(family=FAMILY)
1620

1721
class Item:
22+
"""Represents a file item in the AudioStack system.
23+
24+
This class encapsulates file metadata and provides methods for
25+
file operations like downloading.
26+
"""
27+
1828
def __init__(self, response: dict) -> None:
29+
"""Initialize a File.Item instance from API response data.
30+
31+
Args:
32+
response: Dictionary containing file metadata from the API.
33+
"""
1934
self.file_id: str = response["fileId"]
2035
self.file_name: str = response["fileName"]
2136
self.url: str = response.get("url", "")
@@ -29,6 +44,15 @@ def __init__(self, response: dict) -> None:
2944
self.duration: Optional[str] = response.get("duration", None)
3045

3146
def download(self, fileName: str, path: str = "./") -> None:
47+
"""Download the file to the specified local path.
48+
49+
Args:
50+
fileName: Name to save the file as locally.
51+
path: Directory path where the file should be saved. Defaults to "./".
52+
53+
Raises:
54+
Exception: If no URL is available for the file.
55+
"""
3256
if not self.url:
3357
raise Exception(
3458
"No URL found for this file. Please check the file has been processed."
@@ -37,6 +61,14 @@ def download(self, fileName: str, path: str = "./") -> None:
3761

3862
@staticmethod
3963
def get(file_id: str) -> Item:
64+
"""Retrieve a file by its ID.
65+
66+
Args:
67+
file_id: The unique identifier of the file to retrieve.
68+
69+
Returns:
70+
File.Item: A file item containing the file metadata.
71+
"""
4072
r = File.interface.send_request(
4173
rtype=RequestTypes.GET,
4274
route=f"file/{file_id}",
@@ -48,13 +80,29 @@ def create(
4880
local_path: str,
4981
file_name: str,
5082
folder_id: Optional[UUID] = None,
51-
category: str = "",
5283
) -> Item:
84+
"""Create and upload a new file to AudioStack.
85+
86+
This method uploads a local file to the AudioStack system and waits
87+
for the upload to complete before returning the file item.
88+
89+
Args:
90+
local_path: Path to the local file to upload.
91+
file_name: Name to assign to the file in AudioStack.
92+
folder_id: Optional UUID of the folder to upload to. If None, uses root folder.
93+
94+
Returns:
95+
File.Item: The created file item with complete metadata.
96+
97+
Raises:
98+
Exception: If local_path is not provided, file doesn't exist, file_name is not provided,
99+
or if the upload fails.
100+
"""
53101
if not local_path:
54102
raise Exception("Please supply a localPath (path to your local file)")
55103

56104
if not os.path.isfile(local_path):
57-
raise Exception("Supplied file does not eixst")
105+
raise Exception("Supplied file does not exist")
58106

59107
if not file_name:
60108
raise Exception("Please supply a valid file name")
@@ -64,9 +112,6 @@ def create(
64112
"folderId": folder_id,
65113
}
66114

67-
if category:
68-
payload["categoryId"] = File.get_category_id_by_name(name=category)
69-
70115
r = File.interface.send_request(
71116
rtype=RequestTypes.POST,
72117
route="file/create-upload-url",
@@ -80,7 +125,7 @@ def create(
80125

81126
file = File.get(file_id=r["fileId"])
82127

83-
while file.status != "uploaded" or time.time() - start >= TIMEOUT_THRESHOLD_S:
128+
while file.status != "uploaded" and time.time() - start < TIMEOUT_THRESHOLD_S:
84129
print("Response in progress please wait...")
85130
file = File.get(file_id=r["fileId"])
86131

@@ -91,6 +136,12 @@ def create(
91136

92137
@staticmethod
93138
def delete(file_id: str, folder_id: str = "") -> None:
139+
"""Delete a file from AudioStack.
140+
141+
Args:
142+
file_id: The unique identifier of the file to delete.
143+
folder_id: The folder ID where the file is located. If empty, uses root folder.
144+
"""
94145
if not folder_id:
95146
folder_id = Folder.get_root_folder_id()
96147

@@ -101,11 +152,27 @@ def delete(file_id: str, folder_id: str = "") -> None:
101152

102153

103154
class Folder:
155+
"""Folder management class for handling folder operations in AudioStack.
156+
157+
This class provides methods for creating, retrieving, and deleting folders
158+
in the AudioStack system.
159+
"""
160+
104161
FAMILY = "v3"
105162
interface = RequestInterface(family=FAMILY)
106163

107164
class Item:
165+
"""Represents a folder item in the AudioStack system.
166+
167+
This class encapsulates folder metadata including ID, name, and hierarchy information.
168+
"""
169+
108170
def __init__(self, response: dict) -> None:
171+
"""Initialize a Folder.Item instance from API response data.
172+
173+
Args:
174+
response: Dictionary containing folder metadata from the API.
175+
"""
109176
self.folder_id: str = response["folderId"]
110177
self.folder_name: str = response["folderName"]
111178
self.parent_folder_id: str = response.get("parentFolderId", "")
@@ -114,7 +181,18 @@ def __init__(self, response: dict) -> None:
114181
self.created_at: str = response.get("createdAt", "")
115182

116183
class ListResponse:
184+
"""Represents a list response containing folders and files.
185+
186+
This class encapsulates the response from folder listing operations,
187+
containing both folder and file items along with path chain information.
188+
"""
189+
117190
def __init__(self, response: dict) -> None:
191+
"""Initialize a ListResponse instance from API response data.
192+
193+
Args:
194+
response: Dictionary containing folder listing data from the API.
195+
"""
118196
self.folders: list[Folder.Item] = [
119197
Folder.Item(response=x) for x in response["folders"]
120198
]
@@ -125,6 +203,11 @@ def __init__(self, response: dict) -> None:
125203

126204
@staticmethod
127205
def get_root_folder_id() -> str:
206+
"""Get the ID of the root folder.
207+
208+
Returns:
209+
str: The unique identifier of the root folder.
210+
"""
128211
root_folder_id = Folder.interface.send_request(
129212
rtype=RequestTypes.GET,
130213
route="folder",
@@ -133,6 +216,15 @@ def get_root_folder_id() -> str:
133216

134217
@staticmethod
135218
def create(name: str, parent_folder_id: Optional[UUID] = None) -> "Folder.Item":
219+
"""Create a new folder in AudioStack.
220+
221+
Args:
222+
name: The name of the folder to create.
223+
parent_folder_id: Optional UUID of the parent folder. If None, creates in root.
224+
225+
Returns:
226+
Folder.Item: The created folder item with complete metadata.
227+
"""
136228
payload = {
137229
"folderName": name,
138230
}
@@ -149,6 +241,14 @@ def create(name: str, parent_folder_id: Optional[UUID] = None) -> "Folder.Item":
149241

150242
@staticmethod
151243
def get(folder_id: UUID) -> "Folder.Item":
244+
"""Retrieve a folder by its ID.
245+
246+
Args:
247+
folder_id: The unique identifier of the folder to retrieve.
248+
249+
Returns:
250+
Folder.Item: A folder item containing the folder metadata.
251+
"""
152252
r = Folder.interface.send_request(
153253
rtype=RequestTypes.GET,
154254
route=f"folder/{folder_id}",
@@ -157,6 +257,11 @@ def get(folder_id: UUID) -> "Folder.Item":
157257

158258
@staticmethod
159259
def delete(folder_id: UUID) -> None:
260+
"""Delete a folder from AudioStack.
261+
262+
Args:
263+
folder_id: The unique identifier of the folder to delete.
264+
"""
160265
File.interface.send_request(
161266
rtype=RequestTypes.DELETE,
162267
route=f"folder/{folder_id}",

0 commit comments

Comments
 (0)