From 1c0c81db3f4d7d6f5859b82ee1002e59ae5c33af Mon Sep 17 00:00:00 2001 From: krypton-byte Date: Sun, 27 Oct 2024 10:39:07 +0700 Subject: [PATCH] chore: staticmethod --- examples/multisession_async.py | 1 + goneonize/__main__.py | 5 +++-- neonize/_binder.py | 5 +---- neonize/aioze/client.py | 30 ++++++++++++++++++------------ neonize/aioze/events.py | 5 ++++- neonize/client.py | 14 +++++++++----- neonize/events.py | 1 + neonize/exc.py | 2 +- neonize/utils/enum.py | 3 ++- 9 files changed, 40 insertions(+), 26 deletions(-) diff --git a/examples/multisession_async.py b/examples/multisession_async.py index 32004a6..2a6e3c7 100644 --- a/examples/multisession_async.py +++ b/examples/multisession_async.py @@ -312,6 +312,7 @@ async def handler(client: NewAClient, message: MessageEv): ), ) + @client_factory.event(PairStatusEv) async def PairStatusMessage(_: NewAClient, message: PairStatusEv): log.info(f"logged as {message.ID.User}") diff --git a/goneonize/__main__.py b/goneonize/__main__.py index 5df5c2a..5861fa4 100644 --- a/goneonize/__main__.py +++ b/goneonize/__main__.py @@ -161,5 +161,6 @@ def build_android(): # shell=os.name == "nt", # ) -if __name__ == '__main__': - build() \ No newline at end of file + +if __name__ == "__main__": + build() diff --git a/neonize/_binder.py b/neonize/_binder.py index 550afd6..e93702d 100644 --- a/neonize/_binder.py +++ b/neonize/_binder.py @@ -466,10 +466,7 @@ def get_bytes(self): ctypes.c_int, ] gocode.SendFBMessage.restype = Bytes - gocode.SendPresence.argtypes = [ - ctypes.c_char_p, - ctypes.c_char_p - ] + gocode.SendPresence.argtypes = [ctypes.c_char_p, ctypes.c_char_p] gocode.SendPresence.restype = ctypes.c_char_p else: gocode: Any = object() diff --git a/neonize/aioze/client.py b/neonize/aioze/client.py index 6913282..c190509 100644 --- a/neonize/aioze/client.py +++ b/neonize/aioze/client.py @@ -182,8 +182,9 @@ class GoCode: + @staticmethod def execute_sync_function( - self, func: Callable[SyncFunctionParams, ReturnType] + func: Callable[SyncFunctionParams, ReturnType], ) -> Callable[SyncFunctionParams, Awaitable[ReturnType]]: def call( *args: SyncFunctionParams.args, **kwargs: SyncFunctionParams.kwargs @@ -2672,13 +2673,12 @@ async def send_fb_message( len(extra_buff), ) return SendResponse.FromString(response.get_bytes()) + async def send_presence(self, presence: Presence): - response = await self.__client.SendPresence( - self.uuid, - presence.value - ) - if response: - raise SendPresenceError(response) + response = await self.__client.SendPresence(self.uuid, presence.value) + if response: + raise SendPresenceError(response) + async def connect(self): """Establishes a connection to the WhatsApp servers.""" # Convert the list of functions to a bytearray @@ -2723,8 +2723,6 @@ def disconnect(self) -> None: self.__client.Disconnect(self.uuid) - - class ClientFactory: def __init__(self, database_name: str = "neonize.db") -> None: """ @@ -2753,7 +2751,12 @@ def get_all_devices_from_db(db: str) -> List[Device]: id, server = id.split("@") jid = build_jid(id, server) - device = Device(JID=jid, PushName=push_name, BussinessName=bussniess_name, Initialized=initialized == "true") + device = Device( + JID=jid, + PushName=push_name, + BussinessName=bussniess_name, + Initialized=initialized == "true", + ) devices.append(device) return devices @@ -2763,7 +2766,10 @@ def get_all_devices(self) -> List["Device"]: return self.get_all_devices_from_db(self.database_name) def new_client( - self, jid: Optional[JID] = None, uuid: Optional[str] = None, props: Optional[DeviceProps] = None + self, + jid: Optional[JID] = None, + uuid: Optional[str] = None, + props: Optional[DeviceProps] = None, ) -> NewAClient: """ This function creates a new instance of the client. If the jid parameter is not provided, a new client will be created. @@ -2787,4 +2793,4 @@ def new_client( return client async def run(self): - return await asyncio.gather(*[client.connect() for client in self.clients]) \ No newline at end of file + return await asyncio.gather(*[client.connect() for client in self.clients]) diff --git a/neonize/aioze/events.py b/neonize/aioze/events.py index 122422c..4799234 100644 --- a/neonize/aioze/events.py +++ b/neonize/aioze/events.py @@ -149,6 +149,7 @@ def callback(func: Callable[[NewAClient, EventType], Awaitable[None]]) -> None: return callback + class EventsManager: def __init__(self, client_factory: ClientFactory): self.client_factory = client_factory @@ -165,6 +166,8 @@ def __call__( :return: A decorator that registers the callback function. :rtype: Callable[[Callable[[NewClient, EventType], None]], None] """ + def callback(func: Callable[[NewAClient, EventType], Awaitable[None]]) -> None: self.list_func.update({EVENT_TO_INT[event]: func}) - return callback \ No newline at end of file + + return callback diff --git a/neonize/client.py b/neonize/client.py index 44745c7..acb7016 100644 --- a/neonize/client.py +++ b/neonize/client.py @@ -2522,13 +2522,12 @@ def send_fb_message( len(extra_buff), ) return SendResponse.FromString(response.get_bytes()) + def send_presence(self, presence: Presence): - response = self.__client.SendPresence( - self.uuid, - presence.value - ) + response = self.__client.SendPresence(self.uuid, presence.value) if response: raise SendPresenceError(response) + def connect(self): """Establishes a connection to the WhatsApp servers.""" # Convert the list of functions to a bytearray @@ -2601,7 +2600,12 @@ def get_all_devices_from_db(db: str) -> List[Device]: id, server = id.split("@") jid = build_jid(id, server) - device = Device(JID=jid, PushName=push_name, BussinessName=bussniess_name, Initialized=initialized == "true") + device = Device( + JID=jid, + PushName=push_name, + BussinessName=bussniess_name, + Initialized=initialized == "true", + ) devices.append(device) return devices diff --git a/neonize/events.py b/neonize/events.py index 4440efd..21e1505 100644 --- a/neonize/events.py +++ b/neonize/events.py @@ -105,6 +105,7 @@ class EventsManager: def __init__(self, client_factory: ClientFactory): self.client_factory = client_factory self.list_func: Dict[int, Callable[[NewClient, Message], None]] = {} + def __call__( self, event: Type[EventType] ) -> Callable[[Callable[[NewClient, EventType], None]], None]: diff --git a/neonize/exc.py b/neonize/exc.py index b47100c..3409fb0 100644 --- a/neonize/exc.py +++ b/neonize/exc.py @@ -240,4 +240,4 @@ class GetChatSettingsError(Exception): class SendPresenceError(Exception): - pass \ No newline at end of file + pass diff --git a/neonize/utils/enum.py b/neonize/utils/enum.py index 45531ab..c287c04 100644 --- a/neonize/utils/enum.py +++ b/neonize/utils/enum.py @@ -348,6 +348,7 @@ class ParticipantRequestChange(Enum): APPROVE = "approve" REJECT = "reject" + class Presence(Enum): AVAILABLE = b"available" - UNAVAILABLE = b"unavailable" \ No newline at end of file + UNAVAILABLE = b"unavailable"