Commits

mickael9 committed d3b4c5d

A bit more polishing, it's starting to look nice !

Comments (0)

Files changed (2)

              yield share
 
     def get_share(self, name):
-        result = _get_request('shares/' + self.name)
+        result = _get_request('shares/' + name)
 
         share = UserShare(self)
         share._load(result)
         result = _get_request('files/' + self.share.name + '/' + str(self.id))
         self._load(result)
 
-class FileUploader(threading.Thread):
+class FileUpload(threading.Thread):
     def __init__(self, file, fp):
         super().__init__()
 
         self.fp = fp
         self.file_size = os.path.getsize(fp.name)
         self.percent_done = 0
+        self.bytes_written = 0
+        self.ex = None
 
     def run(self):
-        parsed = urlparse(self.file.put_url)
-        conn = HTTPConnection(parsed.netloc)
-        conn.connect()
-        conn.putrequest('PUT', parsed.path + (('?' + parsed.query) if parsed.query else ''))
-        conn.putheader('Content-Length', str(self.file_size))
-        conn.endheaders()
+        try:
+            parsed = urlparse(self.file.put_url)
+            conn = HTTPConnection(parsed.netloc)
+            conn.connect()
+            conn.putrequest('PUT', parsed.path + (('?' + parsed.query) if parsed.query else ''))
+            conn.putheader('Content-Length', str(self.file_size))
+            conn.endheaders()
 
-        bytes_written = 0
+            while self.bytes_written != self.file_size:
+                data = self.fp.read(4096)
+                conn.sock.sendall(data)
+                self.bytes_written += len(data)
+                self.percent_done = self.bytes_written * 100 / self.file_size
 
-        while bytes_written != self.file_size:
-            data = self.fp.read(4096)
-            conn.sock.sendall(data)
-            bytes_written += len(data)
-
-            self.percent_done = bytes_written * 100 / self.file_size
-
-        resp = conn.getresponse()
-
-        print(resp)
-        print(resp.read())
+            resp = conn.getresponse()
+        except Exception as ex:
+            self.ex = ex
         args.email = input('Please enter your Ge.tt email : ')
 
     if not args.password:
-        args.password = input('Please enter your Ge.tt password : ')
+        import getpass
+        args.password = getpass.getpass('Please enter your Ge.tt password : ')
 
     try:
         user.login_auth(args.email, args.password)
     else:
         share = user.create_share(os.path.basename(args.file[0].name))
 
-uploaders = []
+
+uploads = []
 
 for fp in args.file:
     name = os.path.basename(fp.name)
 
     print('%s : %s' % (name, file.url))
 
-    uploader = FileUploader(file, fp)
-    uploader.start()
-    uploaders.append(uploader)
+    upload = FileUpload(file, fp)
+    upload.start()
+    uploads.append(upload)
 
-finished = False
+print()
 
-while not finished:
-    finished = True
+uploads.sort(key=lambda item: item.file_size)
 
-    for upl in uploaders:
-        if not upl.is_alive():
-            continue
+def print_status(upload):
+    name = upload.file.name
 
-        finished = False
+    if len(name) > 15:
+        name = name[:12] + '...'
 
-        print('%s : %d %%' % (upl.file.name, upl.percent_done))
+    name = name.ljust(15)
 
-    sleep(1)
+    bar_size = int(50 * upload.percent_done / 100)
+    bar = '[' + (bar_size * '#') + ((50 - bar_size) * '-') + ']'
 
-for upl in uploaders:
-    upl.join()
+    sys.stderr.write('\r%s %s %d %%' % (name, bar, upload.percent_done))
 
-print('Done !')
+for upload in uploads:
+    while True:
+        print_status(upload)
+        upload.join(0.5)
+
+        if not upload.is_alive():
+            if upload.ex:
+                sys.stderr.write(('\rError uploading %s : %s' % (upload.file.name, upload.ex)).ljust(80))
+
+            sys.stderr.write('\n')
+            break
+
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.