path.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. # ##### BEGIN GPL LICENSE BLOCK #####
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software Foundation,
  15. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. # ##### END GPL LICENSE BLOCK #####
  18. # <pep8-80 compliant>
  19. """
  20. This module has a similar scope to os.path, containing utility
  21. functions for dealing with paths in Blender.
  22. """
  23. __all__ = (
  24. "abspath",
  25. "basename",
  26. "clean_name",
  27. "display_name",
  28. "display_name_to_filepath",
  29. "display_name_from_filepath",
  30. "ensure_ext",
  31. "extensions_image",
  32. "extensions_movie",
  33. "extensions_audio",
  34. "is_subdir",
  35. "module_names",
  36. "native_pathsep",
  37. "reduce_dirs",
  38. "relpath",
  39. "resolve_ncase",
  40. )
  41. import bpy as _bpy
  42. import os as _os
  43. from _bpy_path import (
  44. extensions_audio,
  45. extensions_movie,
  46. extensions_image,
  47. )
  48. def _getattr_bytes(var, attr):
  49. return var.path_resolve(attr, False).as_bytes()
  50. def abspath(path, start=None, library=None):
  51. """
  52. Returns the absolute path relative to the current blend file
  53. using the "//" prefix.
  54. :arg start: Relative to this path,
  55. when not set the current filename is used.
  56. :type start: string or bytes
  57. :arg library: The library this path is from. This is only included for
  58. convenience, when the library is not None its path replaces *start*.
  59. :type library: :class:`bpy.types.Library`
  60. """
  61. if isinstance(path, bytes):
  62. if path.startswith(b"//"):
  63. if library:
  64. start = _os.path.dirname(
  65. abspath(_getattr_bytes(library, "filepath")))
  66. return _os.path.join(
  67. _os.path.dirname(_getattr_bytes(_bpy.data, "filepath"))
  68. if start is None else start,
  69. path[2:],
  70. )
  71. else:
  72. if path.startswith("//"):
  73. if library:
  74. start = _os.path.dirname(
  75. abspath(library.filepath))
  76. return _os.path.join(
  77. _os.path.dirname(_bpy.data.filepath)
  78. if start is None else start,
  79. path[2:],
  80. )
  81. return path
  82. def relpath(path, start=None):
  83. """
  84. Returns the path relative to the current blend file using the "//" prefix.
  85. :arg path: An absolute path.
  86. :type path: string or bytes
  87. :arg start: Relative to this path,
  88. when not set the current filename is used.
  89. :type start: string or bytes
  90. """
  91. if isinstance(path, bytes):
  92. if not path.startswith(b"//"):
  93. if start is None:
  94. start = _os.path.dirname(_getattr_bytes(_bpy.data, "filepath"))
  95. return b"//" + _os.path.relpath(path, start)
  96. else:
  97. if not path.startswith("//"):
  98. if start is None:
  99. start = _os.path.dirname(_bpy.data.filepath)
  100. return "//" + _os.path.relpath(path, start)
  101. return path
  102. def is_subdir(path, directory):
  103. """
  104. Returns true if *path* in a subdirectory of *directory*.
  105. Both paths must be absolute.
  106. :arg path: An absolute path.
  107. :type path: string or bytes
  108. """
  109. from os.path import normpath, normcase, sep
  110. path = normpath(normcase(path))
  111. directory = normpath(normcase(directory))
  112. if len(path) > len(directory):
  113. sep = sep.encode('ascii') if isinstance(directory, bytes) else sep
  114. if path.startswith(directory.rstrip(sep) + sep):
  115. return True
  116. return False
  117. def clean_name(name, replace="_"):
  118. """
  119. Returns a name with characters replaced that
  120. may cause problems under various circumstances,
  121. such as writing to a file.
  122. All characters besides A-Z/a-z, 0-9 are replaced with "_"
  123. or the *replace* argument if defined.
  124. """
  125. if replace != "_":
  126. if len(replace) != 1 or ord(replace) > 255:
  127. raise ValueError("Value must be a single ascii character")
  128. def maketrans_init():
  129. trans_cache = clean_name._trans_cache
  130. trans = trans_cache.get(replace)
  131. if trans is None:
  132. bad_chars = (
  133. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  134. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  135. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  136. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  137. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  138. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2e, 0x2f, 0x3a,
  139. 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x5b, 0x5c,
  140. 0x5d, 0x5e, 0x60, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  141. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  142. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  143. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  144. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  145. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  146. 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  147. 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
  148. 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  149. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  150. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  151. 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  152. 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  153. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  154. 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  155. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  156. 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe,
  157. )
  158. trans = str.maketrans({char: replace for char in bad_chars})
  159. trans_cache[replace] = trans
  160. return trans
  161. trans = maketrans_init()
  162. return name.translate(trans)
  163. clean_name._trans_cache = {}
  164. def _clean_utf8(name):
  165. if type(name) == bytes:
  166. return name.decode("utf8", "replace")
  167. else:
  168. return name.encode("utf8", "replace").decode("utf8")
  169. _display_name_literals = {
  170. ":": "_colon_",
  171. "+": "_plus_",
  172. }
  173. def display_name(name, *, has_ext=True):
  174. """
  175. Creates a display string from name to be used menus and the user interface.
  176. Capitalize the first letter in all lowercase names,
  177. mixed case names are kept as is. Intended for use with
  178. filenames and module names.
  179. """
  180. if has_ext:
  181. name = _os.path.splitext(basename(name))[0]
  182. # string replacements
  183. for disp_value, file_value in _display_name_literals.items():
  184. name = name.replace(file_value, disp_value)
  185. # strip to allow underscore prefix
  186. # (when paths can't start with numbers for eg).
  187. name = name.replace("_", " ").lstrip(" ")
  188. if name.islower():
  189. name = name.lower().title()
  190. name = _clean_utf8(name)
  191. return name
  192. def display_name_to_filepath(name):
  193. """
  194. Performs the reverse of display_name using literal versions of characters
  195. which aren't supported in a filepath.
  196. """
  197. for disp_value, file_value in _display_name_literals.items():
  198. name = name.replace(disp_value, file_value)
  199. return name
  200. def display_name_from_filepath(name):
  201. """
  202. Returns the path stripped of directory and extension,
  203. ensured to be utf8 compatible.
  204. """
  205. name = _os.path.splitext(basename(name))[0]
  206. name = _clean_utf8(name)
  207. return name
  208. def resolve_ncase(path):
  209. """
  210. Resolve a case insensitive path on a case sensitive system,
  211. returning a string with the path if found else return the original path.
  212. """
  213. def _ncase_path_found(path):
  214. if not path or _os.path.exists(path):
  215. return path, True
  216. # filename may be a directory or a file
  217. filename = _os.path.basename(path)
  218. dirpath = _os.path.dirname(path)
  219. suffix = path[:0] # "" but ensure byte/str match
  220. if not filename: # dir ends with a slash?
  221. if len(dirpath) < len(path):
  222. suffix = path[:len(path) - len(dirpath)]
  223. filename = _os.path.basename(dirpath)
  224. dirpath = _os.path.dirname(dirpath)
  225. if not _os.path.exists(dirpath):
  226. if dirpath == path:
  227. return path, False
  228. dirpath, found = _ncase_path_found(dirpath)
  229. if not found:
  230. return path, False
  231. # at this point, the directory exists but not the file
  232. # we are expecting 'dirpath' to be a directory, but it could be a file
  233. if _os.path.isdir(dirpath):
  234. try:
  235. files = _os.listdir(dirpath)
  236. except PermissionError:
  237. # We might not have the permission to list dirpath...
  238. return path, False
  239. else:
  240. return path, False
  241. filename_low = filename.lower()
  242. f_iter_nocase = None
  243. for f_iter in files:
  244. if f_iter.lower() == filename_low:
  245. f_iter_nocase = f_iter
  246. break
  247. if f_iter_nocase:
  248. return _os.path.join(dirpath, f_iter_nocase) + suffix, True
  249. else:
  250. # can't find the right one, just return the path as is.
  251. return path, False
  252. ncase_path, found = _ncase_path_found(path)
  253. return ncase_path if found else path
  254. def ensure_ext(filepath, ext, case_sensitive=False):
  255. """
  256. Return the path with the extension added if it is not already set.
  257. :arg ext: The extension to check for, can be a compound extension. Should
  258. start with a dot, such as '.blend' or '.tar.gz'.
  259. :type ext: string
  260. :arg case_sensitive: Check for matching case when comparing extensions.
  261. :type case_sensitive: bool
  262. """
  263. if case_sensitive:
  264. if filepath.endswith(ext):
  265. return filepath
  266. else:
  267. if filepath[-len(ext):].lower().endswith(ext.lower()):
  268. return filepath
  269. return filepath + ext
  270. def module_names(path, recursive=False):
  271. """
  272. Return a list of modules which can be imported from *path*.
  273. :arg path: a directory to scan.
  274. :type path: string
  275. :arg recursive: Also return submodule names for packages.
  276. :type recursive: bool
  277. :return: a list of string pairs (module_name, module_file).
  278. :rtype: list
  279. """
  280. from os.path import join, isfile
  281. modules = []
  282. for filename in sorted(_os.listdir(path)):
  283. if filename == "modules":
  284. pass # XXX, hard coded exception.
  285. elif filename.endswith(".py") and filename != "__init__.py":
  286. fullpath = join(path, filename)
  287. modules.append((filename[0:-3], fullpath))
  288. elif "." not in filename:
  289. directory = join(path, filename)
  290. fullpath = join(directory, "__init__.py")
  291. if isfile(fullpath):
  292. modules.append((filename, fullpath))
  293. if recursive:
  294. for mod_name, mod_path in module_names(directory, True):
  295. modules.append(("%s.%s" % (filename, mod_name),
  296. mod_path,
  297. ))
  298. return modules
  299. def basename(path):
  300. """
  301. Equivalent to os.path.basename, but skips a "//" prefix.
  302. Use for Windows compatibility.
  303. """
  304. return _os.path.basename(path[2:] if path[:2] in {"//", b"//"} else path)
  305. def native_pathsep(path):
  306. """
  307. Replace the path separator with the systems native ``os.sep``.
  308. """
  309. if type(path) is str:
  310. if _os.sep == "/":
  311. return path.replace("\\", "/")
  312. else:
  313. if path.startswith("//"):
  314. return "//" + path[2:].replace("/", "\\")
  315. else:
  316. return path.replace("/", "\\")
  317. else: # bytes
  318. if _os.sep == "/":
  319. return path.replace(b"\\", b"/")
  320. else:
  321. if path.startswith(b"//"):
  322. return b"//" + path[2:].replace(b"/", b"\\")
  323. else:
  324. return path.replace(b"/", b"\\")
  325. def reduce_dirs(dirs):
  326. """
  327. Given a sequence of directories, remove duplicates and
  328. any directories nested in one of the other paths.
  329. (Useful for recursive path searching).
  330. :arg dirs: Sequence of directory paths.
  331. :type dirs: sequence
  332. :return: A unique list of paths.
  333. :rtype: list
  334. """
  335. dirs = list({_os.path.normpath(_os.path.abspath(d)) for d in dirs})
  336. dirs.sort(key=lambda d: len(d))
  337. for i in range(len(dirs) - 1, -1, -1):
  338. for j in range(i):
  339. print(i, j)
  340. if len(dirs[i]) == len(dirs[j]):
  341. break
  342. elif is_subdir(dirs[i], dirs[j]):
  343. del dirs[i]
  344. break
  345. return dirs