Browse Source

refactor: refactor function

bluelovers 2 years ago
parent
commit
f564d8ed2c
1 changed files with 6 additions and 15 deletions
  1. 6 15
      modules/ui_extra_networks_user_metadata.py

+ 6 - 15
modules/ui_extra_networks_user_metadata.py

@@ -10,21 +10,12 @@ from modules import generation_parameters_copypaste, images, sysinfo, errors
 from modules.paths_internal import models_path
 
 
-def windows_to_unix_style(path):
-    return Path(path).as_posix()
-
-
-def exclude_root_path(root_path, path_to_exclude):
-    try:
-        relative_path = os.path.relpath(path_to_exclude, root_path)
-        # 如果路径已经在 root_path 之外,relpath 会返回绝对路径
-        # 所以需要检查路径是否在 root_path 之内
-        if not relative_path.startswith('..'):
-            return windows_to_unix_style(relative_path)
-    except ValueError:
-        pass
-    # 如果路径无法相对化,或者位于 root_path 之外,则返回原始路径
-    return windows_to_unix_style(path_to_exclude)
+def exclude_root_path(root_path, path):
+    path_object = Path(path)
+    if path_object.is_relative_to(root_path):
+        path_object = path_object.relative_to(root_path)
+
+    return path_object.as_posix()
 
 
 class UserMetadataEditor: