From ea6a505f836d75f5da6fe506f20210ef29ec14fc Mon Sep 17 00:00:00 2001 From: "Steven B." <51370195+sdb9696@users.noreply.github.com> Date: Wed, 25 Sep 2024 16:44:15 +0100 Subject: [PATCH] Catch excessive protobuf warnings (#16) --- firebase_messaging/const.py | 31 ------------------- firebase_messaging/fcmpushclient.py | 46 +++++++++++++++++++++-------- firebase_messaging/fcmregister.py | 46 +++++++++++++++++++++++------ 3 files changed, 71 insertions(+), 52 deletions(-) diff --git a/firebase_messaging/const.py b/firebase_messaging/const.py index 939ba49..5a562f3 100644 --- a/firebase_messaging/const.py +++ b/firebase_messaging/const.py @@ -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 = ( @@ -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, -} diff --git a/firebase_messaging/fcmpushclient.py b/firebase_messaging/fcmpushclient.py index 9cfc58c..6df40f5 100644 --- a/firebase_messaging/fcmpushclient.py +++ b/firebase_messaging/fcmpushclient.py @@ -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 diff --git a/firebase_messaging/fcmregister.py b/firebase_messaging/fcmregister.py index 22f174b..b7daede 100644 --- a/firebase_messaging/fcmregister.py +++ b/firebase_messaging/fcmregister.py @@ -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 @@ -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__)