Skip to content

Commit

Permalink
Catch excessive protobuf warnings (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdb9696 authored Sep 25, 2024
1 parent 0743d9b commit ea6a505
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 52 deletions.
31 changes: 0 additions & 31 deletions firebase_messaging/const.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
"""Constants module."""

from .proto.mcs_pb2 import ( # pylint: disable=no-name-in-module
Close,
DataMessageStanza,
HeartbeatAck,
HeartbeatPing,
IqStanza,
LoginRequest,
LoginResponse,
StreamErrorStanza,
)

GCM_REGISTER_URL = "https://android.clients.google.com/c2dm/register3"
GCM_CHECKIN_URL = "https://android.clients.google.com/checkin"
GCM_SERVER_KEY_BIN = (
Expand Down Expand Up @@ -41,23 +30,3 @@
MCS_PORT = 5228
MCS_SELECTIVE_ACK_ID = 12
MCS_STREAM_ACK_ID = 13

# MCS Message Types and Tags
MCS_MESSAGE_TAG = {
HeartbeatPing: 0,
HeartbeatAck: 1,
LoginRequest: 2,
LoginResponse: 3,
Close: 4,
"MessageStanza": 5,
"PresenceStanza": 6,
IqStanza: 7,
DataMessageStanza: 8,
"BatchPresenceStanza": 9,
StreamErrorStanza: 10,
"HttpRequest": 11,
"HttpResponse": 12,
"BindAccountRequest": 13,
"BindAccountResponse": 14,
"TalkMetadata": 15,
}
46 changes: 34 additions & 12 deletions firebase_messaging/fcmpushclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,50 @@

from .const import (
MCS_HOST,
MCS_MESSAGE_TAG,
MCS_PORT,
MCS_SELECTIVE_ACK_ID,
MCS_VERSION,
)
from .fcmregister import FcmRegister, FcmRegisterConfig
from .proto.mcs_pb2 import ( # pylint: disable=no-name-in-module
Close,
DataMessageStanza,
HeartbeatAck,
HeartbeatPing,
IqStanza,
LoginRequest,
LoginResponse,
SelectiveAck,
)
from .fcmregister import FcmRegister, FcmRegisterConfig, catch_protobuf_warnings

with catch_protobuf_warnings():
from .proto.mcs_pb2 import ( # pylint: disable=no-name-in-module
Close,
DataMessageStanza,
HeartbeatAck,
HeartbeatPing,
IqStanza,
LoginRequest,
LoginResponse,
SelectiveAck,
StreamErrorStanza,
)

_logger = logging.getLogger(__name__)

OnNotificationCallable = Callable[[dict[str, Any], str, Any], None]
CredentialsUpdatedCallable = Callable[[dict[str, Any]], None]

# MCS Message Types and Tags
MCS_MESSAGE_TAG = {
HeartbeatPing: 0,
HeartbeatAck: 1,
LoginRequest: 2,
LoginResponse: 3,
Close: 4,
"MessageStanza": 5,
"PresenceStanza": 6,
IqStanza: 7,
DataMessageStanza: 8,
"BatchPresenceStanza": 9,
StreamErrorStanza: 10,
"HttpRequest": 11,
"HttpResponse": 12,
"BindAccountRequest": 13,
"BindAccountResponse": 14,
"TalkMetadata": 15,
}


class ErrorType(Enum):
CONNECTION = 1
Expand Down
46 changes: 37 additions & 9 deletions firebase_messaging/fcmregister.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import secrets
import time
import uuid
import warnings
from base64 import b64encode, urlsafe_b64encode
from collections.abc import Iterator
from contextlib import contextmanager
from dataclasses import dataclass
from typing import Any, Callable

Expand All @@ -26,15 +29,40 @@
GCM_SERVER_KEY_B64,
SDK_VERSION,
)
from .proto.android_checkin_pb2 import (
DEVICE_CHROME_BROWSER,
AndroidCheckinProto,
ChromeBuildProto,
)
from .proto.checkin_pb2 import (
AndroidCheckinRequest,
AndroidCheckinResponse,
)


@contextmanager
def catch_protobuf_warnings() -> Iterator[None]:
"""Catch excessive protobuf warnings due to incorrect greater than check.
See https://github.com/protocolbuffers/protobuf/issues/18096
and https://protobuf.dev/support/cross-version-runtime-guarantee/#minor.
"""
match = (
r"^Protobuf gencode version \d+\.\d+\.\d+ is older than the runtime version"
r" \d+\.\d+\.\d+ at .*\.proto\. Please avoid checked-in Protobuf gencode "
r"that can be obsolete\.$"
)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
category=UserWarning,
module="google.protobuf.runtime_version",
message=match,
)
yield


with catch_protobuf_warnings():
from .proto.android_checkin_pb2 import (
DEVICE_CHROME_BROWSER,
AndroidCheckinProto,
ChromeBuildProto,
)
from .proto.checkin_pb2 import (
AndroidCheckinRequest,
AndroidCheckinResponse,
)

_logger = logging.getLogger(__name__)

Expand Down

0 comments on commit ea6a505

Please sign in to comment.