File: engine_helpers.py

package info (click to toggle)
autokey 0.96.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,468 kB
  • sloc: python: 11,473; xml: 3,260; javascript: 249; sh: 24; makefile: 5
file content (88 lines) | stat: -rw-r--r-- 3,245 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright (C) 2020 BlueDrink9

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import typing
import string
import random

from unittest.mock import MagicMock, patch

import pytest
from hamcrest import *

import autokey.model.folder
from autokey.configmanager.configmanager import ConfigManager
from autokey.service import PhraseRunner
import autokey.service
from autokey.scripting import Engine

# For use in paramerizations.
# Call replace_folder_in_args(folder, args) to use.
folder_param="create_new_folder"

@pytest.fixture
def create_engine() -> typing.Tuple[Engine, autokey.model.folder.Folder]:
    # Make sure to not write to the hard disk
    test_folder = autokey.model.folder.Folder("Test folder")
    test_folder.persist = MagicMock()

    # Mock load_global_config to add the test folder to the known folders. This causes the ConfigManager to skip its
    # first-run logic.
    with patch("autokey.model.phrase.Phrase.persist"), patch("autokey.model.folder.Folder.persist"),\
         patch("autokey.configmanager.configmanager.ConfigManager.load_global_config",
               new=(lambda self: self.folders.append(test_folder))):
        engine = Engine(ConfigManager(MagicMock()), MagicMock(spec=PhraseRunner))
        engine.configManager.config_altered(False)

    return engine, test_folder


def create_random_string(length=10):
    return ''.join([random.choice(string.ascii_letters + string.digits) for n in
        range(length)])


def replace_folder_param_in_args(folder, args):
    if isinstance(args, str):
        return args
    args = [folder if x == folder_param else x for x in args]
    return args

def get_item_with_hotkey(engine, hotkey):
    modifiers = sorted(hotkey[0])
    item = engine.configManager.get_item_with_hotkey(modifiers, hotkey[1])
    return item

def assert_both_phrases_with_hotkey_exist(engine, p1, p2, hotkey):
    phrases = [p1, p2]
    for _ in phrases:
        phrase=get_item_with_hotkey(engine, hotkey)
        assert phrase in phrases
        # assert_that(phrase, is_(p1 or p2))
        # Remove hotkey from that phrase, so that the next check should
        # return the other phrase that had that hotkey.
        phrase.unset_hotkey()
        phrases.remove(phrase)

def create_test_hotkey(engine, folder, hotkey, replaceExisting=False,
                       windowFilter=None):
    with patch("autokey.model.phrase.Phrase.persist"):
        return engine.create_phrase(folder,
                create_random_string(),
                "ABC",
                hotkey=hotkey,
                replace_existing_hotkey=replaceExisting,
                window_filter=windowFilter,
                )