|
| 1 | +kaitaiStructCompile.py [](https://unlicense.org/) |
| 2 | +====================== |
| 3 | +[wheel](https://gitlab.com/kaitaiStructCompile.py/kaitaiStructCompile.py/-/jobs/artifacts/master/raw/wheels/kaitaiStructCompile-0.CI-py3-none-any.whl?job=build) |
| 4 | +[](https://pypi.python.org/pypi/kaitaiStructCompile.py) |
| 5 | +[](https://gitlab.com/kaitaiStructCompile.py/kaitaiStructCompile.py/commits/master) |
| 6 | +[](https://coveralls.io/r/KOLANICH/kaitaiStructCompile.py) |
| 7 | +[](https://gitlab.com/kaitaiStructCompile.py/kaitaiStructCompile.py/commits/master) |
| 8 | +[](https://libraries.io/github/KOLANICH/kaitaiStructCompile.py) |
| 9 | +[](https://github.com/KOLANICH-tools/antiflash.py) |
| 10 | + |
| 11 | +This is a tool automating compilation [Kaitai Struct](https://github.com/kaitai-io/kaitai_struct) ```*.ksy``` files into python ones. |
| 12 | + |
| 13 | + |
| 14 | +Features |
| 15 | +-------- |
| 16 | + |
| 17 | +* python bindings to compile KS `.ksy` specs into python source code. |
| 18 | +* importer allowing to import `ksy`s. Seamlessly compiles `ksy`s into python sources. Useful for playing in IPython shell. |
| 19 | +* `setuptools` plugin to automate compiling `ksy`s into `py` in process of building of your package. |
| 20 | + |
| 21 | +Prerequisites |
| 22 | +------------- |
| 23 | +* [Kaitai Struct compiler](https://github.com/kaitai-io/kaitai_struct_compiler) must be unpacked somewhere and all its prerequisites like `JRE` or `OpenJDK ` must be installed. |
| 24 | +* Path to Kaitai Struct compiler root dir (the one containing `lib`, `bin` and `formats`) must be exposed as `KAITAI_STRUCT_ROOT` environment variable. |
| 25 | +* A backend must be installed. [CLI backend](https://github.com/kaitaiStructCompile/kaitaiStructCompileBackendCLI) is currently the only one publicly available. |
| 26 | + |
| 27 | +Usage |
| 28 | +----- |
| 29 | + |
| 30 | +### Importing a ksy |
| 31 | + |
| 32 | +```python |
| 33 | +import kaitaiStructCompile.importer |
| 34 | +kaitaiStructCompile.importer._importer.searchDirs.append(Path("./dirWithKSYFiles")) # you can add a dir to search for KSY files. |
| 35 | +kaitaiStructCompile.importer._importer.flags["readStoresPos"]=True # you can set compiler flags, for more details see the JSON schema |
| 36 | +from kaitaiStructCompile.importer.test import Test |
| 37 | +Test # kaitaiStructCompile.importer.test.Test |
| 38 | +``` |
| 39 | + |
| 40 | +### Manual compilation |
| 41 | + |
| 42 | +```python |
| 43 | +from kaitaiStructCompile import compile |
| 44 | +compile("./ksyFilePath.ksy", "./outputDir", additionalFlags=[ |
| 45 | + #you can expose additional params here |
| 46 | +]) |
| 47 | +``` |
| 48 | + |
| 49 | +### Backends |
| 50 | + |
| 51 | +#### CLI |
| 52 | +See https://github.com/kaitaiStructCompile/kaitaiStructCompileBackendCLI for more info. |
| 53 | + |
| 54 | +#### Other backends |
| 55 | +More backends may be available in future. Even by third parties. Even for alternative Kaitai Struct compiler implementations. |
| 56 | + |
| 57 | + |
| 58 | +#### JVM backend |
| 59 | +I have created a JVM backend. It is **not distributed** and **YOU CANNOT OBTAIN IT** until [the issue with GPL license virality](https://github.com/kaitai-io/kaitai_struct/issues/466) is resolved. |
| 60 | + |
| 61 | +Cons: |
| 62 | +* GPL virality |
| 63 | +* breaks sometimes |
| 64 | + |
| 65 | + |
| 66 | +Pros: |
| 67 | +* better security |
| 68 | +* works much faster |
| 69 | + |
| 70 | + |
| 71 | +### `importer` |
| 72 | +This is an importer allowing to import `ksy`s. Seamlessly compiles `ksy`s into python sources. Useful for playing in IPython shell. |
| 73 | + |
| 74 | + |
| 75 | +#### Usage |
| 76 | + |
| 77 | +```python |
| 78 | +import kaitaiStructCompile.importer |
| 79 | +kaitaiStructCompile.importer._importer.searchDirs.append(Path("./dirWithKSYFiles")) # you can add a dir to search for KSY files. |
| 80 | +kaitaiStructCompile.importer._importer.flags["readStoresPos"] = True # you can set compiler flags, for more details see the JSON schema |
| 81 | +from kaitaiStructCompile.importer.test import Test |
| 82 | +Test # kaitaiStructCompile.importer.test.Test |
| 83 | +``` |
| 84 | + |
| 85 | + |
| 86 | +### Build systems extensions |
| 87 | +This is a set of build system plugins allowing you to just specify `ksy` files you need to compile into python sources in process of building your package in a declarative way. |
| 88 | + |
| 89 | +Supported build systems: |
| 90 | +* `setuptools` |
| 91 | +* `hatchling` |
| 92 | + * to enable also add an empty `[tool.hatch.build.hooks.kaitai_transpile]` section into your `pyproject.toml`. |
| 93 | +* `poetry` |
| 94 | + * works only with `poetry`, not `poetry-core` (because `poetry-core` has no support of plugins at all). |
| 95 | + * adds `kaitai transpile` command. |
| 96 | + |
| 97 | + |
| 98 | +#### Usage |
| 99 | +Just an add a property `tool.kaitai` into the `pyproject.toml`. This dict specified and documented with [the JSON Schema](./kaitaiStructCompile/schemas/config.schema.json), so read it carefully. |
| 100 | + |
| 101 | + |
| 102 | +Here a piece from one project with comments follows: |
| 103 | +```toml |
| 104 | +[build-system] |
| 105 | +requires = ["setuptools>=44", "wheel", "setuptools_scm[toml]>=3.4.3", "kaitaiStructCompile[toml]"] # we need this tool ! |
| 106 | +... |
| 107 | + |
| 108 | +# `tool.kaitai.repos` must contain 2-level nested dicts. The first component is usually repo URI, the second one is the refspec. But one can put there irrelevant things, if one needs for example compile multiple stuff from the same repo and refspec |
| 109 | +[tool.kaitai.repos."https://github.com/KOLANICH/kaitai_struct_formats.git"."mdt"] |
| 110 | +# one can put something irrelevant into the components of the section header, then he has to set `git` and `refspec` |
| 111 | +#git = "https://github.com/KOLANICH/kaitai_struct_formats.git" |
| 112 | +#refspec = "mdt" |
| 113 | +update = true # automatically download the freshest version of the repo each time the project is built |
| 114 | +search = true # glob all the spec from `inputDir` |
| 115 | +localPath = "kaitai_struct_formats" # Where (rel to `setup.py` dir) the repo of formats will be downloaded and from which location the compiler will use it. |
| 116 | +inputDir = "scientific/nt_mdt" # the directory we take KSYs from rel to `localPath` |
| 117 | +outputDir = "NTMDTRead/kaitai" # the directory we will put the generated file rel to setup.py dir |
| 118 | + |
| 119 | +[tool.kaitai.repos."https://github.com/KOLANICH/kaitai_struct_formats.git"."mdt".formats] |
| 120 | +# here we declare our targets. MUST BE SET, even if empty!!! |
| 121 | +``` |
| 122 | + |
| 123 | +Or one can pass the dict of the similar structure to `setuptools.setup` directly using `kaitai` param (in this case you set all the paths yourself!!!), but it is disrecommended. Use the declarative config everywhere it is possible. |
| 124 | + |
| 125 | +#### Real Examples |
| 126 | + |
| 127 | +[1](https://github.com/KOLANICH-physics/NTMDTRead/blob/master/pyproject.toml) |
| 128 | +[2](https://github.com/KOLANICH-physics/SpecprParser.py/blob/master/pyproject.toml) |
| 129 | +[3](https://github.com/KOLANICH-tools/FrozenTable.py/blob/master/pyproject.toml) |
| 130 | +[4](https://github.com/KOLANICH-libs/lime.py/blob/master/pyproject.toml) |
| 131 | +[5](https://github.com/KOLANICH-libs/sunxi_fex.py/blob/master/pyproject.toml) |
| 132 | +[6](https://github.com/KOLANICH-tools/WindowsTelemetryViewer.py/blob/master/pyproject.toml) |
| 133 | +[7](https://github.com/KOLANICH-libs/MFCTool.py/blob/master/pyproject.toml) |
| 134 | +[8](https://github.com/KOLANICH-ML/RDataParser.py/blob/master/pyproject.toml) |
| 135 | +[9](https://github.com/KOLANICH-tools/inkwave.py/blob/master/pyproject.toml) |
| 136 | + |
0 commit comments