Skip to content

Commit

Permalink
Merge be14d1c into 7615f93
Browse files Browse the repository at this point in the history
  • Loading branch information
spyoungtech committed Aug 25, 2023
2 parents 7615f93 + be14d1c commit d92ba5e
Show file tree
Hide file tree
Showing 15 changed files with 947 additions and 524 deletions.
44 changes: 43 additions & 1 deletion ahk/_async/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import tempfile
import time
import warnings
from functools import partial
from typing import Any
from typing import Awaitable
from typing import Callable
Expand Down Expand Up @@ -34,6 +35,7 @@
else:
from typing import TypeAlias

from ..extensions import Extension, _extension_method_registry, _ExtensionMethodRegistry
from ..keys import Key
from .transport import AsyncDaemonProcessTransport
from .transport import AsyncFutureResult
Expand Down Expand Up @@ -135,13 +137,47 @@ def __init__(
TransportClass: Optional[Type[AsyncTransport]] = None,
directives: Optional[list[Directive | Type[Directive]]] = None,
executable_path: str = '',
extensions: list[Extension] | None | Literal['auto'] = None,
):
self._extension_registry: _ExtensionMethodRegistry
self._extensions: list[Extension]
if extensions == 'auto':
is_async = False
is_async = True # unasync: remove
if is_async:
methods = _extension_method_registry.async_methods
else:
methods = _extension_method_registry.sync_methods
extensions = list(set(entry.extension for name, entry in methods.items()))

self._extension_registry = _extension_method_registry
self._extensions = extensions
else:
self._extensions = extensions or []
self._extension_registry = _ExtensionMethodRegistry(sync_methods={}, async_methods={})
for ext in self._extensions:
self._extension_registry.merge(ext._extension_method_registry)

if TransportClass is None:
TransportClass = AsyncDaemonProcessTransport
assert TransportClass is not None
transport = TransportClass(executable_path=executable_path, directives=directives)
transport = TransportClass(executable_path=executable_path, directives=directives, extensions=extensions)
self._transport: AsyncTransport = transport

def __getattr__(self, name: str) -> Callable[..., Any]:
is_async = False
is_async = True # unasync: remove
if is_async:
if name in self._extension_registry.async_methods:
method = self._extension_registry.async_methods[name].method
return partial(method, self)
else:
if name in self._extension_registry.sync_methods:
method = self._extension_registry.sync_methods[name].method
return partial(method, self)

raise AttributeError(f'{self.__class__.__name__!r} object has no attribute {name!r}')

def add_hotkey(
self, keyname: str, callback: Callable[[], Any], ex_handler: Optional[Callable[[str, Exception], Any]] = None
) -> None:
Expand All @@ -168,6 +204,12 @@ def add_hotkey(
warnings.warn(warning.message, warning.category, stacklevel=2)
return None

async def function_call(self, function_name: str, args: list[str], blocking: bool = True) -> Any:
"""
Call an AHK function defined in the daemon script. This method is intended for use by extension authors.
"""
return await self._transport.function_call(function_name, args, blocking=blocking, engine=self) # type: ignore[call-overload]

def add_hotstring(
self,
trigger: str,
Expand Down
11 changes: 10 additions & 1 deletion ahk/_async/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import jinja2

from ahk.extensions import Extension
from ahk._hotkey import ThreadedHotkeyTransport, Hotkey, Hotstring
from ahk.message import RequestMessage
from ahk.message import ResponseMessage
Expand Down Expand Up @@ -656,7 +657,9 @@ def __init__(
directives: Optional[list[Directive | Type[Directive]]] = None,
jinja_loader: Optional[jinja2.BaseLoader] = None,
template: Optional[jinja2.Template] = None,
extensions: list[Extension] | None = None,
):
self._extensions = extensions or []
self._proc: Optional[AsyncAHKProcess]
self._proc = None
self._temp_script: Optional[str] = None
Expand Down Expand Up @@ -711,7 +714,13 @@ def _render_script(self, template: Optional[jinja2.Template] = None, **kwargs: A
template = self._template
kwargs['daemon'] = self.__template
message_types = {str(tom, 'utf-8'): c.__name__.upper() for tom, c in _message_registry.items()}
return template.render(directives=self._directives, message_types=message_types, **kwargs)
return template.render(
directives=self._directives,
message_types=message_types,
message_registry=_message_registry,
extensions=self._extensions,
**kwargs,
)

@property
def lock(self) -> Any:
Expand Down
Loading

0 comments on commit d92ba5e

Please sign in to comment.