Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject

# PyCharm
.idea
7 changes: 7 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ iban

.. autofunction:: iban

image
-----

.. module:: validators.image

.. autofunction:: image

ipv4
----

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def get_version():
'test': [
'pytest>=2.2.3',
'flake8>=2.4.0',
'isort>=4.2.2'
'isort>=4.2.2',
'pillow>=3.4.2'
],
}

Expand Down
46 changes: 46 additions & 0 deletions tests/test_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
import os

import pytest
from PIL import Image

from validators import image, ValidationFailure


@pytest.fixture()
def image_file(tmpdir):
path = os.path.join(str(tmpdir), 'test.png')
img = Image.new('RGBA', (10, 10))
img.save(str(path), format='PNG')
return str(path)


@pytest.fixture()
def text_file(tmpdir):
path = os.path.join(str(tmpdir), 'test.txt')
f = open(path, 'w')
f.close()
return str(path)


@pytest.mark.parametrize('address', [
u'https://i.imgur.com/DKUR9Tk.png',
u'https://i.imgur.com/DKUR9Tk.png?fb'
])
def test_returns_true_on_valid_image_url(address):
assert image(address)


@pytest.mark.parametrize('address', [
u'http://www.google.com/'
])
def test_returns_false_on_invalid_image_url(address):
assert isinstance(image(address), ValidationFailure)


def test_returns_true_on_valid_image_file(image_file):
assert image(image_file)


def test_returns_false_on_invalid_image_file(text_file):
assert isinstance(image(text_file), ValidationFailure)
1 change: 1 addition & 0 deletions validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .extremes import Max, Min # noqa
from .i18n import fi_business_id, fi_ssn # noqa
from .iban import iban # noqa
from .image import image # noqa
from .ip_address import ipv4, ipv6 # noqa
from .length import length # noqa
from .mac_address import mac_address # noqa
Expand Down
63 changes: 63 additions & 0 deletions validators/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import imghdr # isort:skip
import os
from io import BytesIO

from .url import url
from .utils import ValidationFailure, validator

try:
from urllib2 import urlopen
except ImportError:
from urllib.request import urlopen


def _image_url(path):
result = False

try:
if url(path):
response = urlopen(path)
data = BytesIO(response.read())
if imghdr.what(data) is not None:
result = True
else:
result = False
except ValidationFailure:
result = False

return result


def _image_file(path):
result = False

if os.path.exists(path):
if imghdr.what(path) is not None:
result = True
else:
result = False

return result


@validator
def image(path):
"""
Return whether or not a file is a valid image type.

If the file is a valid image type this function returns ``True``, otherwise
:class:`~validators.utils.ValidationFailure`.

:param path: The path to the file to evaluate.
"""

result = False

if _image_url(path):
result = True
elif _image_file(path):
result = True
else:
result = False

return result