Skip to content

Commit

Permalink
move version resolution into the engine
Browse files Browse the repository at this point in the history
  • Loading branch information
spyoungtech committed Sep 21, 2023
1 parent becba02 commit f442c56
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 31 deletions.
28 changes: 27 additions & 1 deletion ahk/_async/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

from .._hotkey import Hotkey
from .._hotkey import Hotstring
from .._utils import _get_executable_major_version
from .._utils import _resolve_executable_path
from .._utils import MsgBoxButtons
from .._utils import MsgBoxDefaultButton
from .._utils import MsgBoxIcon
Expand Down Expand Up @@ -145,6 +147,27 @@ def __init__(
extensions: list[Extension] | None | Literal['auto'] = None,
version: Optional[Literal['v1', 'v2']] = None,
):
if version not in (None, 'v1', 'v2'):
raise ValueError(f'Invalid version ({version!r}). Must be one of None, "v1", or "v2"')
executable_path = _resolve_executable_path(executable_path=executable_path, version=version)
skip_version_check = False
if version is None:
try:
version = _get_executable_major_version(executable_path)
except Exception as e:
warnings.warn(
f'Could not detect AHK version ({e}). This is likely caused by a misconfigured AutoHotkey executable and will likely cause a fatal error later on.\nAssuming v1 for now.'
)
version = 'v1'
skip_version_check = True

if not skip_version_check:
detected_version = _get_executable_major_version(executable_path)
if version != detected_version:
raise RuntimeError(
f'AutoHotkey {version} was requested but AutoHotkey {detected_version} was detected for executable {executable_path}'
)
self._version: Literal['v1', 'v2'] = version
self._extension_registry: _ExtensionMethodRegistry
self._extensions: list[Extension]
if extensions == 'auto':
Expand All @@ -162,6 +185,9 @@ def __init__(
)
self._transport: AsyncTransport = transport

def __repr__(self) -> str:
return f'<{self.__module__}.{self.__class__.__qualname__} object version={self._version!r}>'

def __getattr__(self, name: str) -> Callable[..., Any]:
is_async = False
is_async = True # unasync: remove
Expand Down Expand Up @@ -1332,7 +1358,7 @@ async def show_traytip(
if second is None:
second = 1.0
else:
if self._transport._version == 'v2':
if self._version == 'v2':
warnings.warn(
'supplying seconds is not supported when using AutoHotkey v2. This parameter will be ignored'
)
Expand Down
18 changes: 2 additions & 16 deletions ahk/_async/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
DAEMON_SCRIPT_TEMPLATE as _DAEMON_SCRIPT_TEMPLATE,
DAEMON_SCRIPT_V2_TEMPLATE as _DAEMON_SCRIPT_V2_TEMPLATE,
)
from ahk._utils import _version_detection_script, _resolve_executable_path, _get_executable_major_version
from ahk._utils import _version_detection_script
from ahk.directives import Directive

