Skip to content

Commit

Permalink
resilience against missing libmagic
Browse files Browse the repository at this point in the history
  • Loading branch information
e3rd committed Jan 4, 2024
1 parent eec443a commit edba217
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# CHANGELOG

## 2.0.3
## 2.0.4
- resilience against missing libmagic (useful on a contained environment)

## 2.0.3 (2023-01-03)
- fix: loading headers not encoded with utf-8
- fix: better S/MIME detection #29
- drop Python 3.9 support
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Envelope.load(path="message.eml").attachments()
* If planning to use S/MIME, you should ensure some prerequisites: `sudo apt install swig build-essential python3-dev libssl-dev && pip3 install M2Crypto`
* If planning to send e-mails, prepare SMTP credentials or visit [Configure your SMTP](#configure-your-smtp) tutorial.
* If your e-mails are to be received outside your local domain, visit [DMARC](#dmarc) section.
* Package [python-magic](https://pypi.org/project/python-magic/) is used as a dependency. Due to a [well-known](https://github.com/ahupp/python-magic/blob/master/COMPAT.md) name clash with the [file-magic](https://pypi.org/project/file-magic/) package, in case you need to use the latter, don't worry to run `pip uninstall python-magic && pip install file-magic` after installing envelope which is fully compatible with both projects.
* Package [python-magic](https://pypi.org/project/python-magic/) is used as a dependency. Due to a [well-known](https://github.com/ahupp/python-magic/blob/master/COMPAT.md) name clash with the [file-magic](https://pypi.org/project/file-magic/) package, in case you need to use the latter, don't worry to run `pip uninstall python-magic && pip install file-magic` after installing envelope which is fully compatible with both projects. Both use `libmagic` under the hood which is probably already installed. However, if it is not, [install](https://github.com/ahupp/python-magic?tab=readme-ov-file#installation) `sudo apt install libmagic1`.
## Bash completion
1. Run: `apt install bash-completion jq`
Expand Down Expand Up @@ -365,7 +365,7 @@ Envelope().attach(path="file.jpg")
```
* **mime**: Sets contents mime subtype: "**auto**" (default), "**html**" or "**plain**" for plain text.
Maintype is always set to "text".
Set maintype to "text". If a line is longer than 1000 characters, makes the message be transferred safely by bytes (otherwise these non-standard long lines might cause a transferring SMTP server to include line breaks and redundant spaces that might break up ex: DKIM signature).
If a line is longer than 1000 characters, makes the message be transferred safely by bytes (otherwise these non-standard long lines might cause a transferring SMTP server to include line breaks and redundant spaces that might break up ex: DKIM signature).
In case of `Content-Type` header put to the message, **mime** section functionality **is skipped**.
* **--mime SUBTYPE**
* **.mime(subtype="auto", nl2br="auto")**
Expand Down
8 changes: 6 additions & 2 deletions envelope/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from pathlib import Path
from typing import TYPE_CHECKING, Iterable

import magic
try:
import magic
except ImportError as e:
magic = e

if TYPE_CHECKING:
from .envelope import Envelope
Expand Down Expand Up @@ -48,7 +51,8 @@ def get_mimetype(data: bytes = None, path: Path = None):
""" We support both python-magic and file-magic library, any of them can be on the system. #25
Their funcionality is the same, their API differs.
"""
# XX change to match statement as of Python3.10
if isinstance(magic, ImportError): # user has to install libmagic
raise magic
if hasattr(magic.Magic, "from_file"): # this is python-magic
if data:
return magic.Magic(mime=True).from_buffer(data)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

setup(
name='envelope',
version='2.0.3',
version='2.0.4',
packages=['envelope'],
author='Edvard Rejthar',
author_email='[email protected]',
Expand Down

0 comments on commit edba217

Please sign in to comment.