mickael9 avatar mickael9 committed 82b65a6

Better presentation of shares and some API tweaking.

Comments (0)

Files changed (2)

         self.url = result.get('getturl')
         self.files = {}
 
+        # FIXME: work around API omission
+        if not self.url:
+            self.url = 'http://ge.tt/%s' % self.name
+
         for file_result in result['files']:
             f = File(self)
             f._load(file_result)
         self.user = user
 
     def update(self, **fields):
-        result = _post_request('shares/' + self.name + '/update', _accesstoken=self.user.atoken, **fields)
+        result = _post_request('shares/%s/update' % self.name, _accesstoken=self.user.atoken, **fields)
         self._load(result)
 
     def destroy(self):
-        _post_request('shares/' + self.name + '/destroy', _accesstoken=self.user.atoken)
+        _post_request('shares/%s/destroy' % self.name, _accesstoken=self.user.atoken)
 
-    def create_file(self, filename):
-        result = _post_request('files/' + self.name + '/create', _accesstoken=self.user.atoken, filename=filename)
+    def create_file(self, filename, size=None):
+        result = _post_request('files/%s/create' % self.name, _accesstoken=self.user.atoken, filename=filename)
         file = File(self)
         file._load(result)
+        file.size = size
+        self.files[file.id] = file
         return file
 
 class File(object):
             self.put_url = None
 
     def destroy(self):
-        _post_request('files/' + self.share.name + '/' + str(self.id) + '/destroy', _accesstoken=self.share.user.atoken)
+        _post_request('files/%s/%s/destroy' % (self.share.name, self.id), _accesstoken=self.share.user.atoken)
+        del self.share.files[self.id]
 
     def refresh(self):
-        result = _get_request('files/' + self.share.name + '/' + str(self.id))
+        result = _get_request('files/%s/%s' % (self.share.name,  self.id))
         self._load(result)
 
 class FileUpload(threading.Thread):
 
         self.file = file
         self.fp = fp
-        self.file_size = os.path.getsize(fp.name)
+        self.file_size = file.size
         self.percent_done = 0
         self.bytes_written = 0
         self.ex = None
                 self.bytes_written += len(data)
                 self.percent_done = self.bytes_written * 100 / self.file_size
 
+            self.percent_done = 100 # needed when file_size is zero...
+
             resp = conn.getresponse()
         except Exception as ex:
             self.ex = ex
 
 url_re = re.compile(r'^(?:http://ge\.tt/|/)?(\w+)(?:/(?:v/(\d+)/?)?)?$')
 
-def humanize(nbytes):
-    for (exp, unit) in ((9, 'GB'), (6, 'MB'), (3, 'KB')):
+def humansize(nbytes, pad=False):
+    if nbytes is None:
+        return ''
+
+    for (exp, unit) in ((9, 'GB'), (6, 'MB'), (3, 'KB'), (0, 'B')):
        if nbytes >= 10**exp:
-           return '%.2f %s' % (nbytes / 10**exp, unit)
+           break
 
-    return '%.2f B' % nbytes
+    if pad:
+        return '%6.2f %-2s' % (nbytes / 10**exp, unit)
+    else:
+        return '%.2f %s' % (nbytes / 10**exp, unit)
+
+def shorten(filename, maxsize):
+    base, ext = os.path.splitext(filename)
+    max_base = maxsize - len(ext)
+
+    if len(base) > max_base:
+        base = base[:max_base - 2] + '..'
+
+    return (base + ext).ljust(max_base + len(ext))
 
 def print_status(upload, index, count):
-        name = upload.file.name
-
-        if len(name) > 15:
-            name = name[:12] + '...'
-
-        name = name.ljust(15)
-
+        name = shorten(upload.file.name, 15)
         bar_size = int(50 * upload.percent_done / 100)
         bar = '[' + (bar_size * '#') + ((50 - bar_size) * '-') + ']'
 
         sys.stderr.write('\r%s (%d/%d) %s %d %%' % (name, index, count, bar, upload.percent_done))
 
 def show_share(share):
-    print('Share %s: %s (%d file(s))' % (share.name, share.title or 'Untitled', len(share.files)))
+    print('--------------------------------------------------------------------------------')
+    print('Share: %s (%d file(s)) [%s]' % (share.title or 'Untitled', len(share.files), share.url))
+    print('--------------------------------------------------------------------------------')
+
+    max_url = max([len(_.url) for _ in share.files.values()])
 
     for file in share.files.values():
-        size = ''
-        if file.size is not None:
-            size = '[%s]' % humanize(file.size)
-
-        print('--- %-20s %s [%s]' % (file.name, size, file.url))
+        print('- %s  %s  %s  %s' % (shorten(file.name, 29), humansize(file.size, True), file.url.ljust(max_url), file.readystate))
     print()
 
 def entry_point():
                     file = share.files[id]
                     file.destroy()
 
-                    print('Deleted file %s [%s]' % (file.name, file.url))
+                    print('Deleted file: %s [%s]' % (file.name, file.url))
                 except KeyError:
                     print("No such file in the share")
             else:
-                print('Deleted share %s' % share.name)
+                print('Deleted share: %s [%s]' % (share.title or 'Untitled', share.url))
                 share.destroy()
 
     # File uploads
 
         for fp in chain.from_iterable(args.file):
             name = os.path.basename(fp.name)
-            file = share.create_file(name)
-
-            print('%s: %s' % (name, file.url))
+            file = share.create_file(name, os.path.getsize(fp.name))
 
             upload = FileUpload(file, fp)
 
 
             uploads.append(upload)
 
-        print()
+        show_share(share)
 
         if args.parallel_upload:
             uploads.sort(key=lambda item: item.file_size)
 
                     sys.stderr.write('\n')
                     break
+        print()
 
     user.refresh()
 
     print('Storage used: %s out of %s (%d %%)' % (
-            humanize(user.storage_used),
-            humanize(user.storage_limit),
+            humansize(user.storage_used),
+            humansize(user.storage_limit),
             int(100 * user.storage_used / user.storage_limit)))
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.