Skip to content

Commit

Permalink
invoke_method() refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
FeodorFitsner committed Feb 25, 2024
1 parent 9a1e37c commit 08b9ab0
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 119 deletions.
46 changes: 23 additions & 23 deletions sdk/python/packages/flet-core/src/flet_core/audio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from enum import Enum
from typing import Any, Optional
from warnings import warn

from flet_core.control import Control, OptionalNumber
from flet_core.ref import Ref
Expand Down Expand Up @@ -80,43 +81,43 @@ def _get_control_name(self):
return "audio"

def play(self):
self.page.invoke_method("play", control_id=self.uid)
self.invoke_method("play")

async def play_async(self):
await self.page.invoke_method_async("play", control_id=self.uid)
warn("Obsolete. Use play() method instead.")
self.play()

def pause(self):
self.page.invoke_method("pause", control_id=self.uid)
self.invoke_method("pause")

async def pause_async(self):
await self.page.invoke_method_async("pause", control_id=self.uid)
warn("Obsolete. Use pause() method instead.")
self.pause()

def resume(self):
self.page.invoke_method("resume", control_id=self.uid)
self.invoke_method("resume")

async def resume_async(self):
await self.page.invoke_method_async("resume", control_id=self.uid)
warn("Obsolete. Use resume() method instead.")
self.resume()

def release(self):
self.page.invoke_method("release", control_id=self.uid)
self.invoke_method("release")

async def release_async(self):
await self.page.invoke_method_async("release", control_id=self.uid)
warn("Obsolete. Use release() method instead.")
self.release()

def seek(self, position_milliseconds: int):
self.page.invoke_method(
"seek", {"position": str(position_milliseconds)}, control_id=self.uid
)
self.invoke_method("seek", {"position": str(position_milliseconds)})

async def seek_async(self, position_milliseconds: int):
await self.page.invoke_method_async(
"seek", {"position": str(position_milliseconds)}, control_id=self.uid
)
warn("Obsolete. Use seek() method instead.")
self.seek(position_milliseconds)

