forked from dB-SPL/takprotobuf
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
importing delimited_protobuf.py from source to avoid external depende…
…ncy.
- Loading branch information
Showing
4 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |