Skip to content

Commit

Permalink
Removing magic from optionals, #112
Browse files Browse the repository at this point in the history
  • Loading branch information
pylover committed Aug 5, 2018
1 parent dedfbb7 commit 9c1c3c1
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 42 deletions.
2 changes: 0 additions & 2 deletions requirements-optional.txt
@@ -1,5 +1,3 @@
python-magic >= 0.4.12
pillow
requests
Werkzeug
requests-aws4auth >= 0.9
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -12,6 +12,7 @@
dependencies = [
'sqlalchemy >= 1.1.0b3',
'pillow',
'python-magic >= 0.4.12'
]


Expand Down
6 changes: 0 additions & 6 deletions sphinx/api.rst
Expand Up @@ -203,12 +203,6 @@ MagicAnalyzer
.. autoclass:: MagicAnalyzer


WandAnalyzer
^^^^^^^^^^^^

.. autoclass:: WandAnalyzer


Validator
^^^^^^^^^

Expand Down
2 changes: 1 addition & 1 deletion sphinx/index.rst
Expand Up @@ -62,7 +62,7 @@ Overview
- Limiting file size(min, max), to prevent DOS attacks.
- Adding timestamp in url to help caching.
- Auto generating thumbnails, using ``width``, ``height`` and or ``ratio``.
- Analyzing files & images using ``magic`` and ``wand``.
- Analyzing files & images using ``magic`` and ``PIL``.
- Validating ``mimetype``, ``width``, ``height`` and image ``ratio``.
- Automatically resize & reformat images before store.

Expand Down
6 changes: 4 additions & 2 deletions sphinx/install.rst
Expand Up @@ -28,9 +28,11 @@ or
Optional packages
-----------------

Some optional packages are required when using some features, such as: ``wand`` and or ``python-magic``.
Some optional packages are required when using some features, such as:
``requests-aws4auth`` and or ``requests-aliyun``.


.. code-block:: console
$ pip install wand python-magic
$ pip install requests-aws4auth
5 changes: 1 addition & 4 deletions sphinx/tutorials/image.rst
Expand Up @@ -6,12 +6,9 @@ This is how to attach image file to sqlalchemy model using the :class:`.FileSyst
0. Prerequisites
----------------

Thumbnail generation required the ``wand``, and content type detection required ``magic`` package.
These are optional packages. so you have to install them:

.. code-block:: console
$ pip install wand python-magic
$ pip install sqlalchemy-media
1. Creating workbench
Expand Down
17 changes: 17 additions & 0 deletions sqlalchemy_media/mimetypes_.py
Expand Up @@ -16,6 +16,23 @@
"""

from os.path import splitext
import magic


# Libmagic
def magic_mime_from_buffer(buffer: bytes) -> str:
"""
Try to detect mimetype using ``magic`` library.
.. warning:: :exc:`.OptionalPackageRequirementError` will be raised if
``python-magic`` is not installed.
:param buffer: buffer from header of file.
:return: The mimetype
"""
return magic.from_buffer(buffer, mime=True)



def guess_extension(mimetype: str) -> str:
Expand Down
25 changes: 0 additions & 25 deletions sqlalchemy_media/optionals.py
Expand Up @@ -14,31 +14,6 @@
from .exceptions import OptionalPackageRequirementError


# Libmagic

try:
import magic
except ImportError: # pragma: no cover
magic = None


def magic_mime_from_buffer(buffer: bytes) -> str:
"""
Try to detect mimetype using ``magic`` library.
.. warning:: :exc:`.OptionalPackageRequirementError` will be raised if
``python-magic`` is not installed.
:param buffer: buffer from header of file.
:return: The mimetype
"""

if magic is None: # pragma: no cover
raise OptionalPackageRequirementError('python-magic')

return magic.from_buffer(buffer, mime=True)


# requests-aws4auth
try:
Expand Down
3 changes: 1 addition & 2 deletions sqlalchemy_media/processors.py
Expand Up @@ -7,8 +7,7 @@
from .exceptions import ContentTypeValidationError, DimensionValidationError, \
AspectRatioValidationError, AnalyzeError
from .helpers import validate_width_height_ratio, deprecated
from .mimetypes_ import guess_extension, guess_type
from .optionals import magic_mime_from_buffer
from .mimetypes_ import guess_extension, guess_type, magic_mime_from_buffer
from .typing_ import Dimension


Expand Down

0 comments on commit 9c1c3c1

Please sign in to comment.