def get_duration(self, wait_timeout: Optional[float] = 5) -> Optional[int]:
sr = self.page.invoke_method(
sr = self.invoke_method(
"get_duration",
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
Expand All @@ -125,18 +126,16 @@ def get_duration(self, wait_timeout: Optional[float] = 5) -> Optional[int]:
async def get_duration_async(
self, wait_timeout: Optional[float] = 5
) -> Optional[int]:
sr = await self.page.invoke_method_async(
sr = await self.invoke_method_async(
"get_duration",
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
return int(sr) if sr else None

def get_current_position(self, wait_timeout: Optional[float] = 5) -> Optional[int]:
sr = self.page.invoke_method(
sr = self.invoke_method(
"get_current_position",
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
Expand All @@ -145,9 +144,8 @@ def get_current_position(self, wait_timeout: Optional[float] = 5) -> Optional[in
async def get_current_position_async(
self, wait_timeout: Optional[float] = 5
) -> Optional[int]:
sr = await self.page.invoke_method_async(
sr = await self.invoke_method_async(
"get_current_position",
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
Expand Down Expand Up @@ -217,7 +215,9 @@ def release_mode(self):

@release_mode.setter
def release_mode(self, value: Optional[ReleaseMode]):
self._set_attr("releaseMode", value.value if isinstance(value, ReleaseMode) else value)
self._set_attr(
"releaseMode", value.value if isinstance(value, ReleaseMode) else value
)

# on_loaded
@property
Expand Down
70 changes: 28 additions & 42 deletions sdk/python/packages/flet-core/src/flet_core/audio_recorder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from enum import Enum
from typing import Any, Optional
from warnings import warn

from flet_core.control import Control, OptionalNumber
from flet_core.ref import Ref
Expand Down Expand Up @@ -63,39 +64,31 @@ def _get_control_name(self):
def start_recording(self, output_path: str = None):
if not self.page.web and not output_path:
raise ValueError("output_path must be provided when not on web!")
self.page.invoke_method(
"start_recording", {"outputPath": output_path}, control_id=self.uid
)
self.invoke_method("start_recording", {"outputPath": output_path})

async def start_recording_async(self, output_path: str):
if not self.page.web and not output_path:
raise ValueError("output_path must be provided when not on web!")
await self.page.invoke_method_async(
"start_recording", {"outputPath": output_path}, control_id=self.uid
)
warn("Obsolete. Use start_recording() method instead.")
self.start_recording(output_path)

def is_recording(self, wait_timeout: Optional[float] = 5) -> bool:
recording = self.page.invoke_method(
recording = self.invoke_method(
"is_recording",
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
return recording == "true"

async def is_recording_async(self, wait_timeout: Optional[float] = 5) -> bool:
recording = await self.page.invoke_method_async(
recording = await self.invoke_method_async(
"is_recording",
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
return recording == "true"

def stop_recording(self, wait_timeout: Optional[float] = 5) -> Optional[str]:
out = self.page.invoke_method(
out = self.invoke_method(
"stop_recording",
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
Expand All @@ -104,39 +97,38 @@ def stop_recording(self, wait_timeout: Optional[float] = 5) -> Optional[str]:
async def stop_recording_async(
self, wait_timeout: Optional[float] = 10
) -> Optional[str]:
out = await self.page.invoke_method_async(
out = await self.invoke_method_async(
"stop_recording",
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
return out if out != "null" else None

def resume_recording(self):
self.page.invoke_method("resume_recording", control_id=self.uid)
self.invoke_method("resume_recording")

async def resume_recording_async(self):
await self.page.invoke_method_async("resume_recording", control_id=self.uid)
warn("Obsolete. Use resume_recording() method instead.")
self.resume_recording()

def pause_recording(self):
self.page.invoke_method("pause_recording", control_id=self.uid)
self.invoke_method("pause_recording")

async def pause_recording_async(self):
await self.page.invoke_method_async("pause_recording", control_id=self.uid)
warn("Obsolete. Use pause_recording() method instead.")
self.pause_recording()

def is_paused(self, wait_timeout: Optional[float] = 5) -> bool:
paused = self.page.invoke_method(
paused = self.invoke_method(
"is_paused",
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
return paused == "true"

async def is_paused_async(self, wait_timeout: Optional[float] = 5) -> bool:
supported = await self.page.invoke_method_async(
supported = await self.invoke_method_async(
"is_paused",
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
Expand All @@ -145,14 +137,13 @@ async def is_paused_async(self, wait_timeout: Optional[float] = 5) -> bool:
def is_supported_encoder(
self, encoder: AudioEncoder, wait_timeout: Optional[float] = 5
) -> bool:
supported = self.page.invoke_method(
supported = self.invoke_method(
"is_supported_encoder",
{
"encoder": encoder.value
if isinstance(encoder, AudioEncoder)
else encoder
"encoder": (
encoder.value if isinstance(encoder, AudioEncoder) else encoder
)
},
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
Expand All @@ -161,50 +152,45 @@ def is_supported_encoder(
async def is_supported_encoder_async(
self, encoder: AudioEncoder, wait_timeout: Optional[float] = 5
) -> bool:
supported = await self.page.invoke_method_async(
supported = await self.invoke_method_async(
"is_supported_encoder",
{
"encoder": encoder.value
if isinstance(encoder, AudioEncoder)
else encoder
"encoder": (
encoder.value if isinstance(encoder, AudioEncoder) else encoder
)
},
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
return supported == "true"

def get_input_devices(self, wait_timeout: Optional[float] = 5) -> dict:
devices = self.page.invoke_method(
devices = self.invoke_method(
"get_input_devices",
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
return json.loads(devices)

async def get_input_devices_async(self, wait_timeout: Optional[float] = 5) -> bool:
devices = await self.page.invoke_method_async(
devices = await self.invoke_method_async(
"get_input_devices",
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
return json.loads(devices)

def has_permission(self, wait_timeout: Optional[float] = 10) -> bool:
p = self.page.invoke_method(
p = self.invoke_method(
"has_permission",
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
return p == "true"

async def has_permission_async(self, wait_timeout: Optional[float] = 10) -> bool:
p = await self.page.invoke_method_async(
p = await self.invoke_method_async(
"has_permission",
control_id=self.uid,
wait_for_result=True,
wait_timeout=wait_timeout,
)
Expand Down
16 changes: 6 additions & 10 deletions sdk/python/packages/flet-core/src/flet_core/clipboard.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dataclasses
from typing import Any, Optional
from warnings import warn

from flet_core.control import Control
from flet_core.ref import Ref
Expand Down Expand Up @@ -30,19 +31,14 @@ def is_isolated(self):
return True

def set_data(self, data: str):
self.page.invoke_method("set_data", {"data": data}, control_id=self.uid)
self.invoke_method("set_data", {"data": data})

async def set_data_async(self, data: str):
await self.page.invoke_method_async(
"set_data", {"data": data}, control_id=self.uid
)
warn("Obsolete. Use set_data() method instead.")
self.set_data(data)

def get_data(self) -> str:
return self.page.invoke_method(
"get_data", control_id=self.uid, wait_for_result=True
)
return self.invoke_method("get_data", wait_for_result=True)

async def get_data_async(self) -> str:
return await self.page.invoke_method_async(
"get_data", control_id=self.uid, wait_for_result=True
)
return await self.invoke_method_async("get_data", wait_for_result=True)
32 changes: 32 additions & 0 deletions sdk/python/packages/flet-core/src/flet_core/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,38 @@ async def clean_async(self):
assert self.__page, "Control must be added to the page first."
await self.__page._clean_async(self)

def invoke_method(
self,
method_name: str,
arguments: Optional[Dict[str, str]] = None,
wait_for_result: bool = False,
wait_timeout: Optional[float] = 5,
) -> Optional[str]:
assert self.__page, "Control must be added to the page first."
self.__page._invoke_method(
control_id=self.uid,
method_name=method_name,
arguments=arguments,
wait_for_result=wait_for_result,
wait_timeout=wait_timeout,
)

def invoke_method_async(
self,
method_name: str,
arguments: Optional[Dict[str, str]] = None,
wait_for_result: bool = False,
wait_timeout: Optional[float] = 5,
):
assert self.__page, "Control must be added to the page first."
return self.__page._invoke_method_async(
control_id=self.uid,
method_name=method_name,
arguments=arguments,
wait_for_result=wait_for_result,
wait_timeout=wait_timeout,
)

def copy_attrs(self, dest: Dict[str, Any]):
for attrName in sorted(self.__attrs):
attrName = attrName.lower()
Expand Down
6 changes: 3 additions & 3 deletions sdk/python/packages/flet-core/src/flet_core/dismissible.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from typing import Any, Dict, Optional, Union
from warnings import warn

from flet_core.adaptive_control import AdaptiveControl
from flet_core.constrained_control import ConstrainedControl
Expand Down Expand Up @@ -166,9 +167,8 @@ def confirm_dismiss(self, dismiss: bool):
)

async def confirm_dismiss_async(self, dismiss: bool):
await self.page.invoke_method_async(
"confirm_dismiss", {"dismiss": str(dismiss).lower()}, control_id=self.uid
)
warn("Obsolete. Use confirm_dismiss() method instead.")
self.confirm_dismiss(dismiss)

# content
@property
Expand Down
Loading

0 comments on commit 08b9ab0

Please sign in to comment.