Revision 56f0908a kamaki/clients/pithos.py

b/kamaki/clients/pithos.py
299 299
        return self.head(path, *args, success=success, **kwargs)
300 300

  
301 301
    def object_get(self, object, format='json', hashmap=False, version=None,
302
        data_range=None, if_range=False, if_etag_match=None, if_etag_not_match = None, if_modified_since = None, if_unmodified_since = None, *args, **kwargs):
302
        data_range=None, if_range=False, if_etag_match=None, if_etag_not_match = None,
303
        if_modified_since = None, if_unmodified_since = None, *args, **kwargs):
303 304
        """ Full Pithos+ GET at object level
304 305
        --- request parameters ---
305 306
        @param format (string): json (default) or xml
......
714 715
        self.object_put(object, format='json', hashmap=True, content_type=obj_content_type, 
715 716
            json=hashmap, success=201)
716 717

  
718
    def download_object(self, obj, f, download_cb=None):
719

  
720
        self.assert_container()
721

  
722
        #retrieve object hashmap
723
        hashmap = self.get_object_hashmap(obj)
724
        blocksize = int(hashmap['block_size'])
725
        blockhash = hashmap['block_hash']
726
        total_size = hashmap['bytes']
727
        map = hashmap['hashes']
728
        map_dict = {}
729
        for h in map:
730
            map_dict[h] = True
731
        download_bars = len(map)
732

  
733
        #load progress bar
734
        if download_cb is not None:
735
            download_gen = download_cb(total_size/blocksize + 1)
736
            download_gen.next()
737

  
738
        #load local file existing hashmap
739
        if not f.isatty():
740
            hash_dict = {}
741
            index = 0
742
            from os import path
743
            if path.exists(f.name):
744
                from binascii import hexlify
745
                from .pithos_sh_lib.hashmap import HashMap
746
                h = HashMap(blocksize, blockhash)
747
                h.load(f)
748
                for x in h:
749
                    existing_hash = hexlify(x)
750
                    if existing_hash not in map_dict:
751
                        raise ClientError(message='Local file is substantialy different',
752
                            status=600)
753
                    hash_dict[existing_hash] = index
754
                    index += 1
755
                    if download_cb:
756
                        download_gen.next()
757

  
758
        #download and print
759
        for i, h in enumerate(map):
760
            if not f.isatty() and h in hash_dict:
761
                continue
762
            if download_cb is not None:
763
                download_gen.next()
764
            start = i*blocksize
765
            end = start + blocksize -1 if start+blocksize < total_size else total_size -1
766
            data_range = 'bytes=%s-%s'%(start, end)
767
            data = self.object_get(obj, data_range=data_range, success=(200, 206))
768
            if not f.isatty():
769
                f.seek(start)
770
            f.write(data.text)
771

  
772
    def get_object_hashmap(self, obj, version=None):
773
        r = self.object_get(obj, hashmap=True, version=version)
774
        from json import loads
775
        return loads(r.text)
776

  
717 777
    def set_account_group(self, group, usernames):
718 778
        self.account_post(update=True, groups = {group:usernames})
719 779

  

Also available in: Unified diff