From db99b0282f83bc017b82b94759d2d27d5aa0c81e Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 10:10:54 +0100 Subject: [PATCH 01/18] wip: simplify CMakeLists.txt etc. --- CMakeLists.txt | 30 ++++++++---------------------- kaitai-struct-compiler | 0 pyproject.toml | 5 +++++ 3 files changed, 13 insertions(+), 22 deletions(-) mode change 100644 => 100755 kaitai-struct-compiler diff --git a/CMakeLists.txt b/CMakeLists.txt index e9953dd..6c5e312 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,26 +9,14 @@ string(REGEX REPLACE "\\.ksy$" "" package_name ${package_name}) set(PACKAGE_NAME ${package_name}) # set(CMAKE_ARGS "-DPACKAGE_NAME=${PACKAGE_NAME}") -set(BUILD_SHARED_LIBS OFF) -set(BUILD_STATIC_LIBS ON) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") +set(BUILD_SHARED_LIBS ON) +set(BUILD_STATIC_LIBS OFF) +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") find_package(pybind11 CONFIG REQUIRED) add_subdirectory(kaitai_struct_cpp_stl_runtime) -set (MAIN_HEADERS - kaitai_struct_cpp_stl_runtime/kaitai/kaitaistream.h - kaitai_struct_cpp_stl_runtime/kaitai/kaitaistruct.h - kaitai_struct_cpp_stl_runtime/kaitai/exceptions.h -) - -set (MAIN_SOURCES - kaitai_struct_cpp_stl_runtime/kaitai/kaitaistream.cpp -) - -add_library (${PROJECT_NAME} ${MAIN_HEADERS} ${MAIN_SOURCES}) - # Download awkward headers include(FetchContent) set(AWKWARD_VERSION "v2.4.3") @@ -43,25 +31,23 @@ if(NOT awkward-headers_POPULATED) add_subdirectory(${awkward-headers_SOURCE_DIR} ${awkward-headers_BINARY_DIR} EXCLUDE_FROM_ALL) endif() -target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_BINARY_DIR}/_deps/awkward-headers-src/layout-builder) -target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_BINARY_DIR}/_deps/awkward-headers-src/builder-options) -target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_BINARY_DIR}/_deps/awkward-headers-src/growable-buffer) +MESSAGE(WARNING "${CMAKE_SOURCE_DIR}/kaitai_struct_compiler") # Run sbt package command to compile kaitai_struct_compiler code execute_process( COMMAND sbt package WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/kaitai_struct_compiler + COMMAND_ERROR_IS_FATAL ANY ) execute_process( COMMAND ./kaitai-struct-compiler -t awkward --outdir src-${PACKAGE_NAME} ${KSY} #package name should be declared as a string WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMAND_ERROR_IS_FATAL ANY ) pybind11_add_module(awkward_${PACKAGE_NAME} src-${PACKAGE_NAME}/${PACKAGE_NAME}_main.cpp src-${PACKAGE_NAME}/${PACKAGE_NAME}.cpp) -target_link_libraries(awkward_${PACKAGE_NAME} PRIVATE awkward::layout-builder) -target_include_directories(awkward_${PACKAGE_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/kaitai_struct_cpp_stl_runtime) -target_link_libraries(awkward_${PACKAGE_NAME} PUBLIC kaitai_struct_cpp_stl_runtime) +target_link_libraries(awkward_${PACKAGE_NAME} PRIVATE awkward::layout-builder PUBLIC kaitai_struct_cpp_stl_runtime) # Install into wheel -install(TARGETS awkward_${PACKAGE_NAME} DESTINATION .) \ No newline at end of file +install(TARGETS awkward_${PACKAGE_NAME} DESTINATION .) diff --git a/kaitai-struct-compiler b/kaitai-struct-compiler old mode 100644 new mode 100755 diff --git a/pyproject.toml b/pyproject.toml index 0ef6b58..415c6ec 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -61,6 +61,11 @@ minimum-version = "0.4" build-dir = "build/{wheel_tag}" metadata.version.provider = "scikit_build_core.metadata.setuptools_scm" sdist.include = ["src/kaitai_awkward_runtime/_version.py"] +cmake.verbose = true +logging.level = "INFO" + +[tool.scikit-build.cmake.define] +KSY = "schemas/animal.ksy" [tool.setuptools_scm] From 90510dc07f8e2a76cac12bedc25d20d8ed45b4f8 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 10:15:52 +0100 Subject: [PATCH 02/18] fix: don't force .a files for now --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c5e312..a6e9a17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.15...3.26) project(kaitai_awkward_runtime CXX) set(KSY "" CACHE STRING "Specify the KSY file path") -set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") string(REGEX MATCH "([^/]+)$" package_name ${KSY}) string(REGEX REPLACE "\\.ksy$" "" package_name ${package_name}) From 745e87c42a96221486f2761151918e7a43839e5c Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 10:18:07 +0100 Subject: [PATCH 03/18] fix: add awkward as a dependency --- pyproject.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 415c6ec..97fc24d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,9 @@ classifiers = [ "Typing :: Typed", ] dynamic = ["version"] -dependencies = [] +dependencies = [ + "awkward>2.0.0" +] [project.optional-dependencies] test = [ From f491f4dd349349c0af9a58406d55491ce9535a17 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 10:18:27 +0100 Subject: [PATCH 04/18] docs: fix README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 64f2929..053695f 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,8 @@ python 4. Print the returned `ak.Array`: ```python -import animal -awkward_array = animal.load("../data/animal.raw") +import awkward_animal +awkward_array = awkward_animal.load("../data/animal.raw") print(awkward_array) ``` From 580a8a95ae84da6dd24b36fcf7382b6b354808ce Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 11:05:06 +0100 Subject: [PATCH 05/18] docs: update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 053695f..a695131 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ python 4. Print the returned `ak.Array`: ```python import awkward_animal -awkward_array = awkward_animal.load("../data/animal.raw") +awkward_array = awkward_animal.load("data/animal.raw") print(awkward_array) ``` From b70f371fc3dbd42335fbe860fe2567e95f35f598 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 11:05:22 +0100 Subject: [PATCH 06/18] chore: cleanup cmakelists --- CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a6e9a17..ffaff12 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,11 +6,9 @@ set(KSY "" CACHE STRING "Specify the KSY file path") string(REGEX MATCH "([^/]+)$" package_name ${KSY}) string(REGEX REPLACE "\\.ksy$" "" package_name ${package_name}) set(PACKAGE_NAME ${package_name}) -# set(CMAKE_ARGS "-DPACKAGE_NAME=${PACKAGE_NAME}") set(BUILD_SHARED_LIBS ON) set(BUILD_STATIC_LIBS OFF) -#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") find_package(pybind11 CONFIG REQUIRED) From 10734091fa5c784e08e26d352cde38da4509aec7 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 11:05:57 +0100 Subject: [PATCH 07/18] chore: switch to debug build --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 97fc24d..94ff545 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -65,6 +65,7 @@ metadata.version.provider = "scikit_build_core.metadata.setuptools_scm" sdist.include = ["src/kaitai_awkward_runtime/_version.py"] cmake.verbose = true logging.level = "INFO" +cmake.build-type = "Debug" [tool.scikit-build.cmake.define] KSY = "schemas/animal.ksy" From 457516455aafb4cf98d45efc0879af7daeb70028 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 11:08:54 +0100 Subject: [PATCH 08/18] chore: use Manasvi's branch --- kaitai_struct_compiler | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kaitai_struct_compiler b/kaitai_struct_compiler index 2f894bf..414e6c5 160000 --- a/kaitai_struct_compiler +++ b/kaitai_struct_compiler @@ -1 +1 @@ -Subproject commit 2f894bf7aba2a7214921deb3a8d75d3fa684e945 +Subproject commit 414e6c5c69dfa6a30be34e13b4ff943e3266db9a From b0aff339a75fd1efcf385c9f0daec91a1d5b4b28 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 11:24:33 +0100 Subject: [PATCH 09/18] chore: link kaitai privately --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ffaff12..29b9f3a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,7 +44,7 @@ execute_process( ) pybind11_add_module(awkward_${PACKAGE_NAME} src-${PACKAGE_NAME}/${PACKAGE_NAME}_main.cpp src-${PACKAGE_NAME}/${PACKAGE_NAME}.cpp) -target_link_libraries(awkward_${PACKAGE_NAME} PRIVATE awkward::layout-builder PUBLIC kaitai_struct_cpp_stl_runtime) +target_link_libraries(awkward_${PACKAGE_NAME} PRIVATE awkward::layout-builder kaitai_struct_cpp_stl_runtime) # Install into wheel install(TARGETS awkward_${PACKAGE_NAME} DESTINATION .) From 628318ec86e094a31187340bbd9bec843615c1b0 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 11:30:41 +0100 Subject: [PATCH 10/18] chore: bump dependency --- kaitai_struct_compiler | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kaitai_struct_compiler b/kaitai_struct_compiler index 414e6c5..9aefa32 160000 --- a/kaitai_struct_compiler +++ b/kaitai_struct_compiler @@ -1 +1 @@ -Subproject commit 414e6c5c69dfa6a30be34e13b4ff943e3266db9a +Subproject commit 9aefa325ae2b57df1a81fb82dfd68be6deb5a5c3 From c11a53254f2bb91bd1c63fdbcb2fe91c95849b6d Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 11:34:12 +0100 Subject: [PATCH 11/18] fix: install same awkward version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 94ff545..f0c06b3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,7 +30,7 @@ classifiers = [ ] dynamic = ["version"] dependencies = [ - "awkward>2.0.0" + "awkward>2.4.3" ] [project.optional-dependencies] From a160039962b54d0c49de6c03b3f2bc4a3d1f124a Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 11:34:24 +0100 Subject: [PATCH 12/18] fix: bump CMake minimum version --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 29b9f3a..28fe458 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.15...3.26) +cmake_minimum_required(VERSION 3.19...3.26) project(kaitai_awkward_runtime CXX) set(KSY "" CACHE STRING "Specify the KSY file path") From 34e26b74c2deaeb9e0241eb9d5ff9664d1d4f044 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 11:39:49 +0100 Subject: [PATCH 13/18] feat!: remove the KSY define Allow users to pass this in on the commandline --- pyproject.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f0c06b3..1a4a112 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -67,9 +67,6 @@ cmake.verbose = true logging.level = "INFO" cmake.build-type = "Debug" -[tool.scikit-build.cmake.define] -KSY = "schemas/animal.ksy" - [tool.setuptools_scm] write_to = "src/kaitai_awkward_runtime/_version.py" From b0ce00591cc869a3daa61a1d28a8955f101d7ef2 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 11:47:03 +0100 Subject: [PATCH 14/18] chore: remove dbug message --- CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 28fe458..e08967e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,8 +28,6 @@ if(NOT awkward-headers_POPULATED) add_subdirectory(${awkward-headers_SOURCE_DIR} ${awkward-headers_BINARY_DIR} EXCLUDE_FROM_ALL) endif() -MESSAGE(WARNING "${CMAKE_SOURCE_DIR}/kaitai_struct_compiler") - # Run sbt package command to compile kaitai_struct_compiler code execute_process( COMMAND sbt package From 64c6518cb2509a16380e38c7c8036c7345095261 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 11:47:43 +0100 Subject: [PATCH 15/18] chore: support config-settings --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a695131..66f2601 100644 --- a/README.md +++ b/README.md @@ -10,16 +10,15 @@ Steps for reproducing the environment: git clone --recursive https://github.com/ManasviGoyal/kaitai_awkward_runtime.git ``` -2. Change directory to `kaitai_awkward_runtime` and run CMake: +2. Change directory to `kaitai_awkward_runtime`: ``` cd kaitai_awkward_runtime -export CMAKE_ARGS="-DKSY=schemas/animal.ksy -DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_LIBS=ON" ``` -3. Install the library and open Python: +3. Install the library, specifying the schema, and open Python: ``` -pip install . +pip install . --config-settings 'cmake.define.KSY=schemas/animal.ksy' python ``` From 3161f079ff94e9db986203eae8b10ef0654874d4 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 12:23:42 +0100 Subject: [PATCH 16/18] chore: update links --- pyproject.toml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1a4a112..068ae42 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,10 +52,8 @@ docs = [ ] [project.urls] -Homepage = "https://github.com/org/kaitai_awkward_runtime" -"Bug Tracker" = "https://github.com/org/kaitai_awkward_runtime/issues" -Discussions = "https://github.com/org/kaitai_awkward_runtime/discussions" -Changelog = "https://github.com/org/kaitai_awkward_runtime/releases" +Homepage = "https://github.com/ManasviGoyal/kaitai_awkward_runtime" +"Bug Tracker" = "https://github.com/ManasviGoyal/kaitai_awkward_runtime/issues" [tool.scikit-build] From 33d75a5e9c8b15884e6fe2bd3c87d3cc6047433e Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 13:46:08 +0100 Subject: [PATCH 17/18] Revert "feat!: remove the KSY define" This reverts commit 34e26b74c2deaeb9e0241eb9d5ff9664d1d4f044. --- pyproject.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 068ae42..5de00d5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -65,6 +65,9 @@ cmake.verbose = true logging.level = "INFO" cmake.build-type = "Debug" +[tool.scikit-build.cmake.define] +KSY = "schemas/animal.ksy" + [tool.setuptools_scm] write_to = "src/kaitai_awkward_runtime/_version.py" From ec4c1520b14e2ed3ac7dc47ba1bcaa18b11bb714 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Mon, 9 Oct 2023 13:46:18 +0100 Subject: [PATCH 18/18] wip: build a shared library for now --- CMakeLists.txt | 38 ++++++++++----- README.md | 8 ++-- src/kaitai_awkward_runtime/__init__.py | 3 ++ src/main.cpp | 66 -------------------------- 4 files changed, 33 insertions(+), 82 deletions(-) delete mode 100644 src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e08967e..a7da6e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,18 +1,19 @@ cmake_minimum_required(VERSION 3.19...3.26) -project(kaitai_awkward_runtime CXX) +project(${SKBUILD_PROJECT_NAME} CXX) set(KSY "" CACHE STRING "Specify the KSY file path") - -string(REGEX MATCH "([^/]+)$" package_name ${KSY}) -string(REGEX REPLACE "\\.ksy$" "" package_name ${package_name}) -set(PACKAGE_NAME ${package_name}) - set(BUILD_SHARED_LIBS ON) -set(BUILD_STATIC_LIBS OFF) find_package(pybind11 CONFIG REQUIRED) -add_subdirectory(kaitai_struct_cpp_stl_runtime) +# Setup the RPATH for built libraries +if(APPLE) + set(CMAKE_INSTALL_RPATH "@loader_path") +else() + SET(CMAKE_INSTALL_RPATH "$ORIGIN") +endif() + +add_subdirectory(kaitai_struct_cpp_stl_runtime EXCLUDE_FROM_ALL) # Download awkward headers include(FetchContent) @@ -36,13 +37,24 @@ execute_process( ) execute_process( - COMMAND ./kaitai-struct-compiler -t awkward --outdir src-${PACKAGE_NAME} ${KSY} #package name should be declared as a string + COMMAND ./kaitai-struct-compiler -t awkward --outdir src-${SKBUILD_PROJECT_NAME} ${KSY} #package name should be declared as a string WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND_ERROR_IS_FATAL ANY ) -pybind11_add_module(awkward_${PACKAGE_NAME} src-${PACKAGE_NAME}/${PACKAGE_NAME}_main.cpp src-${PACKAGE_NAME}/${PACKAGE_NAME}.cpp) -target_link_libraries(awkward_${PACKAGE_NAME} PRIVATE awkward::layout-builder kaitai_struct_cpp_stl_runtime) +string(REGEX MATCH "([^/]+)$" KSY_MODULE ${KSY}) +string(REGEX REPLACE "\\.ksy$" "" KSY_MODULE ${KSY_MODULE}) + +pybind11_add_module(awkward_${KSY_MODULE} src-${SKBUILD_PROJECT_NAME}/${KSY_MODULE}_main.cpp src-${SKBUILD_PROJECT_NAME}/${KSY_MODULE}.cpp) +target_link_libraries(awkward_${KSY_MODULE} PRIVATE awkward::layout-builder kaitai_struct_cpp_stl_runtime) + +# Install pure-python files +file(GLOB_RECURSE PYTHON_SOURCES "src/${SKBUILD_PROJECT_NAME}/*.py") + +install( + TARGETS awkward_${KSY_MODULE} kaitai_struct_cpp_stl_runtime + LIBRARY DESTINATION "${SKBUILD_PROJECT_NAME}/lib" + RUNTIME DESTINATION "${SKBUILD_PROJECT_NAME}/lib" + ARCHIVE DESTINATION "${SKBUILD_PROJECT_NAME}/lib") -# Install into wheel -install(TARGETS awkward_${PACKAGE_NAME} DESTINATION .) +install(FILES ${PYTHON_SOURCES} DESTINATION ${SKBUILD_PROJECT_NAME}) diff --git a/README.md b/README.md index 66f2601..d15f217 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # kaitai_awkward_runtime -# awkward-kaitai Building Awkward Arrays using Kaitai binary file descriptors. Steps for reproducing the environment: @@ -16,9 +15,9 @@ cd kaitai_awkward_runtime ``` -3. Install the library, specifying the schema, and open Python: +3. Install the library, and open Python: ``` -pip install . --config-settings 'cmake.define.KSY=schemas/animal.ksy' +pip install . python ``` @@ -29,3 +28,6 @@ awkward_array = awkward_animal.load("data/animal.raw") print(awkward_array) ``` + +> **Info** +> `kaitai_awkward_runtime` depends upon `sbt`, and `gtest` dependencies. diff --git a/src/kaitai_awkward_runtime/__init__.py b/src/kaitai_awkward_runtime/__init__.py index 252fa70..8c2070e 100644 --- a/src/kaitai_awkward_runtime/__init__.py +++ b/src/kaitai_awkward_runtime/__init__.py @@ -10,3 +10,6 @@ from ._version import version as __version__ __all__ = ("__version__",) + + +from .lib.awkward_animal import * diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index cd3f59d..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include -#include -#include -#include -#include "../build/_deps/awkward-headers-src/layout-builder/awkward/LayoutBuilder.h" -#include "../src-animal/animal.h" - -namespace py = pybind11; - -/** - * Create a snapshot of the given builder, and return an `ak.Array` pyobject - * @tparam T type of builder - * @param builder builder - * @return pyobject of Awkward Array - */ -template -py::object snapshot_builder(const T &builder) { - // How much memory to allocate? - std::map names_nbytes = {}; - builder.buffer_nbytes(names_nbytes); - - // Allocate memory - std::map buffers = {}; - for (auto it: names_nbytes) { - uint8_t *ptr = new uint8_t[it.second]; - buffers[it.first] = (void *) ptr; - } - - // Write non-contiguous contents to memory - builder.to_buffers(buffers); - auto from_buffers = py::module::import("awkward").attr("from_buffers"); - - // Build Python dictionary containing arrays - py::dict container; - for (auto it: buffers) { - - py::capsule free_when_done(it.second, [](void *data) { - uint8_t *dataPtr = reinterpret_cast(data); - delete[] dataPtr; - }); - - uint8_t *data = reinterpret_cast(it.second); - container[py::str(it.first)] = py::array_t( - {names_nbytes[it.first]}, - {sizeof(uint8_t)}, - data, - free_when_done - ); - } - return from_buffers(builder.form(), builder.length(), container); -} - -/** - * Create array, and return its snapshot - * @return pyobject of Awkward Array - */ -py::object load(std::string file_path) { - std::ifstream infile(file_path, std::ifstream::binary); - kaitai::kstream ks(&infile); - animal_t zoo = animal_t(&ks); - return snapshot_builder(zoo.animal_builder); -} - -PYBIND11_MODULE(awkward_animal, m) { - m.def("load", &load); -} \ No newline at end of file