Skip to content

Commit

Permalink
move getimagesize into utils
Browse files Browse the repository at this point in the history
And now the tools directory is no longer needed.
  • Loading branch information
ikirudennis committed Aug 9, 2024
1 parent 6544c68 commit 8b26281
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 38 deletions.
2 changes: 1 addition & 1 deletion tests/test_getimagesize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from textile.tools.imagesize import getimagesize
from textile.utils import getimagesize
import pytest

PIL = pytest.importorskip('PIL')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_imagesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

Expand Down
9 changes: 4 additions & 5 deletions textile/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
from collections import OrderedDict
from nh3 import clean

from textile.tools import imagesize
from textile.regex_strings import (align_re_s, cls_re_s, pnct_re_s,
regex_snippets, syms_re_s, table_span_re_s)
from textile.utils import (decode_high, encode_high, encode_html, generate_tag,
has_raw_text, human_readable_url, is_rel_url,
is_valid_url, list_type, normalize_newlines,
parse_attributes, pba)
getimagesize, has_raw_text, human_readable_url,
is_rel_url, is_valid_url, list_type,
normalize_newlines, parse_attributes, pba)
from textile.objects import Block, Table

try:
Expand Down Expand Up @@ -1152,7 +1151,7 @@ def fImage(self, match):
title = ''

if not is_rel_url(url) and self.get_sizes:
size = imagesize.getimagesize(url)
size = getimagesize(url)

if href:
href = self.shelveURL(href)
Expand Down
5 changes: 1 addition & 4 deletions textile/textilefactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ def __init__(self, restricted=False, lite=False, sanitize=False,
self.method_parms['rel'] = 'nofollow'

if noimage is None:
if restricted:
noimage = True
else:
noimage = False
noimage = bool(restricted)

self.class_parms['noimage'] = noimage
self.method_parms['sanitize'] = sanitize
Expand Down
Empty file removed textile/tools/__init__.py
Empty file.
27 changes: 0 additions & 27 deletions textile/tools/imagesize.py

This file was deleted.

29 changes: 29 additions & 0 deletions textile/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,35 @@ def generate_tag(tag, content, attributes=None):
return element_text


def getimagesize(url):
"""
Attempts to determine an image's width and height, and returns a tuple,
(width, height), in pixels or an empty string in case of failure.
Requires that PIL is installed.
"""

try:
from PIL import ImageFile
except ImportError:
return ''

from urllib.request import urlopen

try:
p = ImageFile.Parser()
f = urlopen(url)
while True:
s = f.read(1024)
if not s:
break
p.feed(s)
if p.image:
return p.image.size
except (IOError, ValueError):
return ''


def has_raw_text(text):
"""checks whether the text has text not already enclosed by a block tag"""
r = text.strip()
Expand Down

0 comments on commit 8b26281

Please sign in to comment.