Path rewriting when packing.

Doesn't work with sequences, nor with split dirname/basename fields.
This commit is contained in:
Sybren A. Stüvel
2018-03-06 16:06:36 +01:00
parent 433ad8f16a
commit 71dd5bc11b
9 changed files with 285 additions and 38 deletions
+5 -1
View File
@@ -63,6 +63,7 @@ class BlendFile:
self._is_modified = False
fileobj = path.open(mode, buffering=FILE_BUFFER_SIZE)
fileobj.seek(0, os.SEEK_SET)
magic = fileobj.read(len(BLENDFILE_MAGIC))
if magic == BLENDFILE_MAGIC:
@@ -170,6 +171,9 @@ class BlendFile:
if not self.fileobj:
return
if self._is_modified:
log.debug('closing blend file %s after it was modified', self.raw_filepath)
if self._is_modified and self.is_compressed:
log.debug("recompressing modified blend file %s", self.raw_filepath)
self.fileobj.seek(os.SEEK_SET, 0)
@@ -290,7 +294,7 @@ class BlendFile:
abspath = relpath.absolute(root)
my_log = self.log.getChild('abspath')
my_log.info('Resolved %s relative to %s to %s', relpath, self.filepath, abspath)
my_log.debug('Resolved %s relative to %s to %s', relpath, self.filepath, abspath)
return abspath
+13 -4
View File
@@ -1,12 +1,14 @@
import logging
import os
import typing
import os
from . import dna_io, header, exceptions
from . import header, exceptions
# Either a simple path b'propname', or a tuple (b'parentprop', b'actualprop', arrayindex)
FieldPath = typing.Union[bytes, typing.Iterable[typing.Union[bytes, int]]]
log = logging.getLogger(__name__)
class Name:
"""dna.Name is a C-type name stored in the DNA as bytes."""
@@ -83,6 +85,8 @@ class Field:
class Struct:
"""dna.Struct is a C-type structure stored in the DNA."""
log = log.getChild('Struct')
def __init__(self, dna_type_id: bytes, size: int = None):
"""
:param dna_type_id: name of the struct in C, like b'AlembicObjectPath'.
@@ -268,7 +272,7 @@ class Struct:
struct on disk (e.g. the start of the BlendFileBlock containing the
data).
"""
assert (type(path) == bytes)
assert isinstance(path, bytes), 'path should be bytes, but is %s' % type(path)
field, offset = self.field_from_path(file_header.pointer_size, path)
@@ -283,6 +287,11 @@ class Struct:
fileobj.seek(offset, os.SEEK_CUR)
if self.log.isEnabledFor(logging.DEBUG):
filepos = fileobj.tell()
thing = 'string' if isinstance(value, str) else 'bytes'
self.log.debug('writing %s %r at file offset %d / %x', thing, value, filepos, filepos)
if isinstance(value, str):
return endian.write_string(fileobj, value, dna_name.array_size)
else: