fix ox.api in python3

This commit is contained in:
j 2014-10-04 16:05:00 +02:00
parent b70dfecccc
commit f50b02dd64
2 changed files with 37 additions and 17 deletions

View file

@ -5,7 +5,7 @@ from __future__ import with_statement
from six.moves import http_cookiejar as cookielib from six.moves import http_cookiejar as cookielib
import gzip import gzip
from six import BytesIO from six import BytesIO, PY2
from six.moves import urllib from six.moves import urllib
from types import MethodType from types import MethodType
@ -45,7 +45,10 @@ class API(object):
def _add_method(self, method, name): def _add_method(self, method, name):
if name is None: if name is None:
name = method.func_name name = method.func_name
if PY2:
setattr(self, name, MethodType(method, self, type(self))) setattr(self, name, MethodType(method, self, type(self)))
else:
setattr(self, name, MethodType(method, self))
def _add_action(self, action): def _add_action(self, action):
def method(self, *args, **kw): def method(self, *args, **kw):
@ -57,18 +60,26 @@ class API(object):
return self._request(action, kw) return self._request(action, kw)
if 'doc' in self._properties[action]: if 'doc' in self._properties[action]:
method.__doc__ = self._properties[action]['doc'] method.__doc__ = self._properties[action]['doc']
if PY2:
method.func_name = str(action) method.func_name = str(action)
else:
method.func_name = action
self._add_method(method, action) self._add_method(method, action)
def _json_request(self, url, form): def _json_request(self, url, form):
result = {} result = {}
try: try:
body = str(form) body = form.body()
request = urllib.request.Request(str(url)) if PY2:
request.add_header('Content-type', form.get_content_type()) if not isinstance(url, bytes):
url = url.encode('utf-8')
request = urllib.request.Request(url)
request.add_data(body)
else:
request = urllib.request.Request(url, data=body, method='POST')
request.add_header('Content-Type', form.get_content_type())
request.add_header('Content-Length', str(len(body))) request.add_header('Content-Length', str(len(body)))
request.add_header('Accept-Encoding', 'gzip, deflate') request.add_header('Accept-Encoding', 'gzip, deflate')
request.add_data(body)
f = self._opener.open(request) f = self._opener.open(request)
result = f.read() result = f.read()
if f.headers.get('content-encoding', None) == 'gzip': if f.headers.get('content-encoding', None) == 'gzip':

View file

@ -8,6 +8,8 @@ import mimetypes
import random import random
import sys import sys
from six import PY2
__all__ = ['MultiPartForm'] __all__ = ['MultiPartForm']
@ -36,19 +38,19 @@ class MultiPartForm(object):
def add_field(self, name, value): def add_field(self, name, value):
"""Add a simple field to the form data.""" """Add a simple field to the form data."""
if isinstance(name, unicode): if isinstance(name, bytes):
name = name.encode('utf-8') name = name.decode('utf-8')
if isinstance(value, unicode): if isinstance(value, bytes):
value = value.encode('utf-8') value = value.decode('utf-8')
self.form_fields.append((name, value)) self.form_fields.append((name, value))
return return
def add_file(self, fieldname, filename, fileHandle, mimetype=None): def add_file(self, fieldname, filename, fileHandle, mimetype=None):
"""Add a file to be uploaded.""" """Add a file to be uploaded."""
if isinstance(fieldname, unicode): if isinstance(fieldname, bytes):
fieldname = fieldname.encode('utf-8') fieldname = fieldname.decode('utf-8')
if isinstance(filename, unicode): if isinstance(filename, bytes):
filename = filename.encode('utf-8') filename = filename.decode('utf-8')
if hasattr(fileHandle, 'read'): if hasattr(fileHandle, 'read'):
body = fileHandle.read() body = fileHandle.read()
@ -60,6 +62,12 @@ class MultiPartForm(object):
return return
def __str__(self): def __str__(self):
body = self.body()
if not PY2:
body = body.decode('utf-8')
return body
def body(self):
"""Return a string representing the form data, including attached files.""" """Return a string representing the form data, including attached files."""
# Build a list of lists, each containing "lines" of the # Build a list of lists, each containing "lines" of the
# request. Each part is separated by a boundary string. # request. Each part is separated by a boundary string.
@ -95,5 +103,6 @@ class MultiPartForm(object):
flattened = list(itertools.chain(*parts)) flattened = list(itertools.chain(*parts))
flattened.append('--' + self.boundary + '--') flattened.append('--' + self.boundary + '--')
flattened.append('') flattened.append('')
return '\r\n'.join(flattened) flattened = [part if isinstance(part, bytes) else part.encode('utf-8') for part in flattened]
return b'\r\n'.join(flattened)