pull python-ox changes

This commit is contained in:
j 2014-10-04 21:08:57 +02:00
commit 7040fdd564
13 changed files with 75 additions and 57 deletions

View file

@ -10,6 +10,7 @@ import os
from six import BytesIO
import time
from six.moves import urllib
from six import PY2
import sqlite3
from .utils import json
@ -24,6 +25,7 @@ COMPRESS_TYPES = (
'text/html',
'text/plain',
'text/xml',
'application/json',
'application/xhtml+xml',
'application/x-javascript',
'application/javascript',
@ -117,9 +119,8 @@ def save_url(url, filename, overwrite=False):
if not os.path.exists(dirname):
os.makedirs(dirname)
data = read_url(url)
f = open(filename, 'w')
f.write(data)
f.close()
with open(filename, 'wb') as f:
f.write(data)
def cache_path():
return os.environ.get('oxCACHE', os.path.expanduser('~/.ox/cache'))
@ -203,7 +204,7 @@ class SQLiteCache(Cache):
elif value == 'data':
if row[1] == 1:
r = zlib.decompress(r)
else:
elif PY2:
r = str(r)
break
@ -290,7 +291,7 @@ class FileCache(Cache):
if value == 'headers':
r = info['headers']
else:
with open(f) as data:
with open(f, 'rb') as data:
r = data.read()
if info['compressed']:
r = zlib.decompress(r)
@ -321,9 +322,11 @@ class FileCache(Cache):
if not info['only_headers']:
if info['compressed']:
data = zlib.compress(data)
with open(f, 'w') as _f:
elif not isinstance(data, str):
data = data.encode('utf-8')
with open(f, 'wb') as _f:
_f.write(data)
with open(i, 'w') as _i:
with open(i, 'wb') as _i:
json.dump(info, _i)
if cache_path().startswith('fs:'):