Skip to content

Commit

Permalink
Merge branch 'feature/multipart_reader' into feature/unittest_doc
Browse files Browse the repository at this point in the history
  • Loading branch information
SunRunAway committed Jul 5, 2013
2 parents ebad5f7 + 94e9162 commit f418045
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

*.py[cod]

my-test-env.sh

##
## from https://github.com/github/gitignore/blob/master/Python.gitignore
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
## CHANGE LOG

### v6.0.2
### v6.1.0

2013-07-03 issue [#53](https://github.com/qiniu/python-sdk/pull/53)

- 实现最新版的上传API,<http://docs.qiniu.com/api/put.html>
- io.PutExtra更新,废弃callback_params,bucket,和custom_meta,新增params
- 修复[#16](https://github.com/qiniu/python-sdk/issues/16)
- put接口可以传入类文件对象(file-like object)
- 修复[#52](https://github.com/qiniu/python-sdk/issues/52)
Expand Down
4 changes: 3 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ import qiniu.io
extra = qiniu.io.PutExtra()
extra.mime_type = "text/plain"
ret, err = qiniu.io.put(uptoken, key, "hello!", extra)
# data 可以是str或read()able对象
data = StringIO.StringIO("hello!")
ret, err = qiniu.io.put(uptoken, key, data, extra)
if err is not None:
error(err)
return
Expand Down
7 changes: 5 additions & 2 deletions docs/demo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import os
import sys
import StringIO

# @gist import_io
import qiniu.io
Expand Down Expand Up @@ -51,7 +52,7 @@ def _setup():
secret_key = getenv("QINIU_SECRET_KEY")
bucket_name = getenv("QINIU_BUCKET_NAME")
domain = getenv("QINIU_DOMAIN")
pickey = getenv("QINIU_PIC_KEY")
pickey = 'QINIU_UNIT_TEST_PIC'
setup(access_key, secret_key, bucket_name, domain, pickey)

def getenv(name):
Expand Down Expand Up @@ -112,7 +113,9 @@ def put_binary():
extra = qiniu.io.PutExtra()
extra.mime_type = "text/plain"

ret, err = qiniu.io.put(uptoken, key, "hello!", extra)
# data 可以是str或read()able对象
data = StringIO.StringIO("hello!")
ret, err = qiniu.io.put(uptoken, key, data, extra)
if err is not None:
error(err)
return
Expand Down
6 changes: 0 additions & 6 deletions env.sh

This file was deleted.

2 changes: 1 addition & 1 deletion qiniu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
'''

# -*- coding: utf-8 -*-
__version__ = '6.0.1'
__version__ = '6.1.0'
3 changes: 2 additions & 1 deletion qiniu/httplib_chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def send(self, data, is_chunked=False):
if self.debuglevel > 0: print "sendIng a read()able"
datablock = data.read(blocksize)
while datablock:
print 'chunked:', is_chunked
if self.debuglevel > 0:
print 'chunked:', is_chunked
if is_chunked:
if self.debuglevel > 0: print 'send: with trunked data'
lenstr = string.upper(hex(len(datablock))[2:])
Expand Down
8 changes: 4 additions & 4 deletions qiniu/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PutExtra(object):
def put(uptoken, key, data, extra=None):
""" put your data to Qiniu
key, your resource key. if key is None, Qiniu will generate one.
If key is None, the server will generate one.
data may be str or read()able object.
"""
fields = {
Expand All @@ -30,8 +30,8 @@ def put(uptoken, key, data, extra=None):
extra = PutExtra()

if extra.params:
for key in extra.params:
fields[key] = str(extra.params[key])
for k in extra.params:
fields[k] = str(extra.params[k])

if extra.check_crc:
fields["crc32"] = str(extra.crc32)
Expand All @@ -55,7 +55,7 @@ def put(uptoken, key, data, extra=None):
def put_file(uptoken, key, localfile, extra=None):
""" put a file to Qiniu
key, your resource key. if key is None, Qiniu will generate one.
If key is None, the server will generate one.
"""
if extra is not None and extra.check_crc == 1:
extra.crc32 = _get_file_crc32(localfile)
Expand Down
9 changes: 8 additions & 1 deletion qiniu/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def encode_multipart_formdata(self, fields, files):
L.append('')
L.append('--' + BOUNDARY)
disposition = "Content-Disposition: form-data;"
L.append('%s name="file"; filename="%s"' % (disposition, file_info.get('filename')))
filename = _qiniu_escape(file_info.get('filename'))
L.append('%s name="file"; filename="%s"' % (disposition, filename))
L.append('Content-Type: %s' % file_info.get('content_type', 'application/octet-stream'))
L.append('')
L.append('')
Expand All @@ -127,6 +128,12 @@ def encode_multipart_formdata(self, fields, files):
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, MultiReader(readers)

def _qiniu_escape(s):
edits = [('\\', '\\\\'), ('\"', '\\\"')]
for (search, replace) in edits:
s = s.replace(search, replace)
return s


class MultiReader(object):
""" class MultiReader([readers...])
Expand Down
2 changes: 1 addition & 1 deletion qiniu/rs/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import qiniu.conf

pic = "http://cheneya.qiniudn.com/hello_jpg"
key = 'QINIU_UNIT_TEST_PIC'

def setUp():
qiniu.conf.ACCESS_KEY = os.getenv("QINIU_ACCESS_KEY")
qiniu.conf.SECRET_KEY = os.getenv("QINIU_SECRET_KEY")
key = os.getenv("QINIU_PIC_KEY")
bucket_name = os.getenv("QINIU_BUCKET_NAME")

policy = qiniu.rs.PutPolicy(bucket_name)
Expand Down
4 changes: 2 additions & 2 deletions qiniu/rs/test/rs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def r(length):

conf.ACCESS_KEY = os.getenv("QINIU_ACCESS_KEY")
conf.SECRET_KEY = os.getenv("QINIU_SECRET_KEY")
key = os.getenv("QINIU_PIC_KEY")
key = 'QINIU_UNIT_TEST_PIC'
bucket_name = os.getenv("QINIU_BUCKET_NAME")
noexist_key = os.getenv("QINIU_NOEXIST_PIC_KEY")
noexist_key = 'QINIU_UNIT_TEST_NOEXIST' + r(30)
key2 = "rs_demo_test_key_1_" + r(5)
key3 = "rs_demo_test_key_2_" + r(5)
key4 = "rs_demo_test_key_3_" + r(5)
Expand Down
2 changes: 1 addition & 1 deletion qiniu/rs/test/rs_token_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
conf.SECRET_KEY = os.getenv("QINIU_SECRET_KEY")
bucket_name = os.getenv("QINIU_BUCKET_NAME")
domain = os.getenv("QINIU_DOMAIN")
key = os.getenv("QINIU_PIC_KEY")
key = 'QINIU_UNIT_TEST_PIC'

class TestToken(unittest.TestCase):
def test_put_policy(self):
Expand Down
29 changes: 26 additions & 3 deletions qiniu/test/io_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
policy = rs.PutPolicy(bucket_name)
extra = io.PutExtra()
extra.mime_type = "text/plain"
extra.params = {'x:a':'a'}

def r(length):
lib = string.ascii_uppercase
Expand All @@ -36,23 +37,41 @@ def test_put():
extra.crc32 = binascii.crc32(data) & 0xFFFFFFFF
ret, err = io.put(policy.token(), key, data, extra)
assert err is None
assert ret['key'] == key

def test_put_same_crc():
key = "test_%s" % r(9)
data = "hello bubby!"
extra.check_crc = 2
ret, err = io.put(policy.token(), key, data, extra)
assert err is None
assert ret['key'] == key

def test_put_no_key():
data = r(100)
ret, err = io.put(policy.token(), key=None, data=data)
extra.check_crc = 0
ret, err = io.put(policy.token(), key=None, data=data, extra=extra)
assert err is None
assert ret['hash'] == ret['key']

def test_put_quote_key():
data = r(100)
key = 'a\\b\\c"你好' + r(9)
ret, err = io.put(policy.token(), key, data)
print err
assert err is None
assert ret['key'].encode('utf8') == key

data = r(100)
key = u'a\\b\\c"你好' + r(9)
ret, err = io.put(policy.token(), key, data)
assert err is None
assert ret['key'] == key

def test_put_unicode1():
key = "test_%s" % r(9) + '你好'
data = key
ret, err = io.put(policy.token(), key, data)
ret, err = io.put(policy.token(), key, data, extra)
assert err is None
assert ret[u'key'].endswith(u'你好')

Expand Down Expand Up @@ -86,12 +105,14 @@ def test_put_StringIO():
data = cStringIO.StringIO('hello buddy!')
ret, err = io.put(policy.token(), key, data)
assert err is None
assert ret['key'] == key

def test_put_urlopen():
key = "test_%s" % r(9)
data = urllib.urlopen('http://cheneya.qiniudn.com/hello_jpg')
ret, err = io.put(policy.token(), key, data)
assert err is None
assert ret['key'] == key

def test_put_no_length():
class test_reader(object):
Expand All @@ -113,10 +134,12 @@ def read(self, n=None):
extra.crc32 = binascii.crc32('abc') & 0xFFFFFFFF
ret, err = io.put(policy.token(), key, data, extra)
assert err is None
assert ret['key'] == key

test_put()
test_put_same_crc()
test_put_no_key()
test_put_quote_key()
test_put_unicode1()
test_put_unicode2()
test_put_unicode3()
Expand All @@ -132,7 +155,7 @@ def test_put_file(self):
extra.check_crc = 1
ret, err = io.put_file(policy.token(), key, localfile, extra)
assert err is None
assert ret is not None
assert ret['key'] == key

def test_put_crc_fail(self):
key = "test_%s" % r(9)
Expand Down
4 changes: 4 additions & 0 deletions test-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export QINIU_ACCESS_KEY="<access_key>"
export QINIU_SECRET_KEY="<secret_key>"
export QINIU_BUCKET_NAME="<bucket_name>"
export QINIU_DOMAIN="<bucket_domain>"

0 comments on commit f418045

Please sign in to comment.