Refs #1171
[pithos] / tools / store
index 6d7cbed..9e3ab27 100755 (executable)
@@ -40,6 +40,7 @@ from sys import argv, exit, stdin, stdout
 from datetime import datetime
 from lib.client import Pithos_Client, Fault
 from lib.util import get_user, get_auth, get_server, get_api
+from lib.transfer import upload, download
 
 import json
 import logging
@@ -203,7 +204,7 @@ class Meta(Command):
     
     def execute(self, path=''):
         container, sep, object = path.partition('/')
-        args = {'restricted':self.restricted}
+        args = {'restricted': self.restricted}
         if getattr(self, 'until'):
             t = _time.strptime(self.until, self.format)
             args['until'] = int(_time.mktime(t))
@@ -298,7 +299,7 @@ class GetObject(Command):
         args = self._build_args(attrs)
         args['format'] = 'json' if self.detail else 'text'
         if self.range:
-            args['range'] = 'bytes=%s' %self.range
+            args['range'] = 'bytes=%s' % self.range
         if getattr(self, 'if_range'):
             args['if-range'] = 'If-Range:%s' % getattr(self, 'if_range')
         
@@ -308,6 +309,7 @@ class GetObject(Command):
             if 'detail' in args.keys():
                 args.pop('detail')
             args.pop('format')
+            self.detail = True
             data = self.client.retrieve_object_versionlist(container, object, **args)
         elif self.version:
             data = self.client.retrieve_object_version(container, object,
@@ -317,7 +319,6 @@ class GetObject(Command):
         
         f = self.file and open(self.file, 'w') or stdout
         if self.detail:
-            data = json.loads(data)
             if self.versionlist:
                 print_versions(data, f=f)
             else:
@@ -399,9 +400,13 @@ class PutObject(Command):
             self.client.create_object_using_chunks(container, object, f,
                                                     meta=meta, **args)
         elif self.use_hashes:
-            format = 'json' if detail else 'text'
-            self.client.create_object_by_hashmap(container, object, f, format,
-                                 meta=meta, **args)
+            data = f.read()
+            if data is object:
+                hashmap = json.loads()
+                self.client.create_object_by_hashmap(container, object, hashmap,
+                                                 meta=meta, **args)
+            else:
+                print "Expected object"
         elif self.x_object_manifest:
             self.client.create_manifestation(container, object, self.x_object_manifest)
         elif not f:
@@ -423,6 +428,9 @@ class CopyObject(Command):
         parser.add_option('--public', action='store_true',
                           dest='public', default=False,
                           help='make object publicly accessible')
+        parser.add_option('--content-type', action='store',
+                          dest='content_type', default=None,
+                          help='change object\'s content type')
     
     def execute(self, src, dst, *args):
         src_container, sep, src_object = src.partition('/')
@@ -438,8 +446,10 @@ class CopyObject(Command):
             dst_container = src_container
             dst_object = dst
         
+        args = {'content_type':self.content_type} if self.content_type else {}
         self.client.copy_object(src_container, src_object, dst_container,
-                                dst_object, meta, self.public, self.version, **meta)
+                                dst_object, meta, self.public, self.version,
+                                **args)
 
 @cli_command('set')
 class SetMeta(Command):
@@ -501,6 +511,9 @@ class UpdateObject(Command):
         parser.add_option('--public', action='store_true',
                           dest='x_object_public', default=False,
                           help='make object publicly accessible')
+        parser.add_option('--replace', action='store_true',
+                          dest='replace', default=False,
+                          help='override metadata')
     
     def execute(self, path, *args):
         if path.find('=') != -1:
@@ -512,13 +525,14 @@ class UpdateObject(Command):
             key, sep, val = arg.partition('=')
             meta[key] = val
         
-        if self.no_sharing:
-            self.x_object_sharing = ''
         
         attrs = ['content_encoding', 'content_disposition', 'x_object_sharing',
-                 'x_object_public']
+                 'x_object_public', 'replace']
         args = self._build_args(attrs)
         
+        if self.no_sharing:
+            args['x_object_sharing'] = ''
+        
         container, sep, object = path.partition('/')
         
         f = None
@@ -539,12 +553,12 @@ class MoveObject(Command):
     description = 'move an object to a different location'
     
     def add_options(self, parser):
-        parser.add_option('--version', action='store',
-                          dest='version', default=None,
-                          help='move a specific object version')
         parser.add_option('--public', action='store_true',
                           dest='public', default=False,
                           help='make object publicly accessible')
+        parser.add_option('--content-type', action='store',
+                          dest='content_type', default=None,
+                          help='change object\'s content type')
     
     def execute(self, src, dst, *args):
         src_container, sep, src_object = src.partition('/')
@@ -559,28 +573,9 @@ class MoveObject(Command):
             key, sep, val = arg.partition('=')
             meta[key] = val
         
+        args = {'content_type':self.content_type} if self.content_type else {}
         self.client.move_object(src_container, src_object, dst_container,
-                                dst_object, meta, self.public, self.version)
-
-@cli_command('remove')
-class TrashObject(Command):
-    syntax = '<container>/<object>'
-    description = 'trash an object'
-    
-    def execute(self, src):
-        src_container, sep, src_object = src.partition('/')
-        
-        self.client.trash_object(src_container, src_object)
-
-@cli_command('restore')
-class RestoreObject(Command):
-    syntax = '<container>/<object>'
-    description = 'restore a trashed object'
-    
-    def execute(self, src):
-        src_container, sep, src_object = src.partition('/')
-        
-        self.client.restore_object(src_container, src_object)
+                                dst_object, meta, self.public, **args)
 
 @cli_command('unset')
 class UnsetObject(Command):
@@ -669,6 +664,46 @@ class UnpublishObject(Command):
         
         self.client.unpublish_object(src_container, src_object)
 
+@cli_command('sharing')
+class SharingObject(Command):
+    syntax = 'list users sharing objects with the user'
+    description = 'list user accounts sharing objects with the user'
+    
+    def add_options(self, parser):
+        parser.add_option('-l', action='store_true', dest='detail',
+                          default=False, help='show detailed output')
+        parser.add_option('-n', action='store', type='int', dest='limit',
+                          default=10000, help='show limited output')
+        parser.add_option('--marker', action='store', type='str',
+                          dest='marker', default=None,
+                          help='show output greater then marker')
+        
+    
+    def execute(self):
+        attrs = ['limit', 'marker']
+        args = self._build_args(attrs)
+        args['format'] = 'json' if self.detail else 'text'
+        
+        print_list(self.client.list_shared_by_others(**args))
+
+@cli_command('send')
+class Send(Command):
+    syntax = '<file> <container>[/<prefix>]'
+    description = 'upload file to container (using prefix)'
+    
+    def execute(self, file, path):
+        container, sep, prefix = path.partition('/')
+        upload(self.client, file, container, prefix)
+
+@cli_command('receive')
+class Receive(Command):
+    syntax = '<container>/<object> <file>'
+    description = 'download object to file'
+    
+    def execute(self, path, file):
+        container, sep, object = path.partition('/')
+        download(self.client, container, object, file)
+
 def print_usage():
     cmd = Command('', [])
     parser = cmd.parser