Skip to content

Commit a12c13c

Browse files
authored
Merge pull request #35 from Yulegu-bj/master
针对"ImportError: cannot import name parse"问题兼容
2 parents 9d5e373 + 7c1cf4c commit a12c13c

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

ufile/compact.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ def u(data):
4646
def url_parse(data):
4747
return urllib.urlencode(data)
4848

49+
def quote(data):
50+
return urllib.quote(data)
51+
52+
4953
elif is_py3:
5054
from urllib import parse
5155
import io
@@ -74,3 +78,6 @@ def u(data):
7478
def url_parse(data):
7579
return parse.urlencode(data)
7680

81+
def quote(data):
82+
return parse.quote(data)
83+

ufile/filemanager.py

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
# -*- coding: utf-8 -*-
2-
3-
4-
import os
5-
import time
62
import hashlib
73
import json
8-
from .baseufile import BaseUFile
9-
from .httprequest import _put_stream, _put_file, _post_file, ResponseInfo, _uploadhit_file, _download_file, _delete_file, _getfilelist,_head_file, _restore_file, _classswitch_file, _copy_file, _rename_file, _listobjects
10-
from .util import _check_dict, ufile_put_url, ufile_post_url, file_etag, ufile_uploadhit_url, ufile_getfilelist_url, mimetype_from_file, ufile_restore_url, ufile_classswitch_url, ufile_copy_url, ufile_rename_url, ufile_listobjects_url
11-
from .logger import logger
12-
from .compact import b, s, u, url_parse
4+
import os
5+
import string
6+
import time
7+
138
from . import config
9+
from .baseufile import BaseUFile
10+
from .compact import b, s, u, url_parse, quote
1411
from .config import BLOCKSIZE
15-
import string
16-
from urllib import parse
12+
from .httprequest import _put_stream, _put_file, _post_file, ResponseInfo, _uploadhit_file, _download_file, \
13+
_delete_file, _getfilelist, _head_file, _restore_file, _classswitch_file, _copy_file, _rename_file, _listobjects
14+
from .logger import logger
15+
from .util import _check_dict, ufile_put_url, ufile_post_url, file_etag, ufile_uploadhit_url, ufile_getfilelist_url, \
16+
mimetype_from_file, ufile_restore_url, ufile_classswitch_url, ufile_copy_url, ufile_rename_url, \
17+
ufile_listobjects_url
1718

1819

1920
class FileManager(BaseUFile):
2021
"""
2122
UCloud UFile普通上传文件类
2223
"""
24+
2325
def __init__(self, public_key, private_key, upload_suffix=None, download_suffix=None):
2426
"""
2527
初始化 PutUFile 实例
@@ -100,7 +102,7 @@ def putfile(self, bucket, key, localfile, header=None):
100102
data.seek(0, os.SEEK_SET)
101103
authorization = self.authorization('put', bucket, key, header)
102104
header['Authorization'] = authorization
103-
if file_size!=0:
105+
if file_size != 0:
104106
header['Content-Length'] = str(file_size)
105107
url = ufile_put_url(bucket, key, upload_suffix=self.__upload_suffix)
106108
logger.info('start put file {0} to bucket {1} as {2}'.format(localfile, bucket, key))
@@ -261,15 +263,16 @@ def private_download_url(self, bucket, key, expires=None, header=None, internal=
261263
header['Expires'] = s(str(expires))
262264

263265
signature = self.signature(bucket, key, 'get', header)
264-
query = { 'UCloudPublicKey': self._public_key(),
265-
'Expires': str(expires),
266-
'Signature': signature }
266+
query = {'UCloudPublicKey': self._public_key(),
267+
'Expires': str(expires),
268+
'Signature': signature}
267269
query_str = url_parse(query)
268270

269271
url = self._get_download_domain(bucket) + '/' + key
270272
if internal:
271273
return url + '?' + query_str
272-
return url + '?UCloudPublicKey={0}&Expires={1}&Signature={2}'.format(self._public_key(), str(expires), signature)
274+
return url + '?UCloudPublicKey={0}&Expires={1}&Signature={2}'.format(self._public_key(), str(expires),
275+
signature)
273276

274277
def private_head_url(self, bucket, key, expires=None, header=None):
275278
"""
@@ -294,7 +297,8 @@ def private_head_url(self, bucket, key, expires=None, header=None):
294297
header['Expires'] = s(str(expires))
295298

296299
signature = self.signature(bucket, key, 'head', header)
297-
return self._get_download_domain(bucket) + '/' + key + '?UCloudPublicKey={0}&Expires={1}&Signature={2}'.format(self._public_key(), str(expires), signature)
300+
return self._get_download_domain(bucket) + '/' + key + '?UCloudPublicKey={0}&Expires={1}&Signature={2}'.format(
301+
self._public_key(), str(expires), signature)
298302

