Revision 49cc29b2 kamaki/clients/pithos/__init__.py
b/kamaki/clients/pithos/__init__.py | ||
---|---|---|
36 | 36 |
from os import fstat |
37 | 37 |
from hashlib import new as newhashlib |
38 | 38 |
from time import time |
39 |
from StringIO import StringIO |
|
39 | 40 |
|
40 | 41 |
from binascii import hexlify |
41 | 42 |
|
... | ... | |
43 | 44 |
from kamaki.clients.pithos.rest_api import PithosRestClient |
44 | 45 |
from kamaki.clients.storage import ClientError |
45 | 46 |
from kamaki.clients.utils import path4url, filter_in |
46 |
from StringIO import StringIO |
|
47 | 47 |
|
48 | 48 |
|
49 | 49 |
def _pithos_hash(block, blockhash): |
... | ... | |
623 | 623 |
|
624 | 624 |
self._complete_cb() |
625 | 625 |
|
626 |
def download_to_string( |
|
627 |
self, obj, |
|
628 |
download_cb=None, |
|
629 |
version=None, |
|
630 |
range_str=None, |
|
631 |
if_match=None, |
|
632 |
if_none_match=None, |
|
633 |
if_modified_since=None, |
|
634 |
if_unmodified_since=None): |
|
635 |
"""Download an object to a string (multiple connections) |
|
636 |
|
|
637 |
:param obj: (str) remote object path |
|
638 |
|
|
639 |
:param download_cb: optional progress.bar object for downloading |
|
640 |
|
|
641 |
:param version: (str) file version |
|
642 |
|
|
643 |
:param range_str: (str) from, to are file positions (int) in bytes |
|
644 |
|
|
645 |
:param if_match: (str) |
|
646 |
|
|
647 |
:param if_none_match: (str) |
|
648 |
|
|
649 |
:param if_modified_since: (str) formated date |
|
650 |
|
|
651 |
:param if_unmodified_since: (str) formated date |
|
652 |
|
|
653 |
:returns: (str) the whole object contents |
|
654 |
""" |
|
655 |
restargs = dict( |
|
656 |
version=version, |
|
657 |
data_range=None if range_str is None else 'bytes=%s' % range_str, |
|
658 |
if_match=if_match, |
|
659 |
if_none_match=if_none_match, |
|
660 |
if_modified_since=if_modified_since, |
|
661 |
if_unmodified_since=if_unmodified_since) |
|
662 |
|
|
663 |
( |
|
664 |
blocksize, |
|
665 |
blockhash, |
|
666 |
total_size, |
|
667 |
hash_list, |
|
668 |
remote_hashes) = self._get_remote_blocks_info(obj, **restargs) |
|
669 |
assert total_size >= 0 |
|
670 |
|
|
671 |
if download_cb: |
|
672 |
self.progress_bar_gen = download_cb(len(hash_list)) |
|
673 |
self._cb_next() |
|
674 |
|
|
675 |
ret = '' |
|
676 |
for blockid, blockhash in enumerate(remote_hashes): |
|
677 |
start = blocksize * blockid |
|
678 |
is_last = start + blocksize > total_size |
|
679 |
end = (total_size - 1) if is_last else (start + blocksize - 1) |
|
680 |
(start, end) = _range_up(start, end, range_str) |
|
681 |
if start == end: |
|
682 |
continue |
|
683 |
restargs['data_range'] = 'bytes=%s-%s' % (start, end) |
|
684 |
r = self.object_get(obj, success=(200, 206), **restargs) |
|
685 |
ret += r.content |
|
686 |
self._cb_next() |
|
687 |
|
|
688 |
self._complete_cb() |
|
689 |
return ret |
|
690 |
|
|
626 | 691 |
#Command Progress Bar method |
627 | 692 |
def _cb_next(self, step=1): |
628 | 693 |
if hasattr(self, 'progress_bar_gen'): |
Also available in: Unified diff