Convert BlendPath to pathlib.Path using local filesystem encoding
UTF-8 is tried first, and if that fails the local filesystem encoding is used.
This commit is contained in:
parent
8bb130d336
commit
677d388a15
@ -7,6 +7,7 @@ or vice versa.
|
||||
import os.path
|
||||
import pathlib
|
||||
import string
|
||||
import sys
|
||||
|
||||
|
||||
class BlendPath(bytes):
|
||||
@ -41,6 +42,22 @@ class BlendPath(bytes):
|
||||
raise ValueError("'a / b' only works when 'b' is a relative path")
|
||||
return BlendPath(os.path.join(parentpath, self))
|
||||
|
||||
def to_path(self) -> pathlib.Path:
|
||||
"""Convert this path to a pathlib.Path.
|
||||
|
||||
Interprets the path as UTF-8, and if that fails falls back to the local
|
||||
filesystem encoding.
|
||||
|
||||
Note that this does not handle blend-file-relative paths specially, so
|
||||
the returned Path may still start with '//'.
|
||||
"""
|
||||
# TODO(Sybren): once we target Python 3.6, implement __fspath__().
|
||||
try:
|
||||
decoded = self.decode('utf8')
|
||||
except UnicodeDecodeError:
|
||||
decoded = self.decode(sys.getfilesystemencoding())
|
||||
return pathlib.Path(decoded)
|
||||
|
||||
def is_blendfile_relative(self) -> bool:
|
||||
return self[:2] == b'//'
|
||||
|
||||
|
||||
@ -59,8 +59,7 @@ class _Tracer:
|
||||
abspath = lib_block.bfile.abspath(relpath)
|
||||
|
||||
# Convert bytes to pathlib.Path object so we have a nice interface to work with.
|
||||
# This assumes the path is encoded in UTF-8.
|
||||
path = pathlib.Path(abspath.decode())
|
||||
path = abspath.to_path()
|
||||
try:
|
||||
path = path.resolve()
|
||||
except FileNotFoundError:
|
||||
|
||||
@ -98,7 +98,7 @@ class BlockUsage:
|
||||
"""
|
||||
|
||||
bpath = self.block.bfile.abspath(self.asset_path)
|
||||
path = pathlib.Path(bpath.decode())
|
||||
path = bpath.to_path()
|
||||
if not self.is_sequence:
|
||||
if not path.exists():
|
||||
log.warning('Path %s does not exist for %s', path, self)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user