aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorw-e-w <40751091+w-e-w@users.noreply.github.com>2023-12-28 06:22:51 +0900
committerw-e-w <40751091+w-e-w@users.noreply.github.com>2023-12-28 06:22:51 +0900
commitde04573438bc111f137359b8f4998780bf315275 (patch)
tree22711971699388d107adb7bafcda319fe445a7de /modules
parentde03882d6ca56bc81058f5120f028678a6a54aaa (diff)
create utility truncate_path
utli.truncate_path(target_path, base_path) return the target_path relative to base_path if target_path is a sub path of base_path else return the absolute path
Diffstat (limited to 'modules')
-rw-r--r--modules/util.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/modules/util.py b/modules/util.py
index 60afc067..4861bcb0 100644
--- a/modules/util.py
+++ b/modules/util.py
@@ -2,7 +2,7 @@ import os
import re
from modules import shared
-from modules.paths_internal import script_path
+from modules.paths_internal import script_path, cwd
def natural_sort_key(s, regex=re.compile('([0-9]+)')):
@@ -56,3 +56,13 @@ def ldm_print(*args, **kwargs):
return
print(*args, **kwargs)
+
+
+def truncate_path(target_path, base_path=cwd):
+ abs_target, abs_base = os.path.abspath(target_path), os.path.abspath(base_path)
+ try:
+ if os.path.commonpath([abs_target, abs_base]) == abs_base:
+ return os.path.relpath(abs_target, abs_base)
+ except ValueError:
+ pass
+ return abs_target