diff --git a/blender_asset_tracer/bpathlib.py b/blender_asset_tracer/bpathlib.py index c894a33..0e836e3 100644 --- a/blender_asset_tracer/bpathlib.py +++ b/blender_asset_tracer/bpathlib.py @@ -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'//' diff --git a/blender_asset_tracer/tracer/__init__.py b/blender_asset_tracer/tracer/__init__.py index 363a29c..734fa72 100644 --- a/blender_asset_tracer/tracer/__init__.py +++ b/blender_asset_tracer/tracer/__init__.py @@ -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: diff --git a/blender_asset_tracer/tracer/result.py b/blender_asset_tracer/tracer/result.py index f65b544..9c2c2d6 100644 --- a/blender_asset_tracer/tracer/result.py +++ b/blender_asset_tracer/tracer/result.py @@ -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)