move field lookups into their own function
This commit is contained in:
@@ -319,8 +319,8 @@ class DNACatalog:
|
|||||||
DNACatalog is a catalog of all information in the DNA1 file-block
|
DNACatalog is a catalog of all information in the DNA1 file-block
|
||||||
"""
|
"""
|
||||||
__slots__ = (
|
__slots__ = (
|
||||||
"header",
|
|
||||||
"names",
|
"names",
|
||||||
|
#
|
||||||
"types",
|
"types",
|
||||||
# DNAStruct[]
|
# DNAStruct[]
|
||||||
"structs",
|
"structs",
|
||||||
@@ -458,30 +458,44 @@ class DNAName:
|
|||||||
|
|
||||||
class DNAStruct:
|
class DNAStruct:
|
||||||
"""
|
"""
|
||||||
DNAType is a C-type structure stored in the DNA
|
DNAStruct is a C-type structure stored in the DNA
|
||||||
"""
|
"""
|
||||||
__slots__ = (
|
__slots__ = (
|
||||||
"dna_type",
|
"dna_type",
|
||||||
|
# tuple:
|
||||||
|
# (type, name, size)
|
||||||
"fields",
|
"fields",
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.fields = []
|
self.fields = []
|
||||||
|
|
||||||
|
def field_from_name(self, name):
|
||||||
|
# TODO, use dict lookup?
|
||||||
|
offset = 0
|
||||||
|
for field in self.fields:
|
||||||
|
fname = field[1]
|
||||||
|
if fname.name_short == name:
|
||||||
|
return field, offset
|
||||||
|
offset += field[2]
|
||||||
|
return None, None
|
||||||
|
|
||||||
def field_get(self, header, handle, path,
|
def field_get(self, header, handle, path,
|
||||||
use_nil=True, use_str=True):
|
use_nil=True, use_str=True):
|
||||||
assert(type(path) == bytes)
|
assert(type(path) == bytes)
|
||||||
splitted = path.partition(b'.')
|
splitted = path.partition(b'.')
|
||||||
name = splitted[0]
|
name = splitted[0]
|
||||||
rest = splitted[2]
|
rest = splitted[2]
|
||||||
offset = 0
|
|
||||||
for field in self.fields:
|
field, offset = self.field_from_name(name)
|
||||||
|
if field is None:
|
||||||
|
raise KeyError("%r not found in %r" % (path, [s[1].name_short for s in self.fields]))
|
||||||
|
|
||||||
fname = field[1]
|
fname = field[1]
|
||||||
if fname.name_short == name:
|
|
||||||
handle.seek(offset, os.SEEK_CUR)
|
handle.seek(offset, os.SEEK_CUR)
|
||||||
ftype = field[0]
|
ftype = field[0]
|
||||||
if len(rest) == 0:
|
|
||||||
|
|
||||||
|
if rest == b'':
|
||||||
if fname.is_pointer:
|
if fname.is_pointer:
|
||||||
return DNA_IO.read_pointer(handle, header)
|
return DNA_IO.read_pointer(handle, header)
|
||||||
elif ftype[0] == b'int':
|
elif ftype[0] == b'int':
|
||||||
@@ -501,25 +515,21 @@ class DNAStruct:
|
|||||||
return DNA_IO.read_bytes0(handle, fname.array_size)
|
return DNA_IO.read_bytes0(handle, fname.array_size)
|
||||||
else:
|
else:
|
||||||
return DNA_IO.read_bytes(handle, fname.array_size)
|
return DNA_IO.read_bytes(handle, fname.array_size)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return ftype[2].field_get(header, handle, rest,
|
return ftype[2].field_get(header, handle, rest,
|
||||||
use_nil=use_nil, use_str=use_str)
|
use_nil=use_nil, use_str=use_str)
|
||||||
|
|
||||||
else:
|
|
||||||
offset += field[2]
|
|
||||||
|
|
||||||
raise KeyError("%r not found in %r" % (path, [s[1].name_short for s in self.fields]))
|
|
||||||
|
|
||||||
def field_set(self, header, handle, path, value):
|
def field_set(self, header, handle, path, value):
|
||||||
assert(type(path) == bytes)
|
assert(type(path) == bytes)
|
||||||
splitted = path.partition(b'.')
|
splitted = path.partition(b'.')
|
||||||
name = splitted[0]
|
name = splitted[0]
|
||||||
rest = splitted[2]
|
rest = splitted[2]
|
||||||
offset = 0
|
|
||||||
for field in self.fields:
|
field, offset = self.field_from_name(name)
|
||||||
|
if field is None:
|
||||||
|
raise KeyError("%r not found in %r" % (path, [s[1].name_short for s in self.fields]))
|
||||||
|
|
||||||
fname = field[1]
|
fname = field[1]
|
||||||
if fname.name_short == name:
|
|
||||||
handle.seek(offset, os.SEEK_CUR)
|
handle.seek(offset, os.SEEK_CUR)
|
||||||
ftype = field[0]
|
ftype = field[0]
|
||||||
if len(rest) == 0:
|
if len(rest) == 0:
|
||||||
@@ -530,10 +540,6 @@ class DNAStruct:
|
|||||||
return DNA_IO.write_bytes(handle, value, fname.array_size)
|
return DNA_IO.write_bytes(handle, value, fname.array_size)
|
||||||
else:
|
else:
|
||||||
return ftype[2].field_set(header, handle, rest, value)
|
return ftype[2].field_set(header, handle, rest, value)
|
||||||
else:
|
|
||||||
offset += field[2]
|
|
||||||
|
|
||||||
raise KeyError("%r not found in %r" % (path, [s[1].name_short for s in self.fields]))
|
|
||||||
|
|
||||||
|
|
||||||
class DNA_IO:
|
class DNA_IO:
|
||||||
@@ -663,7 +669,7 @@ class DNA_IO:
|
|||||||
|
|
||||||
class DNAField:
|
class DNAField:
|
||||||
"""
|
"""
|
||||||
DNAField is a coupled DNAType and DNAName
|
DNAField is a coupled DNAStruct and DNAName
|
||||||
"""
|
"""
|
||||||
__slots__ = (
|
__slots__ = (
|
||||||
"name",
|
"name",
|
||||||
|
Reference in New Issue
Block a user