图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.3 KiB

  1. def normpath(path, trailing=False):
  2. """Normalize path, eliminating double slashes, etc."""
  3. comps = path.split("/")
  4. new_comps = []
  5. for comp in comps:
  6. if comp == "":
  7. continue
  8. if comp in (".", ".."):
  9. raise ValueError("relative paths not allowed")
  10. new_comps.append(comp)
  11. new_path = "/".join(new_comps)
  12. if trailing is True and path.endswith("/"):
  13. new_path += "/"
  14. if path.startswith("/") and new_path != "/":
  15. return "/" + new_path
  16. return new_path
  17. def join(a, *p):
  18. """Join two or more pathname components, inserting '/' as needed.
  19. If any component is an absolute path, all previous path components
  20. will be discarded.
  21. """
  22. path = a
  23. for b in p:
  24. if b.startswith("/"):
  25. path = b
  26. elif path == "" or path.endswith("/"):
  27. path += b
  28. else:
  29. path += "/" + b
  30. return path
  31. def isabs(s):
  32. """Test whether a path is absolute"""
  33. return s.startswith("/")
  34. def basename(p):
  35. """Returns the final component of a pathname"""
  36. i = p.rfind("/") + 1
  37. return p[i:]
  38. def _prefix_root(root, path, trailing=False):
  39. """Prepend a root to a path."""
  40. return normpath(
  41. join(_norm_root(root), path.lstrip("/")), trailing=trailing
  42. )
  43. def _norm_root(root):
  44. return normpath(join("/", root))