github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/utils/proto_utils.py (about)

     1  #
     2  # Licensed to the Apache Software Foundation (ASF) under one or more
     3  # contributor license agreements.  See the NOTICE file distributed with
     4  # this work for additional information regarding copyright ownership.
     5  # The ASF licenses this file to You under the Apache License, Version 2.0
     6  # (the "License"); you may not use this file except in compliance with
     7  # the License.  You may obtain a copy of the License at
     8  #
     9  #    http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  #
    17  
    18  """For internal use only; no backwards-compatibility guarantees."""
    19  
    20  # pytype: skip-file
    21  
    22  from typing import Type
    23  from typing import TypeVar
    24  from typing import Union
    25  from typing import overload
    26  
    27  from google.protobuf import any_pb2
    28  from google.protobuf import duration_pb2
    29  from google.protobuf import message
    30  from google.protobuf import struct_pb2
    31  from google.protobuf import timestamp_pb2
    32  
    33  MessageT = TypeVar('MessageT', bound=message.Message)
    34  TimeMessageT = TypeVar(
    35      'TimeMessageT', duration_pb2.Duration, timestamp_pb2.Timestamp)
    36  
    37  message_types = (message.Message, )
    38  
    39  
    40  @overload
    41  def pack_Any(msg):
    42    # type: (message.Message) -> any_pb2.Any
    43    pass
    44  
    45  
    46  @overload
    47  def pack_Any(msg):
    48    # type: (None) -> None
    49    pass
    50  
    51  
    52  def pack_Any(msg):
    53    """Creates a protobuf Any with msg as its content.
    54  
    55    Returns None if msg is None.
    56    """
    57    if msg is None:
    58      return None
    59  
    60    result = any_pb2.Any()
    61    result.Pack(msg)
    62    return result
    63  
    64  
    65  @overload
    66  def unpack_Any(any_msg, msg_class):
    67    # type: (any_pb2.Any, Type[MessageT]) -> MessageT
    68    pass
    69  
    70  
    71  @overload
    72  def unpack_Any(any_msg, msg_class):
    73    # type: (any_pb2.Any, None) -> None
    74    pass
    75  
    76  
    77  def unpack_Any(any_msg, msg_class):
    78    """Unpacks any_msg into msg_class.
    79  
    80    Returns None if msg_class is None.
    81    """
    82    if msg_class is None:
    83      return None
    84    msg = msg_class()
    85    any_msg.Unpack(msg)
    86    return msg
    87  
    88  
    89  @overload
    90  def parse_Bytes(serialized_bytes, msg_class):
    91    # type: (bytes, Type[MessageT]) -> MessageT
    92    pass
    93  
    94  
    95  @overload
    96  def parse_Bytes(serialized_bytes, msg_class):
    97    # type: (bytes, Union[Type[bytes], None]) -> bytes
    98    pass
    99  
   100  
   101  def parse_Bytes(serialized_bytes, msg_class):
   102    """Parses the String of bytes into msg_class.
   103  
   104    Returns the input bytes if msg_class is None."""
   105    if msg_class is None or msg_class is bytes:
   106      return serialized_bytes
   107    msg = msg_class()
   108    msg.ParseFromString(serialized_bytes)
   109    return msg
   110  
   111  
   112  def pack_Struct(**kwargs):
   113    # type: (...) -> struct_pb2.Struct
   114  
   115    """Returns a struct containing the values indicated by kwargs.
   116    """
   117    msg = struct_pb2.Struct()
   118    for key, value in kwargs.items():
   119      msg[key] = value  # pylint: disable=unsubscriptable-object, unsupported-assignment-operation
   120    return msg
   121  
   122  
   123  def from_micros(cls, micros):
   124    # type: (Type[TimeMessageT], int) -> TimeMessageT
   125    result = cls()
   126    result.FromMicroseconds(micros)
   127    return result
   128  
   129  
   130  def to_Timestamp(time):
   131    # type: (Union[int, float]) -> timestamp_pb2.Timestamp
   132  
   133    """Convert a float returned by time.time() to a Timestamp.
   134    """
   135    seconds = int(time)
   136    nanos = int((time - seconds) * 10**9)
   137    return timestamp_pb2.Timestamp(seconds=seconds, nanos=nanos)
   138  
   139  
   140  def from_Timestamp(timestamp):
   141    # type: (timestamp_pb2.Timestamp) -> float
   142  
   143    """Convert a Timestamp to a float expressed as seconds since the epoch.
   144    """
   145    return timestamp.seconds + float(timestamp.nanos) / 10**9