Skip to main content

A decoder for Google Maps protobuf format

Project description

deproto

deproto logo

A Python package for Google Maps protobuf format

PyPI version Python versions License GitHub stars GitHub forks GitHub issues Downloads

FeaturesInstallationQuick StartDocumentationAdvancedTesting

A Python package for decoding, manipulating, and encoding Google Maps protobuf format strings. This library provides an intuitive way to work with protobuf structures commonly found in Google Maps URLs and data.

Features

  • Decode Google Maps protobuf strings into a tree structure
  • Create and modify protobuf structures using multiple approaches
  • Automatic type detection and handling
  • Parent-child relationship tracking
  • Automatic total count management in clusters
  • Tree visualization for debugging
  • Support for various data types

Installation

Install using pip:

pip install -U deproto

Quick Start

from deproto import Protobuf

# Example protobuf string from Google Maps
pb_string = "!1m3!1s2024!2i42!3stest"

# Create decoder instance
decoder = Protobuf(pb_string)

# Decode the string into a tree structure
cluster = decoder.decode()

# Print the tree structure
decoder.print_tree()

# Make changes to values
cluster[0][0].change("2025")

# Encode back to protobuf format
encoded = decoder.encode()

Building Protobuf Structures

There are multiple ways to build protobuf structures:

1. Direct Construction

from deproto.cluster import Cluster
from deproto.node import Node
from deproto.types import StringType, IntType

# Create a structure directly
root = Cluster(1, [
    Node(1, "hello", StringType()),
    Cluster(2, [
        Node(1, 42, IntType())
    ])
])

2. Using add() with Tuples

root = Cluster(1)
root.add(1, [(1, "hello"), (2, 42)])  # Types auto-detected

3. Using add() with Nodes

root = Cluster(1)
root.add(1, [
    Node(1, "hello", StringType()),
    Node(2, 42, IntType())
])

4. Mixed Approach

root = Cluster(1)
root.add(1, Node(1, "hello", StringType()))
root.add(2, [(1, 42)])  # Type auto-detected

Complex Structures

You can build complex nested structures:

root = Cluster(1, [
    Node(1, "metadata", StringType()),
    Cluster(2, [
        Node(1, 42, IntType()),
        Node(2, True, BoolType()),
        Cluster(3, [
            Node(1, "nested", StringType()),
            Node(2, 3.14, IntType())
        ])
    ]),
    Node(3, "end", StringType())
])

Tree Visualization

The print_tree() method provides a clear visualization of the protobuf structure:

1m3
├── 1s2024
├── 2i42
└── 3stest

Supported Data Types

Type Description Example
B Bytes Binary data
b Boolean True/False
d Double 3.14159
e Enum 1, 2, 3
f Float 3.14
i Int32/64 42
s String "hello"
x Fixed32 12345
y Fixed64 123456789
z Base64String Encoded string

Advanced Usage

Parent-Child Relationships

The library maintains parent-child relationships automatically:

root = Cluster(1)
child = Cluster(2, [
    Node(1, True, BoolType())
])
root.append(child)

assert child.parent == root
assert child[0].parent == child

Automatic Total Management

Cluster totals are managed automatically when adding or removing nodes. The total includes both nodes and clusters:

root = Cluster(1)

# Adding nodes in a cluster
root.add(1, [  # This creates: Cluster(1, [Node(1, "test"), Node(2, 42)])
    Node(1, "test", StringType()),
    Node(2, 42, IntType())
])
print(root.total)  # 3 (1 for the cluster + 2 for the nodes)

# Adding a single node
root.add(2, Node(3, "direct", StringType()))
print(root.total)  # 4 (previous 3 + 1 for the new node)

# Complex structure
root.add(3, [  # Creates nested clusters
    Node(1, "hello", StringType()),
    Cluster(2, [
        Node(1, 42, IntType())
    ])
])
print(root.total)  # 8 (previous 4 + 1 for new cluster + 1 for Node("hello") 
                   #    + 1 for inner Cluster + 1 for Node(42))

# Removing a cluster removes its total contribution
root.delete(3)  # Removes the complex structure
print(root.total)  # 4 (back to previous state)

Note: When using add() with a list, it creates a new cluster containing those items, which adds to the total count.

Special Character Handling

String values with special characters are handled automatically:

node = Node(1, "test!*", StringType())
print(node.value_raw)  # "test*21*2A"
print(node.value)      # "test!*"

Testing

Run the test suite:

# Using pytest
pytest tests/

# With coverage
coverage run -m pytest tests/
coverage report

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Ijaz Ur Rahim (ijazurrahim.com | @MrDebugger)

Current Version

0.2.0 - See CHANGELOG.md for version history and details.

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

deproto-0.2.0.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

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

deproto-0.2.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file deproto-0.2.0.tar.gz.

File metadata

  • Download URL: deproto-0.2.0.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for deproto-0.2.0.tar.gz
Algorithm Hash digest
SHA256 99bea1b14a8d1b98728edf2a785e2f2e1842237434311da0e482efceba2cdcbf
MD5 d74f7adee94f150f08092b603a28be33
BLAKE2b-256 ab793dbc5076ab3dc4c4bbff2dcaab07833bb69947165165b8a1faf22b02ecbe

See more details on using hashes here.

File details

Details for the file deproto-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: deproto-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for deproto-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 25434c02e3f33551b317af290e98e5db4054ab3fa7ceba084c3e610ab4b54175
MD5 159dff8bf169d418c4e37c555913cf1f
BLAKE2b-256 79d8e8cd37ac8211ebc8323b373e61872ff116eaf9a1c4d28a0efaf2420e756b

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