From a8e2b7eb6139270bba7ced67784ea3177503f02d Mon Sep 17 00:00:00 2001
From: Sinisa Veseli <sveseli@aps.anl.gov>
Date: Fri, 3 Mar 2017 23:40:10 +0000
Subject: [PATCH] generic url parsing functions

---
 src/python/dm/common/utility/ftpUtility.py | 32 ++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/src/python/dm/common/utility/ftpUtility.py b/src/python/dm/common/utility/ftpUtility.py
index 6987a6c0..3f29c030 100755
--- a/src/python/dm/common/utility/ftpUtility.py
+++ b/src/python/dm/common/utility/ftpUtility.py
@@ -35,6 +35,38 @@ class FtpUtility:
             dirPath = parseResult.path
         return (scheme, host, port, dirPath)
 
+    @classmethod
+    def parseUrl(cls, url, defaultHost=None, defaultPort=None):
+        host = defaultHost
+        port = defaultPort
+        parseResult = urlparse.urlparse(url)
+        scheme = parseResult.scheme
+        netlocTokens = parseResult.netloc
+        if netlocTokens: 
+	    netlocTokens = netlocTokens.split(':')
+            host = netlocTokens[0]
+            if len(netlocTokens) > 1:
+                port = int(netlocTokens[1])
+        dirPath = parseResult.path
+        dirPath = os.path.normpath(dirPath)
+        return (scheme, host, port, dirPath)
+
+    @classmethod
+    def assembleUrl(cls, scheme, host, port, dirPath):
+        url = ''
+	if scheme:
+	    url += '%s://' % scheme
+	if host:
+	    url += '%s' % (host)
+	    if port:
+	        url += ':%s' % (port)
+        if dirPath:
+	    if len(url) and not dirPath.startswith('/'):
+	        url += '/%s' % (os.path.normpath(dirPath))
+	    else:
+	        url += '%s' % (os.path.normpath(dirPath))
+        return url
+
     @classmethod
     def getFtpClient(cls, host, port, username=None, password=None):
         ftp = FTP()
-- 
GitLab