|
1 | 1 | # encoding: utf-8 |
2 | 2 | import os, sys, re |
3 | 3 | from operator import attrgetter |
| 4 | +PY2 = sys.version_info[0] == 2 |
4 | 5 |
|
5 | 6 | # files & io |
6 | 7 | from io import open, StringIO, BytesIO |
7 | 8 | from os.path import abspath, dirname, exists, join, splitext |
8 | 9 | from plotdevice import DeviceError, INTERNAL |
| 10 | +text_type = str if not PY2 else unicode |
9 | 11 |
|
10 | 12 | # data formats |
11 | 13 | import json, csv |
|
14 | 16 | from xml.parsers import expat |
15 | 17 |
|
16 | 18 | # http |
17 | | -import requests |
18 | 19 | from urlparse import urlparse |
19 | 20 | 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 | | - |
44 | 21 |
|
45 | 22 |
|
46 | 23 | ### XML handling ### |
@@ -211,6 +188,45 @@ def csv_dialect(fd): |
211 | 188 | return csv.Sniffer().sniff(snippet) |
212 | 189 |
|
213 | 190 |
|
| 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 | + |
214 | 230 | ### File/URL Reader ### |
215 | 231 |
|
216 | 232 | def read(pth, format=None, encoding=None, cols=None, **kwargs): |
@@ -280,12 +296,3 @@ def read(pth, format=None, encoding=None, cols=None, **kwargs): |
280 | 296 | else: |
281 | 297 | return fd.read() |
282 | 298 |
|
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 | | - |
|
0 commit comments