Kaynağa Gözat

New file and HTTP functionality (head, build directories from scratch)

NIX User 4 yıl önce
ebeveyn
işleme
61d804118e
2 değiştirilmiş dosya ile 42 ekleme ve 6 silme
  1. 28 4
      labkeyFileBrowser.py
  2. 14 2
      labkeyInterface.py

+ 28 - 4
labkeyFileBrowser.py

@@ -9,10 +9,17 @@ class labkeyFileBrowser:
 
     def GetRootUrl(self):
         return self.net.GetLabkeyUrl()+"/_webdav"
-    
+   
+    def entryExists(self,url):
+        #check if entry (file/directory) exists
+        r=self.net.head(url)
+        if r.status==200:
+            return True
+        return False
+
     def listRemoteDir(self,url):
         #input is remoteDir, result are remoteDirs
-
+        dirs=[]
         #r=MethodRequest(dirUrl,method="PROPFIND")
         PROPFIND=u"""<?xml version="1.0" encoding="utf-8"?>\n
                     <propfind xmlns="DAV:">\n
@@ -34,9 +41,15 @@ class labkeyFileBrowser:
             print(e)
             return False,dirs
         
-        tree=ET.XML(f.data)
+        try:
+            tree=ET.XML(f.data)
+        except ET.ParseError:
+            #print(f.data.decode('utf-8'))
+            #if directory is not there, a 404 response will be made by HTTP, which is not an xml file
+            #we are safe to assume the directory is not there
+            return False,dirs
+
         rps=tree.findall('{DAV:}response')
-        dirs=[]
         for r in rps:
             hr=r.find('{DAV:}href')
             dirent=hr.text
@@ -46,6 +59,17 @@ class labkeyFileBrowser:
         del dirs[0]
         return True,dirs
 
+    def buildPathURL(self,project,pathList):
+        #build/check remote directory structure
+        remoteDir=self.formatPathURL(project,'')
+        for p in pathList:
+            remoteDir+='/'+p
+            if not self.entryExists(remoteDir):
+                 self.mkdir(remoteDir)
+
+        return remoteDir
+
+
     def readFileToBuffer(self, url):
         return self.net.get(url,binary=True)
 

+ 14 - 2
labkeyInterface.py

@@ -52,8 +52,7 @@ class labkeyInterface:
 
         debug=False
         if debug:
-            print("GET: {0}").format(url)
-            print("as {0}").format(user)
+            print("GET: {0}".format(url))
         headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
         try:
             if not binary:
@@ -65,6 +64,19 @@ class labkeyInterface:
         except urllib3.exceptions.HTTPError as e:
             print(e)
 
+    def head(self,url):
+
+        debug=False
+        if debug:
+            print("HEAD: {0}".format(url))
+        headers=urllib3.util.make_headers(basic_auth=self.getBasicAuth())
+        
+        try:
+            return self.http.request('HEAD',url,headers=headers)
+        except urllib3.exceptions.HTTPError as e:
+            print(e)
+
+
     def post(self,url,data):
 
         debug=False