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:
parent
37578811d7
commit
5635895d0c
@ -33,12 +33,12 @@ class BlendPath(bytes):
|
|||||||
"""A path within Blender is always stored as bytes."""
|
"""A path within Blender is always stored as bytes."""
|
||||||
|
|
||||||
def __new__(cls, path):
|
def __new__(cls, path):
|
||||||
if isinstance(path, pathlib.Path):
|
if isinstance(path, pathlib.PurePath):
|
||||||
path = str(path).encode('utf-8')
|
path = str(path).encode('utf-8')
|
||||||
if not isinstance(path, bytes):
|
if not isinstance(path, bytes):
|
||||||
raise TypeError('path must be bytes or pathlib.Path, but is %r' % path)
|
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
|
@classmethod
|
||||||
def mkrelative(cls, asset_path: pathlib.Path, bfile_path: pathlib.Path) -> 'BlendPath':
|
def mkrelative(cls, asset_path: pathlib.Path, bfile_path: pathlib.Path) -> 'BlendPath':
|
||||||
@ -57,7 +57,7 @@ class BlendPath(bytes):
|
|||||||
bdir_parts.popleft()
|
bdir_parts.popleft()
|
||||||
asset_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?
|
# TODO(Sybren): should we use sys.getfilesystemencoding() instead?
|
||||||
rel_bytes = str(rel_asset).encode('utf-8')
|
rel_bytes = str(rel_asset).encode('utf-8')
|
||||||
as_bytes = b'//' + len(bdir_parts) * b'../' + rel_bytes
|
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")
|
raise ValueError("'a / b' only works when 'b' is a relative path")
|
||||||
return BlendPath(os.path.join(parentpath, self))
|
return BlendPath(os.path.join(parentpath, self))
|
||||||
|
|
||||||
def to_path(self) -> pathlib.Path:
|
def to_path(self) -> pathlib.PurePath:
|
||||||
"""Convert this path to a pathlib.Path.
|
"""Convert this path to a pathlib.PurePath.
|
||||||
|
|
||||||
Interprets the path as UTF-8, and if that fails falls back to the local
|
Interprets the path as UTF-8, and if that fails falls back to the local
|
||||||
filesystem encoding.
|
filesystem encoding.
|
||||||
@ -98,7 +98,7 @@ class BlendPath(bytes):
|
|||||||
decoded = self.decode('utf8')
|
decoded = self.decode('utf8')
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
decoded = self.decode(sys.getfilesystemencoding())
|
decoded = self.decode(sys.getfilesystemencoding())
|
||||||
return pathlib.Path(decoded)
|
return pathlib.PurePath(decoded)
|
||||||
|
|
||||||
def is_blendfile_relative(self) -> bool:
|
def is_blendfile_relative(self) -> bool:
|
||||||
return self[:2] == b'//'
|
return self[:2] == b'//'
|
||||||
|
|||||||
@ -148,7 +148,7 @@ class BlockUsage:
|
|||||||
"""
|
"""
|
||||||
if self._abspath is None:
|
if self._abspath is None:
|
||||||
bpath = self.block.bfile.abspath(self.asset_path)
|
bpath = self.block.bfile.abspath(self.asset_path)
|
||||||
as_path = bpath.to_path()
|
as_path = pathlib.Path(bpath.to_path())
|
||||||
try:
|
try:
|
||||||
self._abspath = as_path.resolve()
|
self._abspath = as_path.resolve()
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path, PurePosixPath
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from blender_asset_tracer.bpathlib import BlendPath
|
from blender_asset_tracer.bpathlib import BlendPath
|
||||||
@ -6,11 +6,12 @@ from blender_asset_tracer.bpathlib import BlendPath
|
|||||||
|
|
||||||
class BlendPathTest(unittest.TestCase):
|
class BlendPathTest(unittest.TestCase):
|
||||||
def test_string_path(self):
|
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)
|
self.assertEqual(b'//some/file.blend', p)
|
||||||
|
|
||||||
p = BlendPath(Path(r'C:\some\file.blend'))
|
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):
|
def test_is_absolute(self):
|
||||||
self.assertFalse(BlendPath(b'//some/file.blend').is_absolute())
|
self.assertFalse(BlendPath(b'//some/file.blend').is_absolute())
|
||||||
|
|||||||
@ -162,7 +162,7 @@ class PackTest(AbstractPackTest):
|
|||||||
def test_execute_rewrite(self):
|
def test_execute_rewrite(self):
|
||||||
infile, _ = self._pack_with_rewrite()
|
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)
|
extbpath = bpathlib.BlendPath(extpath)
|
||||||
|
|
||||||
# Those libraries should be properly rewritten.
|
# Those libraries should be properly rewritten.
|
||||||
@ -209,8 +209,9 @@ class PackTest(AbstractPackTest):
|
|||||||
seq = ed.get_pointer((b'seqbase', b'first'))
|
seq = ed.get_pointer((b'seqbase', b'first'))
|
||||||
seq_strip = seq.get_pointer(b'strip')
|
seq_strip = seq.get_pointer(b'strip')
|
||||||
|
|
||||||
as_bytes = str((self.blendfiles / 'imgseq').absolute()).encode()
|
imgseq_path = (self.blendfiles / 'imgseq').absolute()
|
||||||
relpath = b'//_outside_project%b' % as_bytes
|
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.
|
# The image sequence base path should be rewritten.
|
||||||
self.assertEqual(b'SQ000210.png', seq[b'name'])
|
self.assertEqual(b'SQ000210.png', seq[b'name'])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user