Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(python): restore missing internal lib #40

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions python/lib/sift_internal/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""
INTERNAL MODULE

Everything inside of this module is not meant to be used publicly. Proceed at your own risk.
"""
3 changes: 3 additions & 0 deletions python/lib/sift_internal/convert/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Utilities for type-conversion and type-casting.
"""
29 changes: 29 additions & 0 deletions python/lib/sift_internal/convert/protobuf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from abc import ABC, abstractmethod
from google.protobuf.message import Message
from typing import cast, Optional, Type, TypeVar

ProtobufMessage = Message

class AsProtobuf(ABC):
"""
Abstract base class used to create create sub-types that can be treated
as an object that can be converted into an instance of `ProtobufMessage`.
"""
@abstractmethod
def as_pb(self, klass: Type[ProtobufMessage]) -> Optional[ProtobufMessage]:
"""
Performs the conversion into a sub-type of `ProtobufMessage`. Should return `None`
if conversion fails.
"""
pass


T = TypeVar("T", bound=ProtobufMessage)
def try_cast_pb(value: AsProtobuf, target_klass: Type[T]) -> T:
"""
Tries to cast the `value` to `target_klass`, otherwise, returns a `TypeError`.
"""
value_pb = value.as_pb(target_klass)
if isinstance(value_pb, target_klass):
return cast(target_klass, value_pb)
raise TypeError(f"Expected a '{target_klass.__module__}{target_klass.__name__}' but got {value.__module__}{value.__class__.__name__}")
3 changes: 3 additions & 0 deletions python/lib/sift_py/ingestion/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Module containing components necessary to leverage Sift's data ingestion API.
"""
12 changes: 6 additions & 6 deletions python/lib/sift_py/ingestion/channel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations
from google.protobuf.empty_pb2 import Empty
from sift_internal.convert.protobuf import AsProtobuf, ProtobufMessage, try_convert_pb
from sift_internal.convert.protobuf import AsProtobuf, ProtobufMessage, try_cast_pb
from enum import Enum
from sift.common.type.v1.channel_enum_type_pb2 import ChannelEnumType as ChannelEnumTypePb
from sift.common.type.v1.channel_bit_field_element_pb2 import (
Expand Down Expand Up @@ -56,16 +56,16 @@ def __init__(
self.bit_field_elements = bit_field_elements
self.enum_types = enum_types

def as_pb(self, klass: Type[ProtobufMessage]) -> ProtobufMessage:
def as_pb(self, klass: Type[ProtobufMessage]) -> Optional[ProtobufMessage]:
return ChannelConfigPb(
name=self.name,
component=self.component or "",
unit=self.unit or "",
description=self.description or "",
data_type=self.data_type.value,
enum_types=[try_convert_pb(etype, ChannelEnumTypePb) for etype in self.enum_types],
enum_types=[try_cast_pb(etype, ChannelEnumTypePb) for etype in self.enum_types],
bit_field_elements=[
try_convert_pb(el, ChannelBitFieldElementPb) for el in self.bit_field_elements
try_cast_pb(el, ChannelBitFieldElementPb) for el in self.bit_field_elements
],
)

Expand All @@ -80,7 +80,7 @@ def __init__(self, name: str, index: int, bit_count: int):
self.index = index
self.bit_count = bit_count

def as_pb(self, klass: Type[ProtobufMessage]) -> ProtobufMessage:
def as_pb(self, klass: Type[ProtobufMessage]) -> Optional[ProtobufMessage]:
return ChannelBitFieldElementPb(
name=self.name,
index=self.index,
Expand All @@ -96,7 +96,7 @@ def __init__(self, name: str, key: int):
self.name = name
self.key = key

def as_pb(self, klass: Type[ProtobufMessage]) -> ProtobufMessage:
def as_pb(self, klass: Type[ProtobufMessage]) -> Optional[ProtobufMessage]:
return ChannelEnumTypePb(name=self.name, key=self.key)


Expand Down
6 changes: 3 additions & 3 deletions python/lib/sift_py/ingestion/flow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations
from .channel import ChannelConfig
from sift_internal.convert.protobuf import try_convert_pb, AsProtobuf, ProtobufMessage
from sift_internal.convert.protobuf import try_cast_pb, AsProtobuf, ProtobufMessage
from sift.ingestion_configs.v1.ingestion_configs_pb2 import (
ChannelConfig as ChannelConfigPb,
FlowConfig as FlowConfigPb,
Expand Down Expand Up @@ -40,10 +40,10 @@ def get_channel(self, name: str, component: Optional[str] = "") -> Optional[Chan
except IndexError:
return None

def as_pb(self, klass: Type[ProtobufMessage]) -> ProtobufMessage:
def as_pb(self, klass: Type[ProtobufMessage]) -> Optional[ProtobufMessage]:
return FlowConfigPb(
name=self.name,
channels=[try_convert_pb(conf, ChannelConfigPb) for conf in self.channels],
channels=[try_cast_pb(conf, ChannelConfigPb) for conf in self.channels],
)

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from ...grpc.transport import SiftChannel
from ..flow import FlowConfig
from ...convert.protobuf import try_convert_pb
from sift_internal.convert.protobuf import try_cast_pb
from sift.ingestion_configs.v1.ingestion_configs_pb2 import (
IngestionConfig,
CreateIngestionConfigRequest,
Expand Down Expand Up @@ -56,7 +56,7 @@ def create_ingestion_config(
asset_name=asset_name,
client_key=client_key,
organization_id=organization_id or "",
flows=[try_convert_pb(flow, FlowConfigPb) for flow in flows],
flows=[try_cast_pb(flow, FlowConfigPb) for flow in flows],
)
res = cast(CreateIngestionConfigResponse, svc.CreateIngestionConfig(req))
return res.ingestion_config
2 changes: 2 additions & 0 deletions scripts/gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ OUTPUT_PROTOS="${TMP_DIR}/protos"
PYTHON_GEN_DIR="python/gen"
PYTHON_LIB_DIR="python/lib"
PYTHON_CLIENT_LIB="sift_py"
PYTHON_CLIENT_LIB_INTERNAL="sift_internal"

USAGE=$(cat<<EOT
Compile protocol buffers into supported target languages.
Expand Down Expand Up @@ -58,6 +59,7 @@ gen_python_modules() {
done

mv $PYTHON_LIB_DIR/$PYTHON_CLIENT_LIB $PYTHON_GEN_DIR
mv $PYTHON_LIB_DIR/$PYTHON_CLIENT_LIB_INTERNAL $PYTHON_GEN_DIR
rm -rf $PYTHON_LIB_DIR
mv $PYTHON_GEN_DIR $PYTHON_LIB_DIR

Expand Down
Loading