1+ from __future__ import annotations
2+
13import abc
24import asyncio
5+ import datetime
36import typing
7+ from typing import (
8+ Optional ,
9+ Any ,
10+ Iterator ,
11+ AsyncIterator ,
12+ Callable ,
13+ Iterable ,
14+ Union ,
15+ Coroutine ,
16+ )
417from dataclasses import dataclass
518
619import grpc
720from google .protobuf .message import Message
21+ from google .protobuf .duration_pb2 import Duration as ProtoDuration
22+ from google .protobuf .timestamp_pb2 import Timestamp as ProtoTimeStamp
823
924import ydb .aio
1025
2136class IFromProto (abc .ABC ):
2237 @staticmethod
2338 @abc .abstractmethod
24- def from_proto (msg : Message ) -> typing .Any :
25- pass
39+ def from_proto (msg : Message ) -> Any :
40+ ...
41+
42+
43+ class IFromProtoWithProtoType (IFromProto ):
44+ @staticmethod
45+ @abc .abstractmethod
46+ def empty_proto_message () -> Message :
47+ ...
2648
2749
2850class IToProto (abc .ABC ):
2951 @abc .abstractmethod
3052 def to_proto (self ) -> Message :
31- pass
53+ ...
54+
55+
56+ class IFromPublic (abc .ABC ):
57+
58+ @staticmethod
59+ @abc .abstractmethod
60+ def from_public (o : typing .Any ) -> typing .Any :
61+ ...
62+
63+
64+ class IToPublic (abc .ABC ):
65+ @abc .abstractmethod
66+ def to_public (self ) -> typing .Any :
67+ ...
3268
3369
3470class UnknownGrpcMessageError (issues .Error ):
@@ -76,7 +112,7 @@ def __next__(self):
76112
77113
78114class SyncIteratorToAsyncIterator :
79- def __init__ (self , sync_iterator : typing . Iterator ):
115+ def __init__ (self , sync_iterator : Iterator ):
80116 self ._sync_iterator = sync_iterator
81117
82118 def __aiter__ (self ):
@@ -92,21 +128,21 @@ async def __anext__(self):
92128
93129class IGrpcWrapperAsyncIO (abc .ABC ):
94130 @abc .abstractmethod
95- async def receive (self ) -> typing . Any :
131+ async def receive (self ) -> Any :
96132 ...
97133
98134 @abc .abstractmethod
99135 def write (self , wrap_message : IToProto ):
100136 ...
101137
102138
103- SupportedDriverType = typing . Union [ydb .Driver , ydb .aio .Driver ]
139+ SupportedDriverType = Union [ydb .Driver , ydb .aio .Driver ]
104140
105141
106142class GrpcWrapperAsyncIO (IGrpcWrapperAsyncIO ):
107143 from_client_grpc : asyncio .Queue
108- from_server_grpc : typing . AsyncIterator
109- convert_server_grpc_to_wrapper : typing . Callable [[typing . Any ], typing . Any ]
144+ from_server_grpc : AsyncIterator
145+ convert_server_grpc_to_wrapper : Callable [[Any ], Any ]
110146 _connection_state : str
111147
112148 def __init__ (self , convert_server_grpc_to_wrapper ):
@@ -140,7 +176,7 @@ async def _start_sync_driver(self, driver: ydb.Driver, stub, method):
140176 )
141177 self .from_server_grpc = SyncIteratorToAsyncIterator (stream_call .__iter__ ())
142178
143- async def receive (self ) -> typing . Any :
179+ async def receive (self ) -> Any :
144180 # todo handle grpc exceptions and convert it to internal exceptions
145181 try :
146182 grpc_message = await self .from_server_grpc .__anext__ ()
@@ -168,7 +204,7 @@ class ServerStatus(IFromProto):
168204 def __init__ (
169205 self ,
170206 status : issues .StatusCode ,
171- issues : typing . Iterable [typing . Any ],
207+ issues : Iterable [Any ],
172208 ):
173209 self .status = status
174210 self .issues = issues
@@ -178,7 +214,7 @@ def __str__(self):
178214
179215 @staticmethod
180216 def from_proto (
181- msg : typing . Union [
217+ msg : Union [
182218 ydb_topic_pb2 .StreamReadMessage .FromServer ,
183219 ydb_topic_pb2 .StreamWriteMessage .FromServer ,
184220 ]
@@ -198,11 +234,37 @@ def issue_to_str(cls, issue: ydb_issue_message_pb2.IssueMessage):
198234
199235
200236def callback_from_asyncio (
201- callback : typing . Union [typing . Callable , typing . Coroutine ]
237+ callback : Union [Callable , Coroutine ]
202238) -> [asyncio .Future , asyncio .Task ]:
203239 loop = asyncio .get_running_loop ()
204240
205241 if asyncio .iscoroutinefunction (callback ):
206242 return loop .create_task (callback ())
207243 else :
208244 return loop .run_in_executor (None , callback )
245+
246+
247+ def proto_duration_from_timedelta (t : Optional [datetime .timedelta ]) -> ProtoDuration :
248+ if t is None :
249+ return None
250+ res = ProtoDuration ()
251+ res .FromTimedelta (t )
252+
253+
254+ def proto_timestamp_from_datetime (t : Optional [datetime .datetime ]) -> ProtoTimeStamp :
255+ if t is None :
256+ return None
257+
258+ res = ProtoTimeStamp ()
259+ res .FromDatetime (t )
260+
261+
262+ def datetime_from_proto_timestamp (ts : Optional [ProtoTimeStamp ]) -> Optional [datetime .datetime ]:
263+ if ts is None :
264+ return None
265+ return ts .ToDatetime ()
266+
267+ def timedelta_from_proto_duration (d : Optional [ProtoDuration ]) -> Optional [datetime .timedelta ]:
268+ if d is None :
269+ return None
270+ return d .ToTimedelta ()
0 commit comments