Skip to content

Commit e750072

Browse files
committed
try to fix root._write
1 parent 42afc4e commit e750072

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

serialize_py/kaitai_serialize_codegen.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def __getitem__(self, i): # i is 0-based
3838
), f"page index is out of range: {i} is not in (0, {header.num_pages - 1})"
3939
# todo: maybe cache page
4040
# equality test: page_a.page_number == page_b.page_number
41-
# FIXME handle root page: i == 0
4241
_pos = root._io.pos()
4342
if i == 0:
4443
# The first 100 bytes of the database file comprise the database file header
@@ -176,15 +175,15 @@ def get_local_key(key, global_names):
176175
root = kaitaistruct_sqlite3.Sqlite3.from_bytes(good_database_header_bytes)
177176

178177
# patch the internal cache attribute of root.pages
179-
# root._m_pages = PagesList(root)
178+
root._m_pages = PagesList(root)
180179

181180
root._read()
182181

183182
# print("root.header.magic", root.header.magic)
184183
# now, this will parse **only** the first page
185184
# fix: 'BtreePage' object has no attribute 'cell_pointers'
186185

187-
if 1:
186+
if 0:
188187
# print("root.pages[0] keys:", get_keys(root.pages[0]))
189188
# print("root.pages[0] seq:", get_seq(root.pages[0]))
190189
# FIXME kaitaistruct.ValidationNotEqualError: /types/database_header/seq/0: at pos 116: validation failed: not equal,
@@ -198,7 +197,7 @@ def get_local_key(key, global_names):
198197
print_value("root.pages[0].num_cells")
199198
print_value("root.pages[0].cell_pointers[0]")
200199
print_value("root.pages[0].cell_pointers[0].ofs_content")
201-
if 1:
200+
if 0:
202201
# print("root.pages[1] keys:", get_keys(root.pages[1]))
203202
# print("root.pages[1] seq:", get_seq(root.pages[1]))
204203
print("root.pages[1]._read()"); root.pages[1]._read()
@@ -309,6 +308,10 @@ def codegen(
309308
print(f"{ind}{ids}if not _io:", file=out)
310309
print(f"{ind}{ids}{ids}_io = kaitaistruct.KaitaiStream(io.BytesIO(bytearray(root_size)))", file=out)
311310
print(f"{ind}{ids}{on} = {mod}.{member}(_io)", file=out)
311+
# TODO remove. this works only for sqlite3.ksy
312+
print(f"{ind}{ids}# try to fix root._write", file=out)
313+
print(f"{ind}{ids}# https://github.com/kaitai-io/kaitai_struct/issues/1245", file=out)
314+
print(f"{ind}{ids}{on}.pages__to_write = False", file=out)
312315
# else:
313316
# print(f"{ind}{ids}# non-root init", file=out)
314317
# print(f"{ind}{ids}{on} = {mod}.{member}(_io, {on_parent}, {on_parent}._root)", file=out)
@@ -423,11 +426,13 @@ def codegen(
423426
print(f"{ind}{ids}root = get_root()", file=out)
424427
print(f"{ind}{ids}_io = root._io", file=out)
425428
# print(f"{ind}{ids}_io.seek(0)", file=out)
426-
print(f"{ind}{ids}# no. _write calls _fetch_instances which throws", file=out)
427-
print(f"{ind}{ids}# root._write(_io)", file=out)
428-
print(f"{ind}{ids}root._write__seq(_io)", file=out)
429-
print(f"{ind}{ids}# root._fetch_instances() # this would throw", file=out)
430-
print(f"{ind}{ids}root._io.write_back_child_streams()", file=out)
429+
print(f"{ind}{ids}if 1:", file=out)
430+
print(f"{ind}{ids}{ids}# no. _write calls _fetch_instances which throws", file=out)
431+
print(f"{ind}{ids}{ids}root._write(_io)", file=out)
432+
print(f"{ind}{ids}else:", file=out)
433+
print(f"{ind}{ids}{ids}root._write__seq(_io)", file=out)
434+
print(f"{ind}{ids}{ids}# root._fetch_instances() # this would throw", file=out)
435+
print(f"{ind}{ids}{ids}root._io.write_back_child_streams()", file=out)
431436
print(f"{ind}{ids}return _io", file=out)
432437
print("", file=out)
433438
print(f"{ind}def get_bytes():", file=out)

serialize_py/kaitai_serialize_manual.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def write(on):
7575
# FIXME add extra space for database header
7676
_io = kaitaistruct.KaitaiStream(io.BytesIO(bytearray(num_pages * page_size + 100)))
7777

78-
if 0:
78+
if 1:
7979
root = kaitaistruct_sqlite3.Sqlite3(_io)
8080
else:
8181
class PatchedSqlite3(kaitaistruct_sqlite3.Sqlite3):
@@ -167,6 +167,11 @@ def _write(self, io=None):
167167
root.pages = []
168168
# page = set_value("root.header.root_page", root.BtreePage(page_number))
169169
page = root.BtreePage(page_number)
170+
171+
# try to fix root._write
172+
# https://github.com/kaitai-io/kaitai_struct/issues/1245
173+
page.cell_content_area__to_write = False
174+
170175
root.pages.append(page)
171176
page.database_header = root.header
172177
page._root = root
@@ -362,11 +367,18 @@ def _write(self, io=None):
362367

363368

364369

370+
# try to fix root._write
371+
# https://github.com/kaitai-io/kaitai_struct/issues/1245
372+
root.pages__to_write = False
373+
365374
_io.seek(0) # fix: _write__seq does not seek before writing
366375

367-
# no. _write calls _fetch_instances which throws
368-
# print(f"writing root"); root._write(_io)
369-
print(f"writing root"); root._write__seq(_io); root._io.write_back_child_streams()
376+
if 1:
377+
# no. _write calls _fetch_instances which throws
378+
# https://github.com/kaitai-io/kaitai_struct/issues/1245
379+
print(f"writing root"); root._write(_io)
380+
else:
381+
print(f"writing root"); root._write__seq(_io); root._io.write_back_child_streams()
370382

371383
print("writing done")
372384

0 commit comments

Comments
 (0)