ftp exists and mkdir with parents methods
authorMaciej Tronowski <mtro@man.poznan.pl>
Tue, 28 Apr 2015 13:35:12 +0000 (15:35 +0200)
committerMaciej Tronowski <mtro@man.poznan.pl>
Tue, 28 Apr 2015 13:35:12 +0000 (15:35 +0200)
filex/ftp.py

index af06c27..3ccd8c6 100644 (file)
@@ -4,6 +4,7 @@ from itertools import chain
 import os
 import re
 from threading import Event
+from urlparse import urlparse, urlunparse
 
 from django.utils.http import urlunquote
 from django.utils.timezone import localtime, UTC
@@ -142,6 +143,18 @@ class FTPOperation:
 
         return data
 
+    def exists(self, url):
+        self.cli.exists(url, self._done, None, self.op_attr)
+
+        try:
+            self.wait()
+        except FTPError as e:
+            if 'No such file or directory' in e.message:
+                return False
+            raise
+        else:
+            return True
+
     def delete(self, url):
         self.cli.delete(url, self._done, None, self.op_attr)
 
@@ -152,7 +165,16 @@ class FTPOperation:
 
         self.wait()
 
-    def mkdir(self, url):
+    def mkdir(self, url, parents=False):
+        if parents:
+            if self.exists(url):
+                return
+
+            u = urlparse(url)
+            parent_url = urlunparse((u.scheme, u.netloc, os.path.dirname(os.path.normpath(u.path)), '', '', ''))
+
+            self.mkdir(parent_url, parents=True)
+
         self.cli.mkdir(url, self._done, None, self.op_attr)
 
         self.wait()