binary-file-parser


Namebinary-file-parser JSON
Version 0.1.3 PyPI version JSON
download
home_page
SummaryRead/Write binary files after describing their specifications in code (similar to an ORM table schema)
upload_time2024-02-23 10:58:05
maintainer
docs_urlNone
author
requires_python>=3.10
licenseMIT License Copyright (c) 2022 Alian713 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords binary file parser
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # BinaryFileParser
BinaryFileParser allows the user to create a binary file format specification in the form of a struct and then use it to
read/write files.

## Installation

On Linux:
```sh
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install binary-file-parser
```

On Windows:
```sh
> py -m venv venv
> venv/Scripts/activate
> pip install binary-file-parser
```

## Getting Started

This is a very basic script to give you an idea of how to use this library. Check out the API Reference (coming soon™) for documentation containing more details on how to use this library.

```py
from binary_file_parser import BaseStruct, Retriever
from binary_file_parser.types import uint32, uint64, str32, FixedLenStr

class Spam(BaseStruct):
    file_version: str = Retriever(FixedLenStr[4], default = 0)
    creator_name: str = Retriever(str32, default = 0)
    saved_timestamp: int = Retriever(uint64, default = 0)
    eggs: int = Retriever(int32, default = 0)

# read a binary file that has the above format
file = Spam.from_file("path/to/file")

# modify the creator name
file.creator_name = "Alian713"
file.eggs = 20

# write the modified data to a new file
file.to_file("path/to/write/to")
```

## A Slightly More Complex Example
The main magic of this library is that:

1. You can use your own structs within another struct
2. Event hooks help you keep any interdependencies in the file structure synchronised:

```py
from __future__ import annotations

from binary_file_parser import BaseStruct, Retriever
from binary_file_parser.types import FixedLenArray, uint8


class Pixel(BaseStruct):
    red: int = Retriever(uint8, default = 0)
    green: int = Retriever(uint8, default = 0)
    blue: int = Retriever(uint8, default = 0)
    alpha: int = Retriever(uint8, default = 0)

    def __init__(
        self,
        red: int,
        green: int,
        blue: int,
        alpha: int = 0,
    ):
        super().__init__(initialise_defaults = False)
        self.red = red
        self.green = green
        self.blue = blue
        self.alpha = alpha

class Img(BaseStruct):
    @staticmethod
    def _set_width(retriever: Retriever, obj: Img):
        # here Img.pixels.dtype refers to FixedLenArray
        Img.pixels.dtype.length = obj._width

    @staticmethod
    def _set_height(retriever: Retriever, obj: Img):
        # The repeat value defines how many times a single retriever should read data
        Retriever.set_repeat(Img.pixels, obj, obj._height)

    @staticmethod
    def _update_dims(retriever: Retriever, obj: Img):
        # this ensures that when the file is written back, the height and width being written back to file are
        # up to date
        obj._height = obj.height
        Img.pixels.dtype.length = obj._width = obj.width
    
    _width: int = Retriever(uint32, default = 100, on_read = [_set_width], on_write = [_update_dims])
    _height: int = Retriever(uint32, default = 200, on_set = [_set_height])
    pixels: list[list[Pixel]] = Retriever(
        FixedLenArray[Pixel, 100], default = [Pixel(0, 0, 0) for _ in range(100)], repeat = 200
    )

    @property
    def width(self) -> int:
        return len(self.pixels[0])

    @property
    def height(self) -> int:
        return len(self.pixels)

# Make a new image from all defaults
a = Img()
# Note: Mutable defaults will be shallow copied, (this means all rows of pixels in the above example are the same!)
# If you have larger structs or nested structs, use default_factory (coming soon:tm:) instead
```

## About the Author

If you have any questions, suggestions or feedback regarding the library, feel free to send me a message on discord!

