Skip to main content

Read and write Valve's KeyValues3 format

Project description

keyvalues3

KeyValues3 is a Valve developed data format. It is similar in structure to JSON, but supports binary encodings, versioning, and data annotations. The text syntax also has some minor ergonomic improvements (support for single- and multi-line comments, trailing commas, and multi-line strings.)

Usage

import keyvalues3 as kv3
bt_config = kv3.read("tests/documents/bt_config.kv3")

>>> bt_config.keys()
dict_keys(['default', 'low', 'fair', 'normal', 'tough', 'hard', 'very_hard', 'expert', 'elite'])

>>> bt_config["elite"]["reaction_time"]
0.12
# The root value is most of the time a dict
>>> type(bt_config.value)
<class 'dict'>

>>> bt_config.original_encoding
Encoding(name='text', version=UUID('e21c7f3c-8a33-41c5-9977-a76d3a32aa0d'))

>>> bt_config.format
Format(name='generic', version=UUID('7412167c-06e9-4698-aff2-e63eb59037e7'))

# To write it back
>>> kv3.write(bt_config, "tests/documents/bt_config.kv3", use_original_encoding=True)

# Write to a stream
>>> import sys
>>> kv3.write({"key": [1,2,3]}, sys.stdout)
<!-- kv3 encoding:text:version{e21c7f3c-8a33-41c5-9977-a76d3a32aa0d} format:generic:version{7412167c-06e9-4698-aff2-e63eb59037e7} -->
{
        key = [1, 2, 3]
}

Install PyPI version

pip install keyvalues3

Supported encodings

Encoding 👩‍💻 Read 📖 Write ✍️
Text UTF-8 Yes ✔️ Yes ✔️
Text UTF-8 Headerless Yes ✔️ Yes ✔️
Binary (VKV) Uncompressed Yes ✔️ Yes ✔️
Binary (VKV) LZ4 Yes ✔️ Yes ✔️
Binary (3VK) V1 Yes ✔️ No ⛔
Binary (3VK) V2 Yes ✔️ No ⛔
Binary (3VK) V3 Yes ✔️ No ⛔
Binary (3VK) V4 Yes ✔️ No ⛔
Binary (3VK) V5 Yes ✔️ No ⛔

Using dataclasses

Dataclasses (marked with @dataclass) are also supported for serialization (uses asdict())

@dataclass
class RenderMeshFile(_Node):  # _Node defines _class, name, notes, and children (+ helper methods)
    filename: resourcepath = ""
    import_translation: list[float] = field(default_factory=lambda: [0, 0, 0])
    import_rotation: list[float] = field(default_factory=lambda: [0, 0, 0])
    import_scale: float = 1.0
    align_origin_x_type: str = "None"
    align_origin_y_type: str = "None"
    align_origin_z_type: str = "None"
    parent_bone: namelink = ""
    import_filter: dict = field(
        default_factory=lambda:dict(
            exclude_by_default = False,
            exception_list = [  ]
        )
    )

You can find more ModelDoc dataclasses here.

Developing

python --version Ensure you're using Python 3.12 or greater.
python -m pip install .
python -m pip install pytest I don't know why this is needed
python -m pytest

Credits

Valve Corporation® for making KeyValues3.
ValveResourceFormat for the legacy binary reader reference.
SourceIO by REDxEYE for new binary reader code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

keyvalues3-0.5.tar.gz (121.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

keyvalues3-0.5-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

Details for the file keyvalues3-0.5.tar.gz.

File metadata

  • Download URL: keyvalues3-0.5.tar.gz
  • Upload date:
  • Size: 121.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.5

File hashes

Hashes for keyvalues3-0.5.tar.gz
Algorithm Hash digest
SHA256 c5421a220f85f57df3ab3e610428bca745b5f1557e6987eea89a3f4926d52de4
MD5 8e9d0e37d479dea9e2b230c942d16c83
BLAKE2b-256 f99c77653df7e48cc45243b37bb935199bcc200a7fc9c95f5b926b6f44391250

See more details on using hashes here.

File details

Details for the file keyvalues3-0.5-py3-none-any.whl.

File metadata

  • Download URL: keyvalues3-0.5-py3-none-any.whl
  • Upload date:
  • Size: 23.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.5

File hashes

Hashes for keyvalues3-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d6342361ed393405c469d6bc39d55cc5680944f05efb1bfa3eebfea9b3ff6f68
MD5 5fd9476dc8158fdb4bcd92ad9e7a6068
BLAKE2b-256 2057a3911d4069949ad5d81b675a10bd1edfb2e784743e837d5e35ce62942525

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page