def test_unicode_quote(): """ Ensure that unicode strings are encoded as utf-8 properly for use with the quote method of the urlparse stdlib. """ assert unicode_quote("non-unicode text") == "non-unicode%20text" assert unicode_quote(u'\xe1gua.txt') == "%C3%A1gua.txt"
def send_object(dstobj, iterable, headers={}): """ Imported and modified from cloudfiles.storage_object.Object.send, to allow specifying custom headers """ assert dstobj.size is not None if not dstobj.content_type: dstobj.content_type = 'application/octet-stream' path = "/%s/%s/%s" % (dstobj.container.conn.uri.rstrip('/'), unicode_quote(dstobj.container.name), unicode_quote(dstobj.name)) headers.update(dstobj._make_headers()) headers['X-Auth-Token'] = dstobj.container.conn.token headers['User-Agent'] = dstobj.container.conn.user_agent http = dstobj.container.conn.connection http.putrequest('PUT', path) for key, value in headers.iteritems(): http.putheader(key, value) http.endheaders() response = None transferred = 0 try: for chunk in iterable: http.send(chunk) transferred += len(chunk) # If the generator didn't yield enough data, stop, drop, and roll. if transferred < dstobj.size: # possible cause: source's container listing has different size than actual file print >> sys.stderr, "%s %s incomplete send: transferred %d/%d" % \ (dstobj.container.name, dstobj.name, transferred, dstobj.size) raise cloudfiles.errors.IncompleteSend() response = http.getresponse() buff = response.read() except socket.timeout, err: if response: # pylint: disable-msg=E1101 response.read() raise err
def varnish_rewrite(obj): match = re.match(r'^(?P<proj>[^\-]+)-(?P<lang>[^\-]+)-(?P<repo>[^\-]+)-(?P<zone>[^\-\.]+)(\..*)?$', obj.container.name) if match: host = 'upload.wikimedia.org' uri = '/%s/%s/%s/%s' % (match.group('proj'), match.group('lang'), match.group('zone'), unicode_quote(obj.name)) return host, uri else: raise cloudfiles.errors.InvalidContainerName()