| Author   | Discord       |
|----------|---------------|
| Alian713 | Alian713#0069 |

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "binary-file-parser",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "",
    "keywords": "binary,file,parser",
    "author": "",
    "author_email": "Divy1211 <divy1211.dc@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/4e/ef/a722d9e36c378ba01c0bfcc90811ee9a0b1027537181f0648e18d0bae55b/binary-file-parser-0.1.3.tar.gz",
    "platform": null,
    "description": "# BinaryFileParser\r\nBinaryFileParser allows the user to create a binary file format specification in the form of a struct and then use it to\r\nread/write files.\r\n\r\n## Installation\r\n\r\nOn Linux:\r\n```sh\r\n$ python3 -m venv venv\r\n$ source venv/bin/activate\r\n$ pip install binary-file-parser\r\n```\r\n\r\nOn Windows:\r\n```sh\r\n> py -m venv venv\r\n> venv/Scripts/activate\r\n> pip install binary-file-parser\r\n```\r\n\r\n## Getting Started\r\n\r\nThis is a very basic script to give you an idea of how to use this library. Check out the API Reference (coming soon\u2122) for documentation containing more details on how to use this library.\r\n\r\n```py\r\nfrom binary_file_parser import BaseStruct, Retriever\r\nfrom binary_file_parser.types import uint32, uint64, str32, FixedLenStr\r\n\r\nclass Spam(BaseStruct):\r\n    file_version: str = Retriever(FixedLenStr[4], default = 0)\r\n    creator_name: str = Retriever(str32, default = 0)\r\n    saved_timestamp: int = Retriever(uint64, default = 0)\r\n    eggs: int = Retriever(int32, default = 0)\r\n\r\n# read a binary file that has the above format\r\nfile = Spam.from_file(\"path/to/file\")\r\n\r\n# modify the creator name\r\nfile.creator_name = \"Alian713\"\r\nfile.eggs = 20\r\n\r\n# write the modified data to a new file\r\nfile.to_file(\"path/to/write/to\")\r\n```\r\n\r\n## A Slightly More Complex Example\r\nThe main magic of this library is that:\r\n\r\n1. You can use your own structs within another struct\r\n2. Event hooks help you keep any interdependencies in the file structure synchronised:\r\n\r\n```py\r\nfrom __future__ import annotations\r\n\r\nfrom binary_file_parser import BaseStruct, Retriever\r\nfrom binary_file_parser.types import FixedLenArray, uint8\r\n\r\n\r\nclass Pixel(BaseStruct):\r\n    red: int = Retriever(uint8, default = 0)\r\n    green: int = Retriever(uint8, default = 0)\r\n    blue: int = Retriever(uint8, default = 0)\r\n    alpha: int = Retriever(uint8, default = 0)\r\n\r\n    def __init__(\r\n        self,\r\n        red: int,\r\n        green: int,\r\n        blue: int,\r\n        alpha: int = 0,\r\n    ):\r\n        super().__init__(initialise_defaults = False)\r\n        self.red = red\r\n        self.green = green\r\n        self.blue = blue\r\n        self.alpha = alpha\r\n\r\nclass Img(BaseStruct):\r\n    @staticmethod\r\n    def _set_width(retriever: Retriever, obj: Img):\r\n        # here Img.pixels.dtype refers to FixedLenArray\r\n        Img.pixels.dtype.length = obj._width\r\n\r\n    @staticmethod\r\n    def _set_height(retriever: Retriever, obj: Img):\r\n        # The repeat value defines how many times a single retriever should read data\r\n        Retriever.set_repeat(Img.pixels, obj, obj._height)\r\n\r\n    @staticmethod\r\n    def _update_dims(retriever: Retriever, obj: Img):\r\n        # this ensures that when the file is written back, the height and width being written back to file are\r\n        # up to date\r\n        obj._height = obj.height\r\n        Img.pixels.dtype.length = obj._width = obj.width\r\n    \r\n    _width: int = Retriever(uint32, default = 100, on_read = [_set_width], on_write = [_update_dims])\r\n    _height: int = Retriever(uint32, default = 200, on_set = [_set_height])\r\n    pixels: list[list[Pixel]] = Retriever(\r\n        FixedLenArray[Pixel, 100], default = [Pixel(0, 0, 0) for _ in range(100)], repeat = 200\r\n    )\r\n\r\n    @property\r\n    def width(self) -> int:\r\n        return len(self.pixels[0])\r\n\r\n    @property\r\n    def height(self) -> int:\r\n        return len(self.pixels)\r\n\r\n# Make a new image from all defaults\r\na = Img()\r\n# Note: Mutable defaults will be shallow copied, (this means all rows of pixels in the above example are the same!)\r\n# If you have larger structs or nested structs, use default_factory (coming soon:tm:) instead\r\n```\r\n\r\n## About the Author\r\n\r\nIf you have any questions, suggestions or feedback regarding the library, feel free to send me a message on discord!\r\n\r\n| Author   | Discord       |\r\n|----------|---------------|\r\n| Alian713 | Alian713#0069 |\r\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2022 Alian713  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Read/Write binary files after describing their specifications in code (similar to an ORM table schema)",
    "version": "0.1.3",
    "project_urls": {
        "Homepage": "https://github.com/Divy1211/BinaryFileParser"
    },
    "split_keywords": [
        "binary",
        "file",
        "parser"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5da1a701d6a5770a763c3cb7401e064a30b679744b8061fe8ba2dc0ddd067df5",
                "md5": "f1b3765b5b159aa0e6a0782a63ac6a41",
                "sha256": "c0a878ed7e9d70feaec58e74b42cd7376a94a81ce641beec31dc328a53ac7436"
            },
            "downloads": -1,
            "filename": "binary_file_parser-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f1b3765b5b159aa0e6a0782a63ac6a41",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 24488,
            "upload_time": "2024-02-23T10:58:01",
            "upload_time_iso_8601": "2024-02-23T10:58:01.210152Z",
            "url": "https://files.pythonhosted.org/packages/5d/a1/a701d6a5770a763c3cb7401e064a30b679744b8061fe8ba2dc0ddd067df5/binary_file_parser-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4eefa722d9e36c378ba01c0bfcc90811ee9a0b1027537181f0648e18d0bae55b",
                "md5": "cdc9754b5a1b81c427bbc518162e1621",
                "sha256": "198dd4b96cc6d4498a65cfcc5293081130ddbb7e05f12e309aae407c17f8fb83"
            },
            "downloads": -1,
            "filename": "binary-file-parser-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "cdc9754b5a1b81c427bbc518162e1621",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 18662,
            "upload_time": "2024-02-23T10:58:05",
            "upload_time_iso_8601": "2024-02-23T10:58:05.650020Z",
            "url": "https://files.pythonhosted.org/packages/4e/ef/a722d9e36c378ba01c0bfcc90811ee9a0b1027537181f0648e18d0bae55b/binary-file-parser-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-23 10:58:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Divy1211",
    "github_project": "BinaryFileParser",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "binary-file-parser"
}
        
Elapsed time: 0.19342s