299303
def deletefile(self, bucket, key, header=None):
300304
"""
@@ -350,7 +354,11 @@ def getfilelist(self, bucket, prefix=None, marker=None, limit=None, header=None)
350354
param['prefix'] = s(prefix)
351355
if limit is not None and isinstance(limit, int):
352356
param['limit'] = s(str(limit))
353-
info_message = ''.join(['start get file list from bucket {0}'.format(bucket), '' if marker is None else ', marker: {0}'.format(marker if isinstance(marker, str) else marker.encode('utf-8')), '' if limit is None else ', limit: {0}'.format(limit), '' if prefix is None else ', prefix: {0}'.format(prefix)])
357+
info_message = ''.join(['start get file list from bucket {0}'.format(bucket),
358+
'' if marker is None else ', marker: {0}'.format(
359+
marker if isinstance(marker, str) else marker.encode('utf-8')),
360+
'' if limit is None else ', limit: {0}'.format(limit),
361+
'' if prefix is None else ', prefix: {0}'.format(prefix)])
354362
logger.info(info_message)
355363
url = ufile_getfilelist_url(bucket, upload_suffix=self.__upload_suffix)
356364
return _getfilelist(url, header, param)
@@ -366,7 +374,7 @@ def head_file(self, bucket, key, header=None):
366374
:return: ResponseInfo: 响应的具体信息,UCloud UFile 服务器返回信息或者网络链接异常
367375
"""
368376
if header is None:
369-
header=dict()
377+
header = dict()
370378
else:
371379
_check_dict(header)
372380
if 'User-Agent' not in header:
@@ -389,10 +397,10 @@ def compare_file_etag(self, bucket, remotekey, localfile):
389397
:param localfile: string类型,本地文件的路径
390398
:return:True为比对一致,False为不一致
391399
"""
392-
ret,resp=self.head_file(bucket, remotekey)
393-
remote_etag=resp.etag.strip('\"')
394-
local_etag=file_etag(localfile, BLOCKSIZE)
395-
return (remote_etag==local_etag)
400+
ret, resp = self.head_file(bucket, remotekey)
401+
remote_etag = resp.etag.strip('\"')
402+
local_etag = file_etag(localfile, BLOCKSIZE)
403+
return (remote_etag == local_etag)
396404

397405
def restore_file(self, bucket, key, header=None):
398406
"""
@@ -405,7 +413,7 @@ def restore_file(self, bucket, key, header=None):
405413
:return: ResponseInfo: 响应的具体信息,UCloud UFile 服务器返回信息或者网络链接异常
406414
"""
407415
if header is None:
408-
header=dict()
416+
header = dict()
409417
else:
410418
_check_dict(header)
411419
if 'User-Agent' not in header:
@@ -432,7 +440,7 @@ def class_switch_file(self, bucket, key, storageclass, header=None):
432440
:return: ResponseInfo: 响应的具体信息,UCloud UFile 服务器返回信息或者网络链接异常
433441
"""
434442
if header is None:
435-
header=dict()
443+
header = dict()
436444
else:
437445
_check_dict(header)
438446
if 'User-Agent' not in header:
@@ -469,7 +477,7 @@ def copy(self, bucket, key, srcbucket, srckey, header=None):
469477
header['User-Agent'] = config.get_default('user_agent')
470478

471479
# update request header
472-
srckey = parse.quote(srckey)
480+
srckey = quote(srckey)
473481
header['X-Ufile-Copy-Source'] = "/" + srcbucket + "/" + srckey
474482
header['Content-Length'] = str(0)
475483
authorization = self.authorization('put', bucket, key, header)
@@ -518,8 +526,12 @@ def listobjects(self, bucket, prefix=None, marker=None, maxkeys=None, delimiter=
518526
authorization = self.authorization('get', bucket, '', header, '', 'listobjects', param)
519527
header['Authorization'] = authorization
520528

521-
info_message = ''.join(['start list objects from bucket {0}'.format(bucket), '' if marker is None else ', marker: {0}'.format(marker if isinstance(marker, str) else marker.encode('utf-8')), '' if maxkeys is None else ', maxkeys: {0}'.format(maxkeys), '' if prefix is None else ', prefix: {0}'.format(prefix), '' if delimiter is None else ', delimiter: {0}'.format(delimiter)])
529+
info_message = ''.join(['start list objects from bucket {0}'.format(bucket),
530+
'' if marker is None else ', marker: {0}'.format(
531+
marker if isinstance(marker, str) else marker.encode('utf-8')),
532+
'' if maxkeys is None else ', maxkeys: {0}'.format(maxkeys),
533+
'' if prefix is None else ', prefix: {0}'.format(prefix),
534+
'' if delimiter is None else ', delimiter: {0}'.format(delimiter)])
522535
logger.info(info_message)
523536
url = ufile_listobjects_url(bucket, upload_suffix=self.__upload_suffix)
524537
return _listobjects(url, header, param)
525-

0 commit comments

Comments
 (0)