ソースを参照

Adding file upload

Andrej Studen/Merlin 4 年 前
コミット
06f162254d
2 ファイル変更20 行追加4 行削除
  1. 14 2
      labkeyFileBrowser.py
  2. 6 2
      labkeyInterface.py

+ 14 - 2
labkeyFileBrowser.py

@@ -1,6 +1,6 @@
 import urllib3
 import xml.etree.ElementTree as ET
-
+import shutil
 
 class labkeyFileBrowser:
 
@@ -47,7 +47,19 @@ class labkeyFileBrowser:
         return True,dirs
 
     def readFileToBuffer(self, url):
-        return self.net.get(url)
+        return self.net.get(url,binary=True)
+
+    def readFileToFile(self,url,path):
+        response=self.readFileToBuffer(url)
+        with open(path,'wb') as out_file:
+            shutil.copyfileobj(response,out_file)
+        response.release_conn()
+
+    def writeFileToFile(self,path,url):
+        with open(path,'rb') as f:
+            data=f.read()
+        self.net.put(url,data)
+
     
     def formatPathURL(self,project,path):
         url=self.GetRootUrl()+'/'+project+'/@files'

+ 6 - 2
labkeyInterface.py

@@ -42,7 +42,7 @@ class labkeyInterface:
 
 
     
-    def get(self,url):
+    def get(self,url,binary=False):
 
         debug=False
         if debug:
@@ -50,7 +50,11 @@ class labkeyInterface:
             print("as {0}").format(user)
         headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
         try:
-            return self.http.request('GET',url,headers=headers)
+            if not binary:
+                return self.http.request('GET',url,headers=headers)
+            else:
+                return self.http.request('GET',url,headers=headers,preload_content=False)
+        #f contains json as a return value
         #f contains json as a return value
         except urllib3.exceptions.HTTPError as e:
             print(e)