本文整理汇总了Python中swift.common.header_key_dict.HeaderKeyDict.setdefault方法的典型用法代码示例。如果您正苦于以下问题:Python HeaderKeyDict.setdefault方法的具体用法?Python HeaderKeyDict.setdefault怎么用?Python HeaderKeyDict.setdefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swift.common.header_key_dict.HeaderKeyDict
的用法示例。
在下文中一共展示了HeaderKeyDict.setdefault方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from swift.common.header_key_dict import HeaderKeyDict [as 别名]
# 或者: from swift.common.header_key_dict.HeaderKeyDict import setdefault [as 别名]
def __call__(self, env, start_response):
method = env['REQUEST_METHOD']
path = env['PATH_INFO']
_, acc, cont, obj = split_path(env['PATH_INFO'], 0, 4,
rest_with_last=True)
if env.get('QUERY_STRING'):
path += '?' + env['QUERY_STRING']
if 'swift.authorize' in env:
resp = env['swift.authorize'](swob.Request(env))
if resp:
return resp(env, start_response)
req_headers = swob.Request(env).headers
self.swift_sources.append(env.get('swift.source'))
self.txn_ids.append(env.get('swift.trans_id'))
try:
resp_class, raw_headers, body = self._find_response(method, path)
headers = HeaderKeyDict(raw_headers)
except KeyError:
if (env.get('QUERY_STRING')
and (method, env['PATH_INFO']) in self._responses):
resp_class, raw_headers, body = self._find_response(
method, env['PATH_INFO'])
headers = HeaderKeyDict(raw_headers)
elif method == 'HEAD' and ('GET', path) in self._responses:
resp_class, raw_headers, body = self._find_response(
'GET', path)
body = None
headers = HeaderKeyDict(raw_headers)
elif method == 'GET' and obj and path in self.uploaded:
resp_class = swob.HTTPOk
headers, body = self.uploaded[path]
else:
raise KeyError("Didn't find %r in allowed responses" % (
(method, path),))
self._calls.append((method, path, req_headers))
# simulate object PUT
if method == 'PUT' and obj:
input = env['wsgi.input'].read()
etag = md5(input).hexdigest()
headers.setdefault('Etag', etag)
headers.setdefault('Content-Length', len(input))
# keep it for subsequent GET requests later
self.uploaded[path] = (deepcopy(headers), input)
if "CONTENT_TYPE" in env:
self.uploaded[path][0]['Content-Type'] = env["CONTENT_TYPE"]
# range requests ought to work, which require conditional_response=True
req = swob.Request(env)
resp = resp_class(req=req, headers=headers, body=body,
conditional_response=req.method in ('GET', 'HEAD'))
wsgi_iter = resp(env, start_response)
self.mark_opened(path)
return LeakTrackingIter(wsgi_iter, self, path)
示例2: process_object_update
# 需要导入模块: from swift.common.header_key_dict import HeaderKeyDict [as 别名]
# 或者: from swift.common.header_key_dict.HeaderKeyDict import setdefault [as 别名]
def process_object_update(self, update_path, device, policy):
"""
Process the object information to be updated and update.
:param update_path: path to pickled object update file
:param device: path to device
:param policy: storage policy of object update
"""
try:
update = pickle.load(open(update_path, 'rb'))
except Exception:
self.logger.exception(
_('ERROR Pickle problem, quarantining %s'), update_path)
self.stats.quarantines += 1
self.logger.increment('quarantines')
target_path = os.path.join(device, 'quarantined', 'objects',
os.path.basename(update_path))
renamer(update_path, target_path, fsync=False)
return
successes = update.get('successes', [])
part, nodes = self.get_container_ring().get_nodes(
update['account'], update['container'])
obj = '/%s/%s/%s' % \
(update['account'], update['container'], update['obj'])
headers_out = HeaderKeyDict(update['headers'])
headers_out['user-agent'] = 'object-updater %s' % os.getpid()
headers_out.setdefault('X-Backend-Storage-Policy-Index',
str(int(policy)))
events = [spawn(self.object_update,
node, part, update['op'], obj, headers_out)
for node in nodes if node['id'] not in successes]
success = True
new_successes = False
for event in events:
event_success, node_id = event.wait()
if event_success is True:
successes.append(node_id)
new_successes = True
else:
success = False
if success:
self.stats.successes += 1
self.logger.increment('successes')
self.logger.debug('Update sent for %(obj)s %(path)s',
{'obj': obj, 'path': update_path})
self.stats.unlinks += 1
self.logger.increment('unlinks')
os.unlink(update_path)
else:
self.stats.failures += 1
self.logger.increment('failures')
self.logger.debug('Update failed for %(obj)s %(path)s',
{'obj': obj, 'path': update_path})
if new_successes:
update['successes'] = successes
write_pickle(update, update_path, os.path.join(
device, get_tmp_dir(policy)))
示例3: __call__
# 需要导入模块: from swift.common.header_key_dict import HeaderKeyDict [as 别名]
# 或者: from swift.common.header_key_dict.HeaderKeyDict import setdefault [as 别名]
def __call__(self, env, start_response):
method = env["REQUEST_METHOD"]
if method not in self.ALLOWED_METHODS:
raise HTTPNotImplemented()
path = env["PATH_INFO"]
_, acc, cont, obj = split_path(env["PATH_INFO"], 0, 4, rest_with_last=True)
if env.get("QUERY_STRING"):
path += "?" + env["QUERY_STRING"]
if "swift.authorize" in env:
resp = env["swift.authorize"](swob.Request(env))
if resp:
return resp(env, start_response)
req_headers = swob.Request(env).headers
self.swift_sources.append(env.get("swift.source"))
self.txn_ids.append(env.get("swift.trans_id"))
try:
resp_class, raw_headers, body = self._find_response(method, path)
headers = HeaderKeyDict(raw_headers)
except KeyError:
if env.get("QUERY_STRING") and (method, env["PATH_INFO"]) in self._responses:
resp_class, raw_headers, body = self._find_response(method, env["PATH_INFO"])
headers = HeaderKeyDict(raw_headers)
elif method == "HEAD" and ("GET", path) in self._responses:
resp_class, raw_headers, body = self._find_response("GET", path)
body = None
headers = HeaderKeyDict(raw_headers)
elif method == "GET" and obj and path in self.uploaded:
resp_class = swob.HTTPOk
headers, body = self.uploaded[path]
else:
raise KeyError("Didn't find %r in allowed responses" % ((method, path),))
self._calls.append((method, path, req_headers))
# simulate object PUT
if method == "PUT" and obj:
input = env["wsgi.input"].read()
etag = md5(input).hexdigest()
headers.setdefault("Etag", etag)
headers.setdefault("Content-Length", len(input))
# keep it for subsequent GET requests later
self.uploaded[path] = (deepcopy(headers), input)
if "CONTENT_TYPE" in env:
self.uploaded[path][0]["Content-Type"] = env["CONTENT_TYPE"]
# range requests ought to work, which require conditional_response=True
req = swob.Request(env)
resp = resp_class(req=req, headers=headers, body=body, conditional_response=req.method in ("GET", "HEAD"))
wsgi_iter = resp(env, start_response)
self.mark_opened(path)
return LeakTrackingIter(wsgi_iter, self, path)
示例4: test_setdefault
# 需要导入模块: from swift.common.header_key_dict import HeaderKeyDict [as 别名]
# 或者: from swift.common.header_key_dict.HeaderKeyDict import setdefault [as 别名]
def test_setdefault(self):
headers = HeaderKeyDict()
# it gets set
headers.setdefault('x-rubber-ducky', 'the one')
self.assertEqual(headers['X-Rubber-Ducky'], 'the one')
# it has the right return value
ret = headers.setdefault('x-boat', 'dinghy')
self.assertEqual(ret, 'dinghy')
ret = headers.setdefault('x-boat', 'yacht')
self.assertEqual(ret, 'dinghy')
# shouldn't crash
headers.setdefault('x-sir-not-appearing-in-this-request', None)
示例5: __call__
# 需要导入模块: from swift.common.header_key_dict import HeaderKeyDict [as 别名]
# 或者: from swift.common.header_key_dict.HeaderKeyDict import setdefault [as 别名]
def __call__(self, env, start_response):
method = env['REQUEST_METHOD']
if method not in self.ALLOWED_METHODS:
raise HTTPNotImplemented()
path = env['PATH_INFO']
_, acc, cont, obj = split_path(env['PATH_INFO'], 0, 4,
rest_with_last=True)
if env.get('QUERY_STRING'):
path += '?' + env['QUERY_STRING']
if 'swift.authorize' in env:
resp = env['swift.authorize'](swob.Request(env))
if resp:
return resp(env, start_response)
req = swob.Request(env)
self.swift_sources.append(env.get('swift.source'))
self.txn_ids.append(env.get('swift.trans_id'))
try:
resp_class, raw_headers, body = self._find_response(method, path)
headers = HeaderKeyDict(raw_headers)
except KeyError:
if (env.get('QUERY_STRING')
and (method, env['PATH_INFO']) in self._responses):
resp_class, raw_headers, body = self._find_response(
method, env['PATH_INFO'])
headers = HeaderKeyDict(raw_headers)
elif method == 'HEAD' and ('GET', path) in self._responses:
resp_class, raw_headers, body = self._find_response(
'GET', path)
body = None
headers = HeaderKeyDict(raw_headers)
elif method == 'GET' and obj and path in self.uploaded:
resp_class = swob.HTTPOk
headers, body = self.uploaded[path]
else:
raise KeyError("Didn't find %r in allowed responses" % (
(method, path),))
# simulate object PUT
if method == 'PUT' and obj:
put_body = ''.join(iter(env['wsgi.input'].read, ''))
if 'swift.callback.update_footers' in env:
footers = HeaderKeyDict()
env['swift.callback.update_footers'](footers)
req.headers.update(footers)
etag = md5(put_body).hexdigest()
headers.setdefault('Etag', etag)
headers.setdefault('Content-Length', len(put_body))
# keep it for subsequent GET requests later
self.uploaded[path] = (dict(req.headers), put_body)
if "CONTENT_TYPE" in env:
self.uploaded[path][0]['Content-Type'] = env["CONTENT_TYPE"]
# simulate object POST
elif method == 'POST' and obj:
metadata, data = self.uploaded.get(path, ({}, None))
# select items to keep from existing...
new_metadata = dict(
(k, v) for k, v in metadata.items()
if (not is_user_meta('object', k) and not
is_object_transient_sysmeta(k)))
# apply from new
new_metadata.update(
dict((k, v) for k, v in req.headers.items()
if (is_user_meta('object', k) or
is_object_transient_sysmeta(k) or
k.lower == 'content-type')))
self.uploaded[path] = new_metadata, data
# note: tests may assume this copy of req_headers is case insensitive
# so we deliberately use a HeaderKeyDict
self._calls.append(
FakeSwiftCall(method, path, HeaderKeyDict(req.headers)))
backend_etag_header = req.headers.get('X-Backend-Etag-Is-At')
conditional_etag = None
if backend_etag_header and backend_etag_header in headers:
# Apply conditional etag overrides
conditional_etag = headers[backend_etag_header]
# range requests ought to work, hence conditional_response=True
if isinstance(body, list):
resp = resp_class(
req=req, headers=headers, app_iter=body,
conditional_response=req.method in ('GET', 'HEAD'),
conditional_etag=conditional_etag)
else:
resp = resp_class(
req=req, headers=headers, body=body,
conditional_response=req.method in ('GET', 'HEAD'),
conditional_etag=conditional_etag)
wsgi_iter = resp(env, start_response)
self.mark_opened(path)
return LeakTrackingIter(wsgi_iter, self, path)
示例6: __call__
# 需要导入模块: from swift.common.header_key_dict import HeaderKeyDict [as 别名]
# 或者: from swift.common.header_key_dict.HeaderKeyDict import setdefault [as 别名]
def __call__(self, env, start_response):
method = env["REQUEST_METHOD"]
if method not in self.ALLOWED_METHODS:
raise HTTPNotImplemented()
path = env["PATH_INFO"]
_, acc, cont, obj = split_path(env["PATH_INFO"], 0, 4, rest_with_last=True)
if env.get("QUERY_STRING"):
path += "?" + env["QUERY_STRING"]
if "swift.authorize" in env:
resp = env["swift.authorize"](swob.Request(env))
if resp:
return resp(env, start_response)
req = swob.Request(env)
self.swift_sources.append(env.get("swift.source"))
self.txn_ids.append(env.get("swift.trans_id"))
try:
resp_class, raw_headers, body = self._find_response(method, path)
headers = HeaderKeyDict(raw_headers)
except KeyError:
if env.get("QUERY_STRING") and (method, env["PATH_INFO"]) in self._responses:
resp_class, raw_headers, body = self._find_response(method, env["PATH_INFO"])
headers = HeaderKeyDict(raw_headers)
elif method == "HEAD" and ("GET", path) in self._responses:
resp_class, raw_headers, body = self._find_response("GET", path)
body = None
headers = HeaderKeyDict(raw_headers)
elif method == "GET" and obj and path in self.uploaded:
resp_class = swob.HTTPOk
headers, body = self.uploaded[path]
else:
raise KeyError("Didn't find %r in allowed responses" % ((method, path),))
# simulate object PUT
if method == "PUT" and obj:
put_body = "".join(iter(env["wsgi.input"].read, ""))
if "swift.callback.update_footers" in env:
footers = HeaderKeyDict()
env["swift.callback.update_footers"](footers)
req.headers.update(footers)
etag = md5(put_body).hexdigest()
headers.setdefault("Etag", etag)
headers.setdefault("Content-Length", len(put_body))
# keep it for subsequent GET requests later
self.uploaded[path] = (dict(req.headers), put_body)
if "CONTENT_TYPE" in env:
self.uploaded[path][0]["Content-Type"] = env["CONTENT_TYPE"]
# simulate object POST
elif method == "POST" and obj:
metadata, data = self.uploaded.get(path, ({}, None))
# select items to keep from existing...
new_metadata = dict(
(k, v)
for k, v in metadata.items()
if (not is_user_meta("object", k) and not is_object_transient_sysmeta(k))
)
# apply from new
new_metadata.update(
dict(
(k, v)
for k, v in req.headers.items()
if (is_user_meta("object", k) or is_object_transient_sysmeta(k) or k.lower == "content-type")
)
)
self.uploaded[path] = new_metadata, data
# note: tests may assume this copy of req_headers is case insensitive
# so we deliberately use a HeaderKeyDict
self._calls.append((method, path, HeaderKeyDict(req.headers)))
# range requests ought to work, hence conditional_response=True
if isinstance(body, list):
resp = resp_class(
req=req, headers=headers, app_iter=body, conditional_response=req.method in ("GET", "HEAD")
)
else:
resp = resp_class(req=req, headers=headers, body=body, conditional_response=req.method in ("GET", "HEAD"))
wsgi_iter = resp(env, start_response)
self.mark_opened(path)
return LeakTrackingIter(wsgi_iter, self, path)