Attempt at making BAT work on Windows

All BlendPath instances will use forward slashes, and there should be
more use of PurePosixPath instead of Path.
This commit is contained in:
Sybren A. Stüvel 2018-12-20 15:15:49 +01:00
parent 37578811d7
commit 5635895d0c
4 changed files with 15 additions and 13 deletions

View File

@ -33,12 +33,12 @@ class BlendPath(bytes):
"""A path within Blender is always stored as bytes."""
def __new__(cls, path):
if isinstance(path, pathlib.Path):
if isinstance(path, pathlib.PurePath):
path = str(path).encode('utf-8')
if not isinstance(path, bytes):
raise TypeError('path must be bytes or pathlib.Path, but is %r' % path)
return super().__new__(cls, path)
return super().__new__(cls, path.replace(b'\\', b'/'))
@classmethod
def mkrelative(cls, asset_path: pathlib.Path, bfile_path: pathlib.Path) -> 'BlendPath':
@ -57,7 +57,7 @@ class BlendPath(bytes):
bdir_parts.popleft()
asset_parts.popleft()
rel_asset = pathlib.Path(*asset_parts)
rel_asset = pathlib.PurePath(*asset_parts)
# TODO(Sybren): should we use sys.getfilesystemencoding() instead?
rel_bytes = str(rel_asset).encode('utf-8')
as_bytes = b'//' + len(bdir_parts) * b'../' + rel_bytes
@ -84,8 +84,8 @@ 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.
def to_path(self) -> pathlib.PurePath:
"""Convert this path to a pathlib.PurePath.
Interprets the path as UTF-8, and if that fails falls back to the local
filesystem encoding.
@ -98,7 +98,7 @@ class BlendPath(bytes):
decoded = self.decode('utf8')
except UnicodeDecodeError:
decoded = self.decode(sys.getfilesystemencoding())
return pathlib.Path(decoded)
return pathlib.PurePath(decoded)
def is_blendfile_relative(self) -> bool:
return self[:2] == b'//'

View File

@ -148,7 +148,7 @@ class BlockUsage:
"""
if self._abspath is None:
bpath = self.block.bfile.abspath(self.asset_path)
as_path = bpath.to_path()
as_path = pathlib.Path(bpath.to_path())
try:
self._abspath = as_path.resolve()
except FileNotFoundError:

View File

@ -1,4 +1,4 @@
from pathlib import Path
from pathlib import Path, PurePosixPath
import unittest
from blender_asset_tracer.bpathlib import BlendPath
@ -6,11 +6,12 @@ from blender_asset_tracer.bpathlib import BlendPath
class BlendPathTest(unittest.TestCase):
def test_string_path(self):
p = BlendPath(Path('//some/file.blend'))
p = BlendPath(PurePosixPath('//some/file.blend'))
self.assertEqual('//some/file.blend', str(PurePosixPath('//some/file.blend')))
self.assertEqual(b'//some/file.blend', p)
p = BlendPath(Path(r'C:\some\file.blend'))
self.assertEqual(b'C:\\some\\file.blend', p)
self.assertEqual(b'C:/some/file.blend', p)
def test_is_absolute(self):
self.assertFalse(BlendPath(b'//some/file.blend').is_absolute())

View File

@ -162,7 +162,7 @@ class PackTest(AbstractPackTest):
def test_execute_rewrite(self):
infile, _ = self._pack_with_rewrite()
extpath = pathlib.Path('//_outside_project', *self.blendfiles.parts[1:])
extpath = pathlib.PurePosixPath('//_outside_project', *self.blendfiles.parts[1:])
extbpath = bpathlib.BlendPath(extpath)
# Those libraries should be properly rewritten.
@ -209,8 +209,9 @@ class PackTest(AbstractPackTest):
seq = ed.get_pointer((b'seqbase', b'first'))
seq_strip = seq.get_pointer(b'strip')
as_bytes = str((self.blendfiles / 'imgseq').absolute()).encode()
relpath = b'//_outside_project%b' % as_bytes
imgseq_path = (self.blendfiles / 'imgseq').absolute()
as_bytes = str(imgseq_path.relative_to(imgseq_path.anchor)).encode()
relpath = bpathlib.BlendPath(b'//_outside_project') / as_bytes
# The image sequence base path should be rewritten.
self.assertEqual(b'SQ000210.png', seq[b'name'])