New add_path function
Change-Id: I12ae8966d00e091a804ac4b4c222592292588c1e
Signed-off-by: Michael Walsh <micwalsh@us.ibm.com>
diff --git a/lib/gen_misc.py b/lib/gen_misc.py
index a2129c1..02aee84 100755
--- a/lib/gen_misc.py
+++ b/lib/gen_misc.py
@@ -77,6 +77,39 @@
return file_path
+def add_path(new_path,
+ path,
+ position=0):
+ r"""
+ Add new_path to path, provided that path doesn't already contain new_path,
+ and return the result.
+
+ Example:
+ If PATH has a value of "/bin/user:/lib/user". The following code:
+
+ PATH = add_path("/tmp/new_path", PATH)
+
+ will change PATH to "/tmp/new_path:/bin/user:/lib/user".
+
+ Description of argument(s):
+ new_path The path to be added. This function will
+ strip the trailing slash.
+ path The path value to which the new_path
+ should be added.
+ position The position in path where the new_path
+ should be added. 0 means it should be
+ added to the beginning, 1 means add it as
+ the 2nd item, etc. sys.maxsize means it
+ should be added to the end.
+ """
+
+ path_list = list(filter(None, path.split(":")))
+ new_path = new_path.rstrip("/")
+ if new_path not in path_list:
+ path_list.insert(int(position), new_path)
+ return ":".join(path_list)
+
+
def dft(value, default):
r"""
Return default if value is None. Otherwise, return value.