diff options
author | w-e-w <40751091+w-e-w@users.noreply.github.com> | 2023-12-27 21:22:51 +0000 |
---|---|---|
committer | w-e-w <40751091+w-e-w@users.noreply.github.com> | 2023-12-27 21:22:51 +0000 |
commit | de04573438bc111f137359b8f4998780bf315275 (patch) | |
tree | 22711971699388d107adb7bafcda319fe445a7de /modules/util.py | |
parent | de03882d6ca56bc81058f5120f028678a6a54aaa (diff) | |
download | stable-diffusion-webui-gfx803-de04573438bc111f137359b8f4998780bf315275.tar.gz stable-diffusion-webui-gfx803-de04573438bc111f137359b8f4998780bf315275.tar.bz2 stable-diffusion-webui-gfx803-de04573438bc111f137359b8f4998780bf315275.zip |
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/util.py')
-rw-r--r-- | modules/util.py | 12 |
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
|