Skip to content

Commit

Permalink
Merge branch 'feature/multipart_reader' into feature/add_docs
Browse files Browse the repository at this point in the history
  • Loading branch information
SunRunAway committed Jul 5, 2013
2 parents f664e3a + b246a33 commit e7f9310
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,6 +4,7 @@

*.py[cod]

my-test-env.sh

##
## from https://github.com/github/gitignore/blob/master/Python.gitignore
Expand Down
5 changes: 4 additions & 1 deletion .travis.yml
Expand Up @@ -3,7 +3,10 @@ python:
- "2.6"
- "2.7"
before_script:
- source env.sh
- export QINIU_ACCESS_KEY="X0XpjFmLMTJpHB_ESHjeolCtipk-1U3Ok7LVTdoN"
- export QINIU_SECRET_KEY="wenlwkU1AYwNBf7Q9cCoG4VT_GYyrHE9AS_R2u81"
- export QINIU_TEST_BUCKET="pysdk"
- export QINIU_TEST_DOMAIN="pysdk.qiniudn.com"
- export PYTHONPATH="$PYTHONPATH:."
script:
- python setup.py nosetests
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
@@ -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
6 changes: 3 additions & 3 deletions docs/demo.py
Expand Up @@ -51,9 +51,9 @@ def _setup():
if access_key is None:
exit("请配置环境变量 QINIU_ACCESS_KEY")
secret_key = getenv("QINIU_SECRET_KEY")
bucket_name = getenv("QINIU_BUCKET_NAME")
domain = getenv("QINIU_DOMAIN")
pickey = getenv("QINIU_PIC_KEY")
bucket_name = getenv("QINIU_TEST_BUCKET")
domain = getenv("QINIU_TEST_DOMAIN")
pickey = 'QINIU_UNIT_TEST_PIC'
setup(access_key, secret_key, bucket_name, domain, pickey)

def getenv(name):
Expand Down
6 changes: 0 additions & 6 deletions env.sh

This file was deleted.

2 changes: 1 addition & 1 deletion qiniu/__init__.py
Expand Up @@ -7,4 +7,4 @@
'''

# -*- coding: utf-8 -*-
__version__ = '6.0.1'
__version__ = '6.1.0'
4 changes: 2 additions & 2 deletions qiniu/io.py
Expand Up @@ -32,8 +32,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 Down
9 changes: 8 additions & 1 deletion qiniu/rpc.py
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
4 changes: 2 additions & 2 deletions qiniu/rs/test/__init__.py
Expand Up @@ -7,12 +7,12 @@
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")
bucket_name = os.getenv("QINIU_TEST_BUCKET")

policy = qiniu.rs.PutPolicy(bucket_name)
uptoken = policy.token()
Expand Down
6 changes: 3 additions & 3 deletions qiniu/rs/test/rs_test.py
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")
bucket_name = os.getenv("QINIU_BUCKET_NAME")
noexist_key = os.getenv("QINIU_NOEXIST_PIC_KEY")
key = 'QINIU_UNIT_TEST_PIC'
bucket_name = os.getenv("QINIU_TEST_BUCKET")
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
6 changes: 3 additions & 3 deletions qiniu/rs/test/rs_token_test.py
Expand Up @@ -14,9 +14,9 @@

conf.ACCESS_KEY = os.getenv("QINIU_ACCESS_KEY")
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")
bucket_name = os.getenv("QINIU_TEST_BUCKET")
domain = os.getenv("QINIU_TEST_DOMAIN")
key = 'QINIU_UNIT_TEST_PIC'

class TestToken(unittest.TestCase):
def test_put_policy(self):
Expand Down
31 changes: 27 additions & 4 deletions qiniu/test/io_test.py
Expand Up @@ -16,11 +16,12 @@

conf.ACCESS_KEY = os.getenv("QINIU_ACCESS_KEY")
conf.SECRET_KEY = os.getenv("QINIU_SECRET_KEY")
bucket_name = os.getenv("QINIU_BUCKET_NAME")
bucket_name = os.getenv("QINIU_TEST_BUCKET")

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
2 changes: 1 addition & 1 deletion qiniu/test/resumable_io_test.py
Expand Up @@ -16,7 +16,7 @@
from qiniu import resumable_io
from qiniu import rs

bucket = os.getenv("QINIU_BUCKET_NAME")
bucket = os.getenv("QINIU_TEST_BUCKET")
conf.ACCESS_KEY = os.getenv("QINIU_ACCESS_KEY")
conf.SECRET_KEY = os.getenv("QINIU_SECRET_KEY")

Expand Down
2 changes: 1 addition & 1 deletion qiniu/test/rsf_test.py
Expand Up @@ -6,7 +6,7 @@
import os
conf.ACCESS_KEY = os.getenv("QINIU_ACCESS_KEY")
conf.SECRET_KEY = os.getenv("QINIU_SECRET_KEY")
bucket_name = os.getenv("QINIU_BUCKET_NAME")
bucket_name = os.getenv("QINIU_TEST_BUCKET")

class TestRsf(unittest.TestCase):
def test_list_prefix(self):
Expand Down
4 changes: 4 additions & 0 deletions test-env.sh
@@ -0,0 +1,4 @@
export QINIU_ACCESS_KEY="<access_key>"
export QINIU_SECRET_KEY="<secret_key>"
export QINIU_TEST_BUCKET="<bucket_name>"
export QINIU_TEST_DOMAIN="<bucket_domain>"

0 comments on commit e7f9310

Please sign in to comment.