trading_agent.attachments.attachment_store
¶
Keeps the files the user sends with messages, in each desk's uploads folder.
Each upload gets its own folder, named by its identifier, under the India date it arrived:
desks/
A picture is checked to be a real PNG, JPEG, WebP or GIF by its contents. A document is turned into text once, when it arrives, so every model reads the same text and nothing has to be parsed again. The text file starts and ends with the same marker lines that label web pages, because a document can come from anywhere and must not be able to give the desk instructions. Uploads are kept: the desk can come back to a document weeks later.
Typical usage example:
store = AttachmentStore(layout) attachment = store.save('main', 'report.pdf', data) same = store.find('main', attachment.attachment_id)
AttachmentRefusedError
¶
Bases: ValueError
An upload cannot be accepted: it is empty, too large, of a type not taken, or damaged.
AttachmentNotFoundError
¶
Bases: LookupError
No upload has that identifier on that desk.
AttachmentStore
¶
Every desk's uploads.
Creates the store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layout
|
WorkspaceLayout
|
Where the desk folders are. |
required |
image_preparer
|
ImagePreparer | None
|
Checks pictures; None for a new one. |
None
|
readers
|
Sequence[TextReader] | None
|
The document readers; None for PDF, Word, Excel and plain text. |
None
|
untrusted_text
|
UntrustedText | None
|
Labels document text as data; None for a new one. |
None
|
clock
|
Callable[[], datetime] | None
|
Returns the current time; None uses the system clock. Tests replace it. |
None
|
Source code in src/trading_agent/attachments/attachment_store.py
accepted_extensions
¶
Every file ending the store takes.
Returns:
| Type | Description |
|---|---|
list[str]
|
The picture endings, then the document endings. |
Source code in src/trading_agent/attachments/attachment_store.py
save
¶
Stores one upload, and reads a document's text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk it is for. |
required |
file_name
|
str
|
The file's name as the user sent it. |
required |
data
|
bytes
|
The file's contents. |
required |
Returns:
| Type | Description |
|---|---|
Attachment
|
The stored upload. |
Raises:
| Type | Description |
|---|---|
AttachmentRefusedError
|
The file is empty, larger than 20 MB, of a type not taken, or a damaged picture. |
Source code in src/trading_agent/attachments/attachment_store.py
find
¶
Finds one of a desk's uploads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk. |
required |
attachment_id
|
str
|
The upload's identifier. |
required |
Returns:
| Type | Description |
|---|---|
Attachment
|
The upload. |
Raises:
| Type | Description |
|---|---|
AttachmentNotFoundError
|
The desk has no upload with that identifier. |