from concurrent.futures import Future, ThreadPoolExecutor
Expand Down Expand Up @@ -676,14 +676,7 @@ def __init__(
self._jinja_env: jinja2.Environment
self._execution_lock = threading.Lock()
self._a_execution_lock = asyncio.Lock() # unasync: remove
self._executable_path = _resolve_executable_path(executable_path=executable_path, version=version)
if version is None:
try:
version = _get_executable_major_version(self._executable_path)
except Exception as e:
warnings.warn(f'Could not detect AHK version ({e}). Defaulting to v1')
version = 'v1'
skip_version_check = True
self._executable_path = executable_path

if version is None or version == 'v1':
template_name = 'daemon.ahk'
Expand All @@ -694,13 +687,6 @@ def __init__(
else:
raise ValueError(f'Invalid version {version!r} - must be one of "v1" or "v2"')

if not skip_version_check:
detected_version = _get_executable_major_version(self._executable_path)
if version != detected_version:
raise RuntimeError(
f'AutoHotkey {version} was requested but AutoHotkey {detected_version} was detected for executable {self._executable_path}'
)

if jinja_loader is None:
try:
loader: jinja2.BaseLoader
Expand Down
28 changes: 27 additions & 1 deletion ahk/_sync/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

from .._hotkey import Hotkey
from .._hotkey import Hotstring
from .._utils import _get_executable_major_version
from .._utils import _resolve_executable_path
from .._utils import MsgBoxButtons
from .._utils import MsgBoxDefaultButton
from .._utils import MsgBoxIcon
Expand Down Expand Up @@ -141,6 +143,27 @@ def __init__(
extensions: list[Extension] | None | Literal['auto'] = None,
version: Optional[Literal['v1', 'v2']] = None,
):
if version not in (None, 'v1', 'v2'):
raise ValueError(f'Invalid version ({version!r}). Must be one of None, "v1", or "v2"')
executable_path = _resolve_executable_path(executable_path=executable_path, version=version)
skip_version_check = False
if version is None:
try:
version = _get_executable_major_version(executable_path)
except Exception as e:
warnings.warn(
f'Could not detect AHK version ({e}). This is likely caused by a misconfigured AutoHotkey executable and will likely cause a fatal error later on.\nAssuming v1 for now.'
)
version = 'v1'
skip_version_check = True

if not skip_version_check:
detected_version = _get_executable_major_version(executable_path)
if version != detected_version:
raise RuntimeError(
f'AutoHotkey {version} was requested but AutoHotkey {detected_version} was detected for executable {executable_path}'
)
self._version: Literal['v1', 'v2'] = version
self._extension_registry: _ExtensionMethodRegistry
self._extensions: list[Extension]
if extensions == 'auto':
Expand All @@ -158,6 +181,9 @@ def __init__(
)
self._transport: Transport = transport

def __repr__(self) -> str:
return f'<{self.__module__}.{self.__class__.__qualname__} object version={self._version!r}>'

def __getattr__(self, name: str) -> Callable[..., Any]:
is_async = False
if is_async:
Expand Down Expand Up @@ -1320,7 +1346,7 @@ def show_traytip(
if second is None:
second = 1.0
else:
if self._transport._version == 'v2':
if self._version == 'v2':
warnings.warn(
'supplying seconds is not supported when using AutoHotkey v2. This parameter will be ignored'
)
Expand Down
16 changes: 3 additions & 13 deletions ahk/_sync/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
DAEMON_SCRIPT_TEMPLATE as _DAEMON_SCRIPT_TEMPLATE,
DAEMON_SCRIPT_V2_TEMPLATE as _DAEMON_SCRIPT_V2_TEMPLATE,
)
from ahk._utils import _version_detection_script, _resolve_executable_path, _get_executable_major_version
from ahk._utils import _version_detection_script
from ahk.directives import Directive

from concurrent.futures import Future, ThreadPoolExecutor
Expand Down Expand Up @@ -640,14 +640,7 @@ def __init__(
self.__template: jinja2.Template
self._jinja_env: jinja2.Environment
self._execution_lock = threading.Lock()
self._executable_path = _resolve_executable_path(executable_path=executable_path, version=version)
if version is None:
try:
version = _get_executable_major_version(self._executable_path)
except Exception as e:
warnings.warn(f'Could not detect AHK version ({e}). Defaulting to v1')
version = 'v1'
skip_version_check = True
self._executable_path = executable_path


if version is None or version == 'v1':
Expand All @@ -659,10 +652,7 @@ def __init__(
else:
raise ValueError(f'Invalid version {version!r} - must be one of "v1" or "v2"')

if not skip_version_check:
detected_version = _get_executable_major_version(self._executable_path)
if version != detected_version:
raise RuntimeError(f'AutoHotkey {version} was requested but AutoHotkey {detected_version} was detected for executable {self._executable_path}')


if jinja_loader is None:
try:
Expand Down
7 changes: 7 additions & 0 deletions tests/_async/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ async def test_type_escape(self):
text = await self.win.get_text()
assert '!' in text

async def test_send_input_manual_escapes(self):
await self.win.activate()
await self.ahk.send_input('Hello{Enter}World{!}')
time.sleep(0.4)
text = await self.win.get_text()
assert 'Hello\r\nWorld!' in text

async def test_send_literal_tilde_n(self):
expected_text = '```nim\nimport std/strformat\n```'
await self.win.send(expected_text, control='Edit1')
Expand Down
7 changes: 7 additions & 0 deletions tests/_sync/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ def test_type_escape(self):
text = self.win.get_text()
assert '!' in text

def test_send_input_manual_escapes(self):
self.win.activate()
self.ahk.send_input('Hello{Enter}World{!}')
time.sleep(0.4)
text = self.win.get_text()
assert 'Hello\r\nWorld!' in text

def test_send_literal_tilde_n(self):
expected_text = '```nim\nimport std/strformat\n```'
self.win.send(expected_text, control='Edit1')
Expand Down

0 comments on commit f442c56

Please sign in to comment.