Skip to content

Commit

Permalink
importing delimited_protobuf.py from source to avoid external depende…
Browse files Browse the repository at this point in the history
…ncy.
  • Loading branch information
ampledata committed Jun 22, 2024
1 parent f311d86 commit 51428fe
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Protobuf messages to Python objects, and serializing CoT XML messages as Protobu
[Documentation is available here.](https://takproto.rtfd.io/)

## Copyright & License

TAKProto is licensed under the MIT License.

Copyright Sensors & Signals LLC https://www.snstac.com

Copyright 2020 Delta Bravo-15 <[email protected]>
Expand All @@ -29,3 +32,6 @@ 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.

delimited_protobuf.py licensed under the Apache License, Version 2.0 and is
copyright 2024 Frank Dai https://github.com/soulmachine
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ package_dir =
takproto = takproto
install_requires =
protobuf >= 4.21.0
delimited-protobuf >= 1.0.0


[options.extras_require]
Expand Down
2 changes: 1 addition & 1 deletion stdeb.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[DEFAULT]
Package3: takproto
Replaces3: python3-takproto
Depends3: python3-protobuf, python3-delimited-protobuf
Depends3: python3-protobuf
74 changes: 74 additions & 0 deletions takproto/delimited_protobuf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Makefile from https://github.com/soulmachine/delimited-protobuf
#
# Copyright Frank Dai https://github.com/soulmachine
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""A read/write library for length-delimited protobuf messages"""

from __future__ import absolute_import

from typing import BinaryIO, Optional, Type, TypeVar

from google.protobuf.internal.decoder import _DecodeVarint
from google.protobuf.internal.encoder import _EncodeVarint
from google.protobuf.message import Message

T = TypeVar("YourProtoClass", bound=Message)


def _read_varint(stream: BinaryIO, offset: int = 0) -> int:
"""Read a varint from the stream."""
if offset > 0:
stream.seek(offset)
buf: bytes = stream.read(1)
if buf == b"":
return 0 # reached EOF
while (buf[-1] & 0x80) >> 7 == 1: # while the MSB is 1
new_byte = stream.read(1)
if new_byte == b"":
raise EOFError("unexpected EOF")
buf += new_byte
varint, _ = _DecodeVarint(buf, 0)
return varint


def read(stream: BinaryIO, proto_class_name: Type[T]) -> Optional[T]:
"""
Read a single length-delimited message from the given stream.
Similar to:
* [`CodedInputStream`](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/io/coded_stream.h#L66)
* [`parseDelimitedFrom()`](https://github.com/protocolbuffers/protobuf/blob/master/java/core/src/main/java/com/google/protobuf/Parser.java)
"""
size = _read_varint(stream)
if size == 0:
return None
buf = stream.read(size)
msg = proto_class_name()
msg.ParseFromString(buf)
return msg


def write(stream: BinaryIO, msg: T):
"""
Write a single length-delimited message to the given stream.
Similar to:
* [`CodedOutputStream`](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/io/coded_stream.h#L47)
* [`MessageLite#writeDelimitedTo`](https://github.com/protocolbuffers/protobuf/blob/master/java/core/src/main/java/com/google/protobuf/MessageLite.java#L126)
"""
assert stream is not None
_EncodeVarint(stream.write, msg.ByteSize())
stream.write(msg.SerializeToString())

0 comments on commit 51428fe

Please sign in to comment.