Skip to content

Commit fc47aa2

Browse files
committed
prevent ImportErrors when requests is missing
- the only command the lack of an http library should affect is read() - will now only trigger an exception if read(some_url) is called but requests hasn’t been installed - also means setup.py doesn’t need to check on the build/lib dir before running tests
1 parent 723341f commit fc47aa2

3 files changed

Lines changed: 49 additions & 43 deletions

File tree

plotdevice/run/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
# add our embedded PyObjC site-dir to the sys.path (and remove any conflicts)
1313
map(sys.path.remove, filter(lambda p:p.endswith('PyObjC'), sys.path))
14-
if exists(objc_dir):
15-
site.addsitedir(objc_dir)
14+
site.addsitedir(objc_dir)
1615

1716
# test the sys.path by attempting to load a PyObjC submodule
1817
import objc

plotdevice/util/readers.py

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# encoding: utf-8
22
import os, sys, re
33
from operator import attrgetter
4+
PY2 = sys.version_info[0] == 2
45

56
# files & io
67
from io import open, StringIO, BytesIO
78
from os.path import abspath, dirname, exists, join, splitext
89
from plotdevice import DeviceError, INTERNAL
10+
text_type = str if not PY2 else unicode
911

1012
# data formats
1113
import json, csv
@@ -14,33 +16,8 @@
1416
from xml.parsers import expat
1517

1618
# http
17-
import requests
1819
from urlparse import urlparse
1920
from Foundation import NSDateFormatter, NSLocale, NSTimeZone, NSDate
20-
from cachecontrol import CacheControl, CacheControlAdapter
21-
from cachecontrol.caches import FileCache
22-
from cachecontrol.heuristics import LastModified
23-
24-
PY2 = sys.version_info[0] == 2
25-
text_type = str if not PY2 else unicode
26-
27-
### HTTP utils ###
28-
29-
cache_dir = '%s/Library/Caches/PlotDevice'%os.environ['HOME']
30-
HTTP = CacheControl(requests.Session(), cache=FileCache(cache_dir), heuristic=LastModified())
31-
32-
_nsdf = NSDateFormatter.alloc().init()
33-
_nsdf.setLocale_(NSLocale.alloc().initWithLocaleIdentifier_("en_US_POSIX"))
34-
_nsdf.setDateFormat_("EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss zzz")
35-
_nsdf.setTimeZone_(NSTimeZone.timeZoneForSecondsFromGMT_(0))
36-
37-
def last_modified(resp):
38-
"""Return the last modified date as a unix time_t"""
39-
last_mod = _nsdf.dateFromString_(resp.headers.get('Last-Modified'))
40-
if not last_mod:
41-
last_mod = NSDate.date()
42-
return last_mod.timeIntervalSince1970()
43-
4421

4522

4623
### XML handling ###
@@ -211,6 +188,45 @@ def csv_dialect(fd):
211188
return csv.Sniffer().sniff(snippet)
212189

213190

191+
### HTTP utils ###
192+
193+
try:
194+
import requests
195+
from cachecontrol import CacheControl, CacheControlAdapter
196+
from cachecontrol.caches import FileCache
197+
from cachecontrol.heuristics import LastModified
198+
199+
cache_dir = '%s/Library/Caches/PlotDevice'%os.environ['HOME']
200+
HTTP = CacheControl(requests.Session(), cache=FileCache(cache_dir), heuristic=LastModified())
201+
except ImportError:
202+
class Decoy(object):
203+
def get(self, url):
204+
unsupported = 'could not find the "requests" library (try running "python setup.py dev" first)'
205+
raise RuntimeError(unsupported)
206+
HTTP = Decoy()
207+
208+
def binaryish(content, format):
209+
bin_types = ('pdf','eps','png','jpg','jpeg','gif','tiff','tif','zip','tar','gz')
210+
bin_formats = ('raw','bytes','img','image')
211+
if any(b in content for b in bin_types):
212+
return True
213+
if format:
214+
return any(b in format for b in bin_types+bin_formats)
215+
return False
216+
217+
_nsdf = NSDateFormatter.alloc().init()
218+
_nsdf.setLocale_(NSLocale.alloc().initWithLocaleIdentifier_("en_US_POSIX"))
219+
_nsdf.setDateFormat_("EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss zzz")
220+
_nsdf.setTimeZone_(NSTimeZone.timeZoneForSecondsFromGMT_(0))
221+
222+
def last_modified(resp):
223+
"""Return the last modified date as a unix time_t"""
224+
last_mod = _nsdf.dateFromString_(resp.headers.get('Last-Modified'))
225+
if not last_mod:
226+
last_mod = NSDate.date()
227+
return last_mod.timeIntervalSince1970()
228+
229+
214230
### File/URL Reader ###
215231

216232
def read(pth, format=None, encoding=None, cols=None, **kwargs):
@@ -280,12 +296,3 @@ def read(pth, format=None, encoding=None, cols=None, **kwargs):
280296
else:
281297
return fd.read()
282298

283-
def binaryish(content, format):
284-
bin_types = ('pdf','eps','png','jpg','jpeg','gif','tiff','tif','zip','tar','gz')
285-
bin_formats = ('raw','bytes','img','image')
286-
if any(b in content for b in bin_types):
287-
return True
288-
if format:
289-
return any(b in format for b in bin_types+bin_formats)
290-
return False
291-

setup.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@
4747
"Intended Audience :: End Users/Desktop",
4848
"License :: OSI Approved :: MIT License",
4949
"Operating System :: MacOS :: MacOS X",
50+
"Programming Language :: Python :: 2",
5051
"Programming Language :: Python :: 2.7",
51-
"Programming Language :: Python :: 2 :: Only",
52+
"Programming Language :: Python :: 3",
53+
"Programming Language :: Python :: 3.4",
5254
"Topic :: Artistic Software",
5355
"Topic :: Multimedia :: Graphics",
5456
"Topic :: Multimedia :: Graphics :: Editors :: Vector-Based",
@@ -217,12 +219,10 @@ def initialize_options(self):
217219
def finalize_options(self):
218220
pass
219221
def run(self):
220-
try:
221-
import plotdevice.lib
222-
except ImportError:
223-
unbuilt = 'Build the c-extensions with "python setup.py dev" before running tests.'
224-
raise ImportError(unbuilt)
225-
self.spawn([sys.executable, '-m', 'tests'])
222+
from subprocess import call
223+
test_cmd = [sys.executable, '-m', 'tests']
224+
print(" ".join(test_cmd))
225+
call(test_cmd)
226226

227227
class BuildAppCommand(Command):
228228
description = "Build PlotDevice.app with xcode"

0 commit comments

Comments
 (0)