From efe08313490ff418f96f62dbdb94c93d05ffac10 Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 10:52:11 -0700 Subject: [PATCH 01/18] Added image validator. --- .gitignore | 3 ++ validators/__init__.py | 1 + validators/image.py | 71 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 validators/image.py diff --git a/.gitignore b/.gitignore index 76012ef6..c36547e0 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,6 @@ nosetests.xml .mr.developer.cfg .project .pydevproject + +# PyCharm +.idea diff --git a/validators/__init__.py b/validators/__init__.py index ee7d91cf..2a833a70 100644 --- a/validators/__init__.py +++ b/validators/__init__.py @@ -12,5 +12,6 @@ from .url import url # noqa from .utils import ValidationFailure, validator # noqa from .uuid import uuid # noqa +from .image import image __version__ = '0.11.1' diff --git a/validators/image.py b/validators/image.py new file mode 100644 index 00000000..6e13770f --- /dev/null +++ b/validators/image.py @@ -0,0 +1,71 @@ +import os +import imghdr + +from io import BytesIO +from .url import url + +try: + from urllib2 import urlopen +except ImportError: + from urllib.request import urlopen + +from .utils import validator, ValidationFailure + + +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`. + + Examples:: + >>> image('pic.jpg') + True + + >>> image('https://i.imgur.com/DKUR9Tk.png') + True + + :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 From 5974870b202c6e65d65b20f5059113ad33a1f99b Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 11:13:00 -0700 Subject: [PATCH 02/18] Added tests for image URLs. --- tests/test_image.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/test_image.py diff --git a/tests/test_image.py b/tests/test_image.py new file mode 100644 index 00000000..aa48b679 --- /dev/null +++ b/tests/test_image.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +import pytest + +from validators import image, ValidationFailure + +#https://i.imgur.com/DKUR9Tk.png is Grumpy Cat + +@pytest.mark.parameterize('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(path) + +@pytest.mark.parameterize('address', [ + u'http://www.google.com/' +]) +def test_returns_false_on_invalid_image_url(address): + assert isinstance(image(address), ValidationFailure) From 900a0ea176af0ecda3706ac3b8bc7139d7c221af Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 11:38:40 -0700 Subject: [PATCH 03/18] Corrected typo. --- tests/test_image.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_image.py b/tests/test_image.py index aa48b679..f73722b2 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -5,14 +5,16 @@ #https://i.imgur.com/DKUR9Tk.png is Grumpy Cat -@pytest.mark.parameterize('address', [ + +@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(path) -@pytest.mark.parameterize('address', [ + +@pytest.mark.parametrize('address', [ u'http://www.google.com/' ]) def test_returns_false_on_invalid_image_url(address): From 17276fc59d4e531ab5b857a1ddbfb48d85bd8a17 Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 11:42:08 -0700 Subject: [PATCH 04/18] Corrected typo. --- tests/test_image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_image.py b/tests/test_image.py index f73722b2..7c3ba404 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -11,7 +11,7 @@ u'https://i.imgur.com/DKUR9Tk.png?fb' ]) def test_returns_true_on_valid_image_url(address): - assert image(path) + assert image(address) @pytest.mark.parametrize('address', [ From 2617e5998133db1285594afe36947b8f41fd99e4 Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 22:01:32 -0700 Subject: [PATCH 05/18] Added PIL to tests. Added tests for local picture files. --- setup.py | 3 ++- tests/test_image.py | 13 +++++++++++++ validators/__init__.py | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index c6c81e53..388b92de 100644 --- a/setup.py +++ b/setup.py @@ -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' ], } diff --git a/tests/test_image.py b/tests/test_image.py index 7c3ba404..dc7822be 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -1,11 +1,20 @@ # -*- coding: utf-8 -*- import pytest +from PIL import Image from validators import image, ValidationFailure #https://i.imgur.com/DKUR9Tk.png is Grumpy Cat +@pytest.fixture() +def image_file(tmpdir_factory): + temp_path = tmpdir_factory.mktemp('pics').join('test.png') + new_image = Image.new('RGBA', (10, 10)) + new_image.save(str(temp_path)) + return temp_path + + @pytest.mark.parametrize('address', [ u'https://i.imgur.com/DKUR9Tk.png', u'https://i.imgur.com/DKUR9Tk.png?fb' @@ -19,3 +28,7 @@ def test_returns_true_on_valid_image_url(address): ]) 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(str(image_file)) diff --git a/validators/__init__.py b/validators/__init__.py index 2a833a70..81d1d462 100644 --- a/validators/__init__.py +++ b/validators/__init__.py @@ -12,6 +12,6 @@ from .url import url # noqa from .utils import ValidationFailure, validator # noqa from .uuid import uuid # noqa -from .image import image +from .image import image # noqa __version__ = '0.11.1' From 4d3439af348d1fc74ed1d9d700061e624e1fec65 Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 22:05:41 -0700 Subject: [PATCH 06/18] Added PIL to tests. Added tests for local picture files. --- tests/test_image.py | 3 --- validators/image.py | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_image.py b/tests/test_image.py index dc7822be..cf66cbf0 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -1,11 +1,8 @@ # -*- coding: utf-8 -*- import pytest from PIL import Image - from validators import image, ValidationFailure -#https://i.imgur.com/DKUR9Tk.png is Grumpy Cat - @pytest.fixture() def image_file(tmpdir_factory): diff --git a/validators/image.py b/validators/image.py index 6e13770f..a174fa5a 100644 --- a/validators/image.py +++ b/validators/image.py @@ -50,7 +50,7 @@ def image(path): :class:`~validators.utils.ValidationFailure`. Examples:: - >>> image('pic.jpg') + >>> image('test.png') True >>> image('https://i.imgur.com/DKUR9Tk.png') From 3e60ea71cbde8bcf18aeacf15ba9187023b974b0 Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 22:07:42 -0700 Subject: [PATCH 07/18] Added PIL to tests. Added tests for local picture files. --- tests/test_image.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_image.py b/tests/test_image.py index cf66cbf0..4eeb07b2 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -5,8 +5,8 @@ @pytest.fixture() -def image_file(tmpdir_factory): - temp_path = tmpdir_factory.mktemp('pics').join('test.png') +def image_file(tmpdir): + temp_path = tmpdir.join('test.png') new_image = Image.new('RGBA', (10, 10)) new_image.save(str(temp_path)) return temp_path From a0a850ef2e104fe5869ceb0002a96731699000b8 Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 22:15:41 -0700 Subject: [PATCH 08/18] Added PIL to tests. Added tests for local picture files. --- tests/test_image.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_image.py b/tests/test_image.py index 4eeb07b2..ee72cbae 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -5,11 +5,11 @@ @pytest.fixture() -def image_file(tmpdir): - temp_path = tmpdir.join('test.png') - new_image = Image.new('RGBA', (10, 10)) - new_image.save(str(temp_path)) - return temp_path +def image_file(tmpdir_factory): + img = Image.new('RGBA', (10, 10)) + fn = tmpdir_factory.mktemp('pic').join('test.png') + img.save(str(fn)) + return fn @pytest.mark.parametrize('address', [ From 979ed7fd332850cde9ad222c95b24052c227d8cf Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 22:37:44 -0700 Subject: [PATCH 09/18] Added PIL to tests. Added tests for local picture files. --- tests/test_image.py | 10 ++++++---- validators/__init__.py | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/test_image.py b/tests/test_image.py index ee72cbae..60fc71ac 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -1,15 +1,17 @@ # -*- coding: utf-8 -*- +import os import pytest from PIL import Image from validators import image, ValidationFailure @pytest.fixture() -def image_file(tmpdir_factory): +def image_file(tmpdir): + tmp = str(tmpdir) + path = os.path.join([tmp, 'test.png']) img = Image.new('RGBA', (10, 10)) - fn = tmpdir_factory.mktemp('pic').join('test.png') - img.save(str(fn)) - return fn + img.save(str(path), format='PNG') + return path @pytest.mark.parametrize('address', [ diff --git a/validators/__init__.py b/validators/__init__.py index 81d1d462..6c1d3eb5 100644 --- a/validators/__init__.py +++ b/validators/__init__.py @@ -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 @@ -12,6 +13,5 @@ from .url import url # noqa from .utils import ValidationFailure, validator # noqa from .uuid import uuid # noqa -from .image import image # noqa __version__ = '0.11.1' From 20d18c2f09e9db72bab1af4e04e75f84c7883f2f Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 22:46:28 -0700 Subject: [PATCH 10/18] Added PIL to tests. Added tests for local picture files. Test for local file still not working. --- tests/test_image.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_image.py b/tests/test_image.py index 60fc71ac..3365974c 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -11,7 +11,7 @@ def image_file(tmpdir): path = os.path.join([tmp, 'test.png']) img = Image.new('RGBA', (10, 10)) img.save(str(path), format='PNG') - return path + return tmp @pytest.mark.parametrize('address', [ @@ -30,4 +30,4 @@ def test_returns_false_on_invalid_image_url(address): def test_returns_true_on_valid_image_file(image_file): - assert image(str(image_file)) + assert image(os.path.join([image_file, 'test.png'])) From 276913789a030160050cf17791f5a64c9c3cb3e0 Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 22:50:20 -0700 Subject: [PATCH 11/18] Added PIL to tests. Added tests for local picture files. --- tests/test_image.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_image.py b/tests/test_image.py index 3365974c..84fd1c21 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -8,7 +8,7 @@ @pytest.fixture() def image_file(tmpdir): tmp = str(tmpdir) - path = os.path.join([tmp, 'test.png']) + path = os.path.join(tmp, 'test.png') img = Image.new('RGBA', (10, 10)) img.save(str(path), format='PNG') return tmp @@ -30,4 +30,4 @@ def test_returns_false_on_invalid_image_url(address): def test_returns_true_on_valid_image_file(image_file): - assert image(os.path.join([image_file, 'test.png'])) + assert image(os.path.join(image_file, 'test.png')) From 5f0535a746f28a9d28b670eb025f4de9ae9d6873 Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 23:15:33 -0700 Subject: [PATCH 12/18] Tests work. --- tests/test_image.py | 19 +++++++++++++++---- validators/image.py | 7 ------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/test_image.py b/tests/test_image.py index 84fd1c21..87789543 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -7,11 +7,18 @@ @pytest.fixture() def image_file(tmpdir): - tmp = str(tmpdir) - path = os.path.join(tmp, 'test.png') + path = os.path.join(str(tmpdir), 'test.png') img = Image.new('RGBA', (10, 10)) img.save(str(path), format='PNG') - return tmp + 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', [ @@ -30,4 +37,8 @@ def test_returns_false_on_invalid_image_url(address): def test_returns_true_on_valid_image_file(image_file): - assert image(os.path.join(image_file, 'test.png')) + assert image(image_file) + + +def test_returns_false_on_invalid_image_file(text_file): + assert isinstance(image(text_file), ValidationFailure) \ No newline at end of file diff --git a/validators/image.py b/validators/image.py index a174fa5a..5a6ad1a9 100644 --- a/validators/image.py +++ b/validators/image.py @@ -49,13 +49,6 @@ def image(path): If the file is a valid image type this function returns ``True``, otherwise :class:`~validators.utils.ValidationFailure`. - Examples:: - >>> image('test.png') - True - - >>> image('https://i.imgur.com/DKUR9Tk.png') - True - :param path: The path to the file to evaluate. """ From 1d5339050db5843e3f8af69519ec782a2da73aa4 Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 23:19:09 -0700 Subject: [PATCH 13/18] Tests work. Newline added at end of image_test. --- tests/test_image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_image.py b/tests/test_image.py index 87789543..10d85f53 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -41,4 +41,4 @@ def test_returns_true_on_valid_image_file(image_file): def test_returns_false_on_invalid_image_file(text_file): - assert isinstance(image(text_file), ValidationFailure) \ No newline at end of file + assert isinstance(image(text_file), ValidationFailure) From 72f41472763f188bc42a230bdd9962ec77489fc4 Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 23:30:05 -0700 Subject: [PATCH 14/18] imports sorted. --- tests/test_image.py | 2 ++ validators/image.py | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_image.py b/tests/test_image.py index 10d85f53..ed7aa480 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -1,7 +1,9 @@ # -*- coding: utf-8 -*- import os + import pytest from PIL import Image + from validators import image, ValidationFailure diff --git a/validators/image.py b/validators/image.py index 5a6ad1a9..f9a91930 100644 --- a/validators/image.py +++ b/validators/image.py @@ -1,7 +1,7 @@ -import os import imghdr - +import os from io import BytesIO + from .url import url try: From 7781932ab7370ffffc9da73a3ee55da0af499242 Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 23:42:29 -0700 Subject: [PATCH 15/18] imports sorted. --- validators/image.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/validators/image.py b/validators/image.py index f9a91930..f756f496 100644 --- a/validators/image.py +++ b/validators/image.py @@ -2,13 +2,12 @@ import os from io import BytesIO -from .url import url - try: from urllib2 import urlopen except ImportError: from urllib.request import urlopen +from .url import url from .utils import validator, ValidationFailure From aaf7d86875733cf4b6c97ab3295d43981d415785 Mon Sep 17 00:00:00 2001 From: shirizaan Date: Tue, 20 Dec 2016 23:54:15 -0700 Subject: [PATCH 16/18] tests pass. imports sorted. isort passes. --- validators/image.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/validators/image.py b/validators/image.py index f756f496..48f4b851 100644 --- a/validators/image.py +++ b/validators/image.py @@ -2,14 +2,14 @@ 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 -from .url import url -from .utils import validator, ValidationFailure - def _image_url(path): result = False From eb2cb386c5affdb9190e6bd84b3b921db6639fd3 Mon Sep 17 00:00:00 2001 From: shirizaan Date: Wed, 21 Dec 2016 00:22:30 -0700 Subject: [PATCH 17/18] tests pass. imports sorted. isort passes. --- validators/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validators/__init__.py b/validators/__init__.py index 6c1d3eb5..be9f4020 100644 --- a/validators/__init__.py +++ b/validators/__init__.py @@ -4,7 +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 .image import image # noqa from .ip_address import ipv4, ipv6 # noqa from .length import length # noqa from .mac_address import mac_address # noqa From 258ec5809bdd1d50ee8fad9a5303c4516de337c3 Mon Sep 17 00:00:00 2001 From: shirizaan Date: Wed, 21 Dec 2016 01:58:33 -0700 Subject: [PATCH 18/18] tests pass. imports sorted. isort passes. --- docs/index.rst | 7 +++++++ validators/image.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/index.rst b/docs/index.rst index b7284dfb..616fe753 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -91,6 +91,13 @@ iban .. autofunction:: iban +image +----- + +.. module:: validators.image + +.. autofunction:: image + ipv4 ---- diff --git a/validators/image.py b/validators/image.py index 48f4b851..141ef3f4 100644 --- a/validators/image.py +++ b/validators/image.py @@ -1,4 +1,4 @@ -import imghdr +import imghdr # isort:skip import os from io import BytesIO