From 0c1065ee24a40f6e96d1c89561b79a28fd29a681 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 23 Oct 2014 22:24:43 +0200 Subject: [PATCH] downloading files works now --- gui/browser.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/gui/browser.py b/gui/browser.py index 3177a66..98aea08 100755 --- a/gui/browser.py +++ b/gui/browser.py @@ -55,21 +55,35 @@ class PathHandle: self.path.append(path_item) self.refresh() + @staticmethod + def request_url(req_path): + # TODO, get from config + BAM_SERVER = "http://localhost:5000" + result = "%s/%s" % (BAM_SERVER, req_path) + print(result) + return result + @staticmethod def json_from_server(path): - def request_url(req_path): - # TODO, get from config - BAM_SERVER = "http://localhost:5000" - result = "%s/%s" % (BAM_SERVER, req_path) - print(result) - return result import requests payload = {"path": "/".join(path)} - r = requests.get(request_url("files"), params=payload, auth=("bam", "bam")) + r = requests.get(PathHandle.request_url("files"), params=payload, auth=("bam", "bam")) print(r.text) return r.json() + @staticmethod + def download_from_server(path, idname): + import requests + payload = {"filepath": "/".join(path) + "/" + idname} + r = requests.get(PathHandle.request_url("file"), params=payload, auth=("bam", "bam"), stream=True) + local_filename = payload['filepath'].split('/')[-1] + with open(local_filename, 'wb') as f: + for chunk in r.iter_content(chunk_size=1024): + if chunk: # filter out keep-alive new chunks + f.write(chunk) + f.flush() + print(local_filename) class Application(tk.Frame): @@ -126,8 +140,10 @@ class Application(tk.Frame): self.repopulate() - def exec_path_file(idname): + self.path_handle.download_from_server(self.path_handle.path, idname) + print(self.path_handle.path) + self.repopulate() @@ -142,8 +158,10 @@ class Application(tk.Frame): self.grid_members.append(but_path) row = 1 - import random - random.shuffle(items) + + # import random + # random.shuffle(items) + for name_short, name_full, item_type in [("..", "", "folder")] + items: print(name_short, name_full, item_type) if item_type == "folder":