本文整理匯總了Python中catsnap.image_truck.ImageTruck.new_from_url方法的典型用法代碼示例。如果您正苦於以下問題:Python ImageTruck.new_from_url方法的具體用法?Python ImageTruck.new_from_url怎麽用?Python ImageTruck.new_from_url使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類catsnap.image_truck.ImageTruck
的用法示例。
在下文中一共展示了ImageTruck.new_from_url方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_new_from_url_reraises_non_sni_ssl_errors
# 需要導入模塊: from catsnap.image_truck import ImageTruck [as 別名]
# 或者: from catsnap.image_truck.ImageTruck import new_from_url [as 別名]
def test_new_from_url_reraises_non_sni_ssl_errors(self, mock_requests):
error = requests.exceptions.SSLError(
urllib3.exceptions.SSLError(
CertificateError("hostname 'catsinthecity.com' doesn't "
"match 'nossl.edgecastcdn.net'")))
mock_requests.get.side_effect = error
ImageTruck.new_from_url('https://catsinthecity.com/image.jpg')
示例2: test_new_from_url_raises_usefully_for_sni_trouble
# 需要導入模塊: from catsnap.image_truck import ImageTruck [as 別名]
# 或者: from catsnap.image_truck.ImageTruck import new_from_url [as 別名]
def test_new_from_url_raises_usefully_for_sni_trouble(self, mock_requests):
error = requests.exceptions.SSLError(
urllib3.exceptions.SSLError(
ssl.SSLError(1, '_ssl.c:503: error:14077410:SSL routines:'
'SSL23_GET_SERVER_HELLO:sslv3 alert handshake '
'failure')))
mock_requests.get.side_effect = error
ImageTruck.new_from_url('https://some.server.using.sni/image.jpg')
示例3: add
# 需要導入模塊: from catsnap.image_truck import ImageTruck [as 別名]
# 或者: from catsnap.image_truck.ImageTruck import new_from_url [as 別名]
def add(request_format):
tag_names = request.form['tags'].split(' ')
url = request.form['url']
if url:
print 'fetching from remote url'
truck = ImageTruck.new_from_url(url)
elif request.files['file']:
image = request.files['file']
truck = ImageTruck.new_from_stream(image.stream, image.mimetype)
else:
abort(400)
metadata = ImageMetadata.image_metadata(truck.contents)
print 'potentially reorienting'
truck.contents = ReorientImage.reorient_image(truck.contents)
print 'uploading to s3'
truck.upload()
session = Client().session()
image = Image(filename=truck.calculate_filename(),
source_url=url,
description=request.form.get('description'),
title=request.form.get('title'),
**metadata)
album_id = request.form['album']
if album_id:
image.album_id = album_id
session.add(image)
image.add_tags(tag_names)
ResizeImage.make_resizes(image, truck)
if request_format == 'html':
return redirect(url_for('show_image', image_id=image.image_id))
elif request_format == 'json':
return {'url': truck.url()}
示例4: add
# 需要導入模塊: from catsnap.image_truck import ImageTruck [as 別名]
# 或者: from catsnap.image_truck.ImageTruck import new_from_url [as 別名]
def add(request_format):
url = request.form.get("url")
if url:
try:
trucks = [ImageTruck.new_from_url(url)]
except RequestException:
abort(request_format, 400, "That url is no good.")
except TryHTTPError:
abort(
request_format,
400,
"Catsnap couldn't establish an HTTPS connection to that "
"image. An HTTP connection may succeed (this is a problem "
"on Catsnap's end, not something you did wrong).",
)
elif request.files.get("file[]"):
trucks = [ImageTruck.new_from_stream(data.stream) for data in request.files.getlist("file[]")]
elif request.files.get("file"):
data = request.files["file"]
trucks = [ImageTruck.new_from_stream(data.stream)]
else:
abort(request_format, 400, "Please submit either a file or a url.")
# These loops are sorta awkwardly phrased to avoid lots of round-tripping
# to the database. I hope you don't consider the optimization premature.
session = Client().session()
images = []
for truck in trucks:
image = Image(filename=truck.filename, source_url=url)
album_id = request.form.get("album_id")
if album_id:
image.album_id = int(album_id)
session.add(image)
images.append(image)
session.flush()
contentses = []
for i in xrange(0, len(images)):
(truck, image) = trucks[i], images[i]
contents = ImageContents(image_id=image.image_id, contents=truck.contents, content_type=truck.content_type)
session.add(contents)
contentses.append(contents)
session.flush()
task_ids = []
# Hey, this is a loop around a round-trip to redis. Although the Python
# Redis library offers a way to send multiple requests in one thwack,
# Celery doesn't appear to offer a way to use it. Nothing to be done.
for contents in contentses:
task_ids.append(delay(g.queued_tasks, process_image, contents.image_contents_id))
if request_format == "html":
return redirect(url_for("show_image", image_id=image.image_id))
elif request_format == "json":
return [
{"url": trucks[i].url(), "image_id": images[i].image_id, "task_id": task_ids[i]}
for i in xrange(0, len(trucks))
]
示例5: add
# 需要導入模塊: from catsnap.image_truck import ImageTruck [as 別名]
# 或者: from catsnap.image_truck.ImageTruck import new_from_url [as 別名]
def add(request_format):
url = request.form.get('url')
if url:
try:
trucks = [ImageTruck.new_from_url(url)]
except RequestException:
abort(request_format, 400, "That url is no good.")
elif request.files.get('file[]'):
trucks = [ImageTruck.new_from_stream(data.stream)
for data in request.files.getlist('file[]')]
elif request.files.get('file'):
data = request.files['file']
trucks = [ImageTruck.new_from_stream(data.stream)]
else:
abort(request_format, 400, "Please submit either a file or a url.")
# These loops are sorta awkwardly phrased to avoid lots of round-tripping
# to the database. I hope you don't consider the optimization premature.
session = Client().session()
images = []
for truck in trucks:
image = Image(filename=truck.filename, source_url=url)
album_id = request.form.get('album_id')
if album_id:
image.album_id = int(album_id)
session.add(image)
images.append(image)
session.flush()
contentses = []
for i in xrange(0, len(images)):
(truck, image) = trucks[i], images[i]
contents = ImageContents(image_id=image.image_id,
contents=truck.contents,
content_type=truck.content_type)
session.add(contents)
contentses.append(contents)
session.flush()
task_ids = []
# Hey, this is a loop around a round-trip to redis. Although the Python
# Redis library offers a way to send multiple requests in one thwack,
# Celery doesn't appear to offer a way to use it. Nothing to be done.
for contents in contentses:
task_ids.append(delay(g.queued_tasks,
process_image,
contents.image_contents_id))
if request_format == 'html':
return redirect(url_for('show_image', image_id=image.image_id))
elif request_format == 'json':
return [{
'url': trucks[i].url(),
'image_id': images[i].image_id,
'task_id': task_ids[i],
} for i in xrange(0, len(trucks))]
示例6: test_new_from_url
# 需要導入模塊: from catsnap.image_truck import ImageTruck [as 別名]
# 或者: from catsnap.image_truck.ImageTruck import new_from_url [as 別名]
def test_new_from_url(self, requests):
response = Mock()
response.content = "Ain't no party like a Liz Lemon party"
response.headers = {'content-type': 'party'}
requests.get.return_value = response
truck = ImageTruck.new_from_url('http://some.url')
eq_(truck.contents, "Ain't no party like a Liz Lemon party")
eq_(truck.content_type, "party")
eq_(truck.source_url, "http://some.url")
示例7: test_new_from_url__raises_on_non_200
# 需要導入模塊: from catsnap.image_truck import ImageTruck [as 別名]
# 或者: from catsnap.image_truck.ImageTruck import new_from_url [as 別名]
def test_new_from_url__raises_on_non_200(self, requests):
response = Mock()
response.raise_for_status.side_effect = HTTPError
requests.get.return_value = response
ImageTruck.new_from_url('http://some.url')