Skip to main content

A library to serialize and deserialize gvariant values in gschema files.

Project description

GVariant Python Utilities

This project provides a comprehensive toolkit for working with GVariant type strings and value representations within Python. It allows for the bidirectional conversion between GVariant type signatures, GVariant text formats (gschema compatible), and native Python objects.


Features

  • Type Parsing: Converts GVariant type strings (e.g., a{sv}) into standard Python typing hints.
  • Value Conversion: Parses GVariant text literals into coerced Python types based on a provided signature.
  • GSchema Serialization: Serializes Python objects into strings compatible with GVariant text format and glib-compile-schemas.
  • Recursive Support: Handles nested containers including arrays, tuples, dictionaries, and "maybe" types up to the GVariant limit of 65 levels.

Core Components

1. GVariantParser

The parser implements a recursive descent strategy to interpret GVariant type signatures. It maps basic GVariant characters to Python types and handles complex container definitions.

GVariant Character Python Type Description
b bool Boolean
s, o, g str String, Object Path, or Signature
y, n, i, x int Various width signed integers
q, u, t int Various width unsigned integers
d float Double precision floating point
a List[T] Array of type T
m Optional[T] Maybe/Nullable type T
() Tuple[...] Fixed-size tuple
{} Dict[K, V] Dictionary entry (usually within an array)
v, *, ? Any Indefinite or variant types

2. GVariantValueConverter

This utility takes a string representation of a value and a GVariant type string. It uses ast.literal_eval for safe structural parsing and then recursively coerces the data into the target Python types. It specifically handles GVariant-specific keywords like nothing for null values and true/false for booleans.

3. GVariantSerializer

The serializer ensures that Python data structures are converted back into strings that the gschema compiler can understand. This includes converting Python's True to true, None to nothing, and ensuring correct bracket usage for arrays and dictionaries.


Usage Examples

Parsing Type Strings

from gvariant_utils import GVariantParser

parser = GVariantParser()
python_type = parser.parse("a{si}") 
# Result: typing.Dict[str, int]

Converting String Literals to Python Objects

from gvariant_utils import GVariantValueConverter

converter = GVariantValueConverter()
value = converter.parse_value_string("[1, 2, 3]", "ai")
# Result: [1, 2, 3] (as a list of integers)

maybe_val = converter.parse_value_string("nothing", "ms")
# Result: None

Serializing to GSchema Format

from gvariant_utils import GVariantSerializer

data = {"enabled": True, "timeout": 30}
gschema_string = GVariantSerializer.serialize(data)
# Result: '{"enabled": true, "timeout": 30}'

Technical Specifications

  • Nesting Limit: Following the D-Bus and GVariant specifications, this implementation supports a recursion depth of up to 65 levels.
  • Basic Type Restriction: In accordance with the GVariant spec, dictionary keys (the first element in {}) are restricted to basic types to ensure hashability.
  • Indefinite Types: Types such as v (variant) and * (any) are mapped to Python's Any type, as their specific structure is determined at runtime rather than by the signature.

Implementation Notes

The implementation relies on the Python typing module for type representation and the ast module for secure evaluation of string literals. It avoids the use of eval(), making it safe for use with untrusted input strings.

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

pygvariant-0.1.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

pygvariant-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file pygvariant-0.1.0.tar.gz.

File metadata

  • Download URL: pygvariant-0.1.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for pygvariant-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2ebebf5ef9df41f08ae799a6b2e251ed501f6249a3f010d1eeb4a3eb0d858728
MD5 d01734eacd84fcc5d4382525909c725f
BLAKE2b-256 9c4d7eead1a389f72ffc02f1141cbadd8d50505c5e8e0790c73a3556bc4330c5

See more details on using hashes here.

File details

Details for the file pygvariant-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pygvariant-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for pygvariant-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4648ac1e9b3c9ca2af0a83a45a477abdc624febb77749feaf02fcd5ff1b36510
MD5 2452301b43098360dbceea62e05af3d0
BLAKE2b-256 967e50d60add36cac3d16759f40faa33c63a605c37fa99cd964626e823691373

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