From 0b04a699b7f1204c444459af74684b7ec38a7f56 Mon Sep 17 00:00:00 2001 From: Spencer Young Date: Fri, 10 Apr 2020 16:30:58 -0700 Subject: [PATCH 1/4] script engine now takes a `directives` argument --- ahk/script.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ahk/script.py b/ahk/script.py index 852e1c92..3759b9ef 100644 --- a/ahk/script.py +++ b/ahk/script.py @@ -65,7 +65,7 @@ def _resolve_executable_path(executable_path: str = ''): class ScriptEngine(object): - def __init__(self, executable_path: str = "", **kwargs): + def __init__(self, executable_path: str = "", directives=None, **kwargs): """ This class is typically not used directly. AHK components inherit from this class and the arguments for this class should usually be passed in to :py:class:`~ahk.AHK`. @@ -81,7 +81,7 @@ def __init__(self, executable_path: str = "", **kwargs): :raises ExecutableNotFound: if AHK executable cannot be found or the specified file does not exist """ self.executable_path = _resolve_executable_path(executable_path) - + self._directives = directives templates_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'templates') self.env = Environment(loader=FileSystemLoader(templates_path), autoescape=False, trim_blocks=True) @@ -91,7 +91,7 @@ def render_template(self, template_name, directives=None, blocking=True, **kwarg Renders a given jinja template and returns a string of script text :param template_name: the name of the jinja template to render - :param directives: additional AHK directives to add to the resulting script + :param directives: additional AHK directives to add to all scripts :param blocking: whether the template should be rendered to block (use #Persistent directive) :param kwargs: keywords passed to template rendering :return: An AutoHotkey script as a string @@ -111,6 +111,9 @@ def render_template(self, template_name, directives=None, blocking=True, **kwarg directives.add(Persistent) elif Persistent in directives: directives.remove(Persistent) + if self._directives: + for directive in self._directives: + directives.add(directive) kwargs['directives'] = directives template = self.env.get_template(template_name) From fbac689f47fbc2f8bc489284c438d2a4fa6b0858 Mon Sep 17 00:00:00 2001 From: Spencer Young Date: Fri, 10 Apr 2020 16:32:23 -0700 Subject: [PATCH 2/4] add console spy feature alpha --- ahk/console_spy.py | 16 ++++++++++++++++ setup.py | 5 ++++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 ahk/console_spy.py diff --git a/ahk/console_spy.py b/ahk/console_spy.py new file mode 100644 index 00000000..db9caaef --- /dev/null +++ b/ahk/console_spy.py @@ -0,0 +1,16 @@ +from ahk import AHK +from ahk.window import Window +from ahk.directives import NoTrayIcon + + +def _console_spy(*args, **kwargs): + ahk = AHK(directives={NoTrayIcon}) + + while True: + position = ahk.mouse_position + window = Window.from_mouse_position(engine=ahk) + color = ahk.pixel_get_color(*position) + print(f'Mouse Position: {position} Pixel Color: {color} Window Title: {window.title} ', end='\r') + +def _main(): + _console_spy() diff --git a/setup.py b/setup.py index 08e2d9f9..eff4d18d 100644 --- a/setup.py +++ b/setup.py @@ -30,5 +30,8 @@ ], tests_require=test_requirements, include_package_data=True, - zip_safe=False + zip_safe=False, + entry_points={ + 'console_scripts': ['console_spy = ahk.console_spy:_main'] + } ) From 54b7839c90d0e3d7d0627b7afad0deba0d645e11 Mon Sep 17 00:00:00 2001 From: Spencer Young Date: Fri, 10 Apr 2020 17:48:56 -0700 Subject: [PATCH 3/4] docstrings for directives --- ahk/script.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ahk/script.py b/ahk/script.py index 3759b9ef..d1e81951 100644 --- a/ahk/script.py +++ b/ahk/script.py @@ -78,6 +78,8 @@ def __init__(self, executable_path: str = "", directives=None, **kwargs): If environment variable not present, tries to look for 'AutoHotkey.exe' or 'AutoHotkeyA32.exe' with shutil.which + :param directives: additional AHK directives to add to all rendered scripts + :raises ExecutableNotFound: if AHK executable cannot be found or the specified file does not exist """ self.executable_path = _resolve_executable_path(executable_path) @@ -91,7 +93,7 @@ def render_template(self, template_name, directives=None, blocking=True, **kwarg Renders a given jinja template and returns a string of script text :param template_name: the name of the jinja template to render - :param directives: additional AHK directives to add to all scripts + :param directives: additional AHK directives to add to the resulting script :param blocking: whether the template should be rendered to block (use #Persistent directive) :param kwargs: keywords passed to template rendering :return: An AutoHotkey script as a string From 2c0eb9ad7d29740c99256789051f66e5e4539972 Mon Sep 17 00:00:00 2001 From: Spencer Young Date: Fri, 10 Apr 2020 22:54:09 -0700 Subject: [PATCH 4/4] clean keyboard interrupt; make console_spy non-public. --- ahk/{console_spy.py => _console_spy.py} | 6 +++++- setup.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) rename ahk/{console_spy.py => _console_spy.py} (83%) diff --git a/ahk/console_spy.py b/ahk/_console_spy.py similarity index 83% rename from ahk/console_spy.py rename to ahk/_console_spy.py index db9caaef..54e95182 100644 --- a/ahk/console_spy.py +++ b/ahk/_console_spy.py @@ -12,5 +12,9 @@ def _console_spy(*args, **kwargs): color = ahk.pixel_get_color(*position) print(f'Mouse Position: {position} Pixel Color: {color} Window Title: {window.title} ', end='\r') + def _main(): - _console_spy() + try: + _console_spy() + except KeyboardInterrupt: + print('Exiting.') \ No newline at end of file diff --git a/setup.py b/setup.py index eff4d18d..fd14c276 100644 --- a/setup.py +++ b/setup.py @@ -32,6 +32,6 @@ include_package_data=True, zip_safe=False, entry_points={ - 'console_scripts': ['console_spy = ahk.console_spy:_main'] + 'console_scripts': ['console_spy = ahk._console_spy:_main'] } )