Skip to content

Commit 8b26281

Browse files
committed
move getimagesize into utils
And now the tools directory is no longer needed.
1 parent 6544c68 commit 8b26281

7 files changed

Lines changed: 36 additions & 38 deletions

File tree

tests/test_getimagesize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from textile.tools.imagesize import getimagesize
1+
from textile.utils import getimagesize
22
import pytest
33

44
PIL = pytest.importorskip('PIL')

tests/test_imagesize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
def test_imagesize():
55
imgurl = 'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png'
6-
result = textile.tools.imagesize.getimagesize(imgurl)
6+
result = textile.utils.getimagesize(imgurl)
77
try:
88
import PIL # noqa: F401
99

textile/core.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020
from collections import OrderedDict
2121
from nh3 import clean
2222

23-
from textile.tools import imagesize
2423
from textile.regex_strings import (align_re_s, cls_re_s, pnct_re_s,
2524
regex_snippets, syms_re_s, table_span_re_s)
2625
from textile.utils import (decode_high, encode_high, encode_html, generate_tag,
27-
has_raw_text, human_readable_url, is_rel_url,
28-
is_valid_url, list_type, normalize_newlines,
29-
parse_attributes, pba)
26+
getimagesize, has_raw_text, human_readable_url,
27+
is_rel_url, is_valid_url, list_type,
28+
normalize_newlines, parse_attributes, pba)
3029
from textile.objects import Block, Table
3130

3231
try:
@@ -1152,7 +1151,7 @@ def fImage(self, match):
11521151
title = ''
11531152

11541153
if not is_rel_url(url) and self.get_sizes:
1155-
size = imagesize.getimagesize(url)
1154+
size = getimagesize(url)
11561155

11571156
if href:
11581157
href = self.shelveURL(href)

textile/textilefactory.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ def __init__(self, restricted=False, lite=False, sanitize=False,
2020
self.method_parms['rel'] = 'nofollow'
2121

2222
if noimage is None:
23-
if restricted:
24-
noimage = True
25-
else:
26-
noimage = False
23+
noimage = bool(restricted)
2724

2825
self.class_parms['noimage'] = noimage
2926
self.method_parms['sanitize'] = sanitize

textile/tools/__init__.py

Whitespace-only changes.

textile/tools/imagesize.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

textile/utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,35 @@ def generate_tag(tag, content, attributes=None):
7979
return element_text
8080

8181

82+
def getimagesize(url):
83+
"""
84+
Attempts to determine an image's width and height, and returns a tuple,
85+
(width, height), in pixels or an empty string in case of failure.
86+
Requires that PIL is installed.
87+
88+
"""
89+
90+
try:
91+
from PIL import ImageFile
92+
except ImportError:
93+
return ''
94+
95+
from urllib.request import urlopen
96+
97+
try:
98+
p = ImageFile.Parser()
99+
f = urlopen(url)
100+
while True:
101+
s = f.read(1024)
102+
if not s:
103+
break
104+
p.feed(s)
105+
if p.image:
106+
return p.image.size
107+
except (IOError, ValueError):
108+
return ''
109+
110+
82111
def has_raw_text(text):
83112
"""checks whether the text has text not already enclosed by a block tag"""
84113
r = text.strip()

0 commit comments

Comments
 (0)