Skip to content

Commit

Permalink
Merge branch 'develop' into chore/pyproject.toml-config
Browse files Browse the repository at this point in the history
  • Loading branch information
ARYAN-NIKNEZHAD authored Aug 20, 2024
2 parents b9313ea + 7fefe08 commit a8066ea
Show file tree
Hide file tree
Showing 20 changed files with 882 additions and 317 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
## Unreleased

### Fix

- **build**: solve versioning problem
- **README.md**: update README.md
- **stubs**: regenerate stubs

### Refactor

- improve docs for client section
- improve client service to handle reqs without ctx

## v0.4.0 (2024-07-17)

### Feat

- **sage_imap**: enhance imap service
- **IMAPMailboxService**: Add .eml file upload functionality to IMAP server
- **sage_imap**: enhance EmailMessage dataclass
- **IMAPMailboxService**: Add .eml file upload functionality to IMAP server

### Fix

Expand Down
74 changes: 6 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
- [Example 3: Working with Mailbox Methods](#example-3-working-with-mailbox-methods)
- [IMAPMailboxService Example](#imapmailboxservice-example)
- [Example Usage with Nested Context Managers:](#example-usage-with-nested-context-managers)
- [Methods of IMAPMailboxService Explained](#methods-of-imapmailboxservice-explained)
- [License](#license)

## Introduction
Expand Down Expand Up @@ -60,6 +59,8 @@ logging.basicConfig(level=logging.DEBUG)

This example demonstrates how to create an IMAP client using the `IMAPClient` class.

The `IMAPClient` class can also be used without a context manager; simply call `connect()` to establish the connection and `disconnect()` to close it

```python
from sage_imap.services import IMAPClient

Expand Down Expand Up @@ -143,84 +144,21 @@ try:
with IMAPClient('imap.example.com', username, password) as client:
with IMAPMailboxService(client) as mailbox:
# Select a mailbox
mailbox.select_mailbox(DefaultMailboxes.INBOX)
mailbox.select(DefaultMailboxes.INBOX)

# Delete messages temporarily (move to trash)
msg_set = MessageSet('1,2,3')
mailbox.delete_temporarily(msg_set)
mailbox.trash(msg_set)

# Restore messages from trash to original folder
mailbox.restore_from_trash(msg_set, DefaultMailboxes.INBOX)
mailbox.restore(msg_set, DefaultMailboxes.INBOX)

# Permanently delete messages
mailbox.delete_permanently(msg_set)
mailbox.delete(msg_set)

except IMAPClientError as e:
print(f"An error occurred with the IMAP client: {e}")
```

### Methods of IMAPMailboxService Explained

1. **select_mailbox(mailbox=DefaultMailboxes.INBOX)**
- **Purpose:** Selects the specified mailbox for subsequent operations.
- **Example:**
```python
mailbox_service.select_mailbox('INBOX')
```

2. **close_mailbox()**
- **Purpose:** Closes the currently selected mailbox to ensure all changes are saved and the connection is properly terminated.
- **Example:**
```python
mailbox_service.close_mailbox()
```

3. **check()**
- **Purpose:** Sends a CHECK command to the IMAP server to synchronize the mailbox.
- **Example:**
```python
mailbox_service.check()
```

4. **delete_temporarily(msg_set: MessageSet)**
- **Purpose:** Marks messages for deletion and moves them to the trash folder.
- **Example:**
```python
mailbox_service.delete_temporarily(MessageSet('1,2,3'))
```

5. **delete_permanently(msg_set: MessageSet)**
- **Purpose:** Permanently deletes messages marked for deletion.
- **Example:**
```python
mailbox_service.delete_permanently(MessageSet('1,2,3'))
```

6. **move_to_folder(msg_set: MessageSet, folder: str)**
- **Purpose:** Moves messages to the specified folder.
- **Example:**
```python
mailbox_service.move_to_folder(MessageSet('1,2,3'), 'Archive')
```

7. **restore_from_trash(msg_set: MessageSet, original_folder: str)**
- **Purpose:** Restores messages from the trash to the original folder.
- **Example:**
```python
mailbox_service.restore_from_trash(MessageSet('1,2,3'), 'INBOX')
```

8. **get_mailbox_status(mailbox=DefaultMailboxes.INBOX, *status_items: List[MailboxStatusItems

])**
- **Purpose:** Gets the status of the specified mailbox based on the provided status items.
- **Example:**
```python
status = mailbox_service.get_mailbox_status('INBOX', MailboxStatusItems.MESSAGES)
print(f"Mailbox status: {status}")
```

By utilizing these classes and methods, you can manage your IMAP mailboxes effectively and perform necessary email operations in a robust and error-handling manner.

## License
This project is licensed under the MIT License.
2 changes: 1 addition & 1 deletion docs/source/code/flags.rst → docs/source/code/enums.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Flag Module
=============

.. automodule:: sage_imap.helpers.flags
.. automodule:: sage_imap.helpers.enums
:members:
:undoc-members:
:show-inheritance:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Message Module
==============

.. automodule:: sage_imap.helpers.message
.. automodule:: sage_imap.models
:members:
:undoc-members:
:show-inheritance:
Expand Down
4 changes: 2 additions & 2 deletions docs/source/code/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Code API

client
exceptions
flags
enums
folder
mailbox
message
models
search
13 changes: 0 additions & 13 deletions docs/source/getting_started/configuration.rst

This file was deleted.

30 changes: 28 additions & 2 deletions docs/source/getting_started/examples/example1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Example 1: Creating an IMAP Client

This example demonstrates how to create an IMAP client using the `IMAPClient` class.

The IMAPClient class can be used both with and without a context manager.

# **With Context Manager**

.. code-block:: python
from sage_imap.services.client import IMAPClient
Expand All @@ -15,6 +19,24 @@ This example demonstrates how to create an IMAP client using the `IMAPClient` cl
status, messages = client.select("INBOX")
print(f"Selected INBOX with status: {status}")
# **Without Context Manager**

.. code-block:: python
from sage_imap.services.client import IMAPClient
# Initialize and use without context manager
client = IMAPClient('imap.example.com', 'username', 'password')
try:
client.connect()
capabilities = client.connection.capability()
print(f"Server capabilities: {capabilities}")
status, messages = client.connection.select("INBOX")
print(f"Selected INBOX with status: {status}")
finally:
client.disconnect()
Explanation
-----------

Expand All @@ -25,11 +47,15 @@ This example illustrates a low-level approach to working with IMAP. If you want
- When the `with` block is entered, the connection to the IMAP server is established, and the user is authenticated.
- When the `with` block is exited, the connection is automatically closed, ensuring that resources are cleaned up properly.

2. **Why Use IMAPClient**:
2. **IMAPClient Without Context Manager**:
- You can also use the `IMAPClient` class without a context manager. In this case, you need to manually call `connect()` to establish the connection and `disconnect()` to close it.
- This approach provides explicit control over when the connection is opened and closed but requires careful handling to ensure resources are properly released.

3. **Why Use IMAPClient**:
- The `IMAPClient` exists to simplify the management of IMAP connections. By using it as a context manager, you don't have to worry about manually opening and closing the connection. This reduces the risk of resource leaks and makes your code cleaner and more maintainable.
- Within the context manager, you have access to the `imaplib` capabilities directly through the `client` object. This allows you to perform various IMAP operations seamlessly.

3. **Capabilities and Select Methods**:
4. **Capabilities and Select Methods**:
- The `.capability()` method is called to retrieve the server's capabilities, providing information about what commands and features the server supports.
- The `.select("INBOX")` method is used to select the "INBOX" mailbox for further operations. It returns the status of the selection and the number of messages in the mailbox.

Expand Down
Loading

0 comments on commit a8066ea

Please sign in to comment.