Ejemplo n.º 1
0
    def test_internal_rules_dont_create_duplicates(self):
        from castervoice.lib import utilities
        from castervoice.lib.ctrl.rules_config import RulesConfig
        from castervoice.rules.core.alphabet_rules import alphabet

        # "write" the rules.toml file:
        self._setup_rules_config_file(
            loadable_true=["Alphabet"],
            enabled=["GrammarActivatorRule", "ManualGrammarReloadRule"])

        # check that the mock file changes were written
        self.assertEqual(
            2, len(self._rule_config._config[RulesConfig._ENABLED_ORDERED]))

        # initialize the gm
        alphabet_rule = alphabet.get_rule()
        self._initialize(FullContentSet([alphabet_rule], [], []))
        """
        After initialization, there should only be one copy of each of these in rules.toml: 
        GrammarActivatorRule 
        ManualGrammarReloadRule
        """
        config = utilities.load_toml_file(
            TestGrammarManager._MOCK_PATH_RULES_CONFIG)
        enabled_ordered = config[RulesConfig._ENABLED_ORDERED]
        self.assertEqual(1, enabled_ordered.count("GrammarActivatorRule"))
        self.assertEqual(1, enabled_ordered.count("ManualGrammarReloadRule"))
Ejemplo n.º 2
0
    def test_enable_incompatible_rule_knockout_is_saved(self):
        from castervoice.lib import utilities
        from castervoice.lib.ctrl.rules_config import RulesConfig
        from castervoice.rules.ccr.java_rules import java
        from castervoice.rules.ccr.python_rules import python

        # "write" the rules.toml file:
        self._setup_rules_config_file(loadable_true=["Java", "Python"],
                                      enabled=["Java"])

        # check that the mock file changes were written
        self.assertEqual(
            1, len(self._rule_config._config[RulesConfig._ENABLED_ORDERED]))

        # initialize the gm
        a, b = java.get_rule(), python.get_rule()
        self._initialize(FullContentSet([a, b], [], []))

        # simulate a spoken "enable" command from the GrammarActivator:
        self._gm._change_rule_enabled("Python", True)
        # afterwards, the config should have Python enabled but not Java
        config = utilities.load_toml_file(
            TestGrammarManager._MOCK_PATH_RULES_CONFIG)
        self.assertNotIn("Java", config[RulesConfig._ENABLED_ORDERED])
        self.assertIn("Python", config[RulesConfig._ENABLED_ORDERED])
Ejemplo n.º 3
0
    def test_enable_rule_causes_a_save(self):
        from castervoice.lib import utilities
        from castervoice.lib.ctrl.rules_config import RulesConfig
        from castervoice.rules.core.alphabet_rules import alphabet
        from castervoice.rules.core.punctuation_rules import punctuation

        # "write" the rules.toml file:
        self._setup_rules_config_file(
            loadable_true=["Alphabet", "Punctuation"], enabled=["Alphabet"])

        # check that the mock file changes were written
        self.assertEqual(
            1, len(self._rule_config._config[RulesConfig._ENABLED_ORDERED]))

        # initialize the gm
        a, b = alphabet.get_rule(), punctuation.get_rule()
        self._initialize(FullContentSet([a, b], [], []))

        # simulate a spoken "enable" command from the GrammarActivator:
        self._gm._change_rule_enabled("Punctuation", True)
        # afterwards, the config should have both Alphabet and Punctuation enabled
        config = utilities.load_toml_file(
            TestGrammarManager._MOCK_PATH_RULES_CONFIG)
        self.assertIn("Alphabet", config[RulesConfig._ENABLED_ORDERED])
        self.assertIn("Punctuation", config[RulesConfig._ENABLED_ORDERED])
Ejemplo n.º 4
0
    def refresh(self, *args):
        '''args: spec, list of lists of strings'''

        # get mapping
        recorded_macros = utilities.load_toml_file(
            settings.SETTINGS["paths"]["RECORDED_MACROS_PATH"])
        if len(args) > 0:
            recorded_macros[args[0]] = args[1]
            utilities.save_toml_file(
                recorded_macros,
                settings.SETTINGS["paths"]["RECORDED_MACROS_PATH"])
        mapping = {}
        for spec in recorded_macros:
            # Create a copy of the string without Unicode characters.
            ascii_str = str(spec)
            sequences = recorded_macros[spec]
            delay = settings.SETTINGS["miscellaneous"][
                "history_playback_delay_secs"]
            # It appears that the associative string (ascii_str) must be ascii, but the sequences within Playback must be Unicode.
            mapping[ascii_str] = R(
                Playback([(sequence, delay) for sequence in sequences]),
                rdescript="Recorded Macro: " + ascii_str) * Repeat(extra="n")
        mapping["record from history"] = R(Function(self.record_from_history),
                                           rdescript="Record From History")
        mapping["delete recorded macros"] = R(
            Function(self.delete_recorded_macros),
            rdescript="Delete Recorded Macros")
        # reload with new mapping
        self.reset(mapping)
Ejemplo n.º 5
0
    def refresh(self, *args):
        '''args: spec, list of lists of strings'''

        # get mapping
        recorded_macros = utilities.load_toml_file(
            settings.SETTINGS["paths"]["RECORDED_MACROS_PATH"])
        if len(args) > 0:
            recorded_macros[args[0]] = args[1]
            utilities.save_toml_file(
                recorded_macros,
                settings.SETTINGS["paths"]["RECORDED_MACROS_PATH"])
        mapping = {}
        for spec in recorded_macros:
            sequences = recorded_macros[spec]
            delay = settings.SETTINGS["miscellaneous"][
                "history_playback_delay_secs"]
            play = Playback([(sequence, delay) for sequence in sequences])
            command = play * Repeat(
                extra="n") if spec.endswith("[times <n>]") else play
            mapping[spec] = R(command, rdescript="Recorded Macro: " + spec)
        mapping["record from history"] = R(Function(self.record_from_history),
                                           rdescript="Record From History")
        mapping["delete recorded macros"] = R(
            Function(self.delete_recorded_macros),
            rdescript="Delete Recorded Macros")
        # reload with new mapping
        self.reset(mapping)
Ejemplo n.º 6
0
    def test_knockout_with_companions_saves_correctly(self):
        from castervoice.lib import utilities
        from castervoice.lib.ctrl.rules_config import RulesConfig
        from castervoice.rules.ccr.java_rules import java
        from castervoice.rules.ccr.java_rules import java2
        from castervoice.rules.ccr.python_rules import python
        from castervoice.rules.ccr.python_rules import python2
        from castervoice.rules.core.alphabet_rules import alphabet
        from castervoice.rules.apps.microsoft_office import outlook

        # "write" the companion config file
        self._setup_config_file(utilities, ["paths", "COMPANION_CONFIG_PATH"],
                                TestGrammarManager._MOCK_PATH_COMPANION_CONFIG,
                                {
                                    "Java": ["JavaNon"],
                                    "Python": ["PythonNon"]
                                })

        # "write" the rules.toml file:
        self._setup_rules_config_file(
            loadable_true=[
                "Alphabet", "OutlookRule", "Java", "JavaNon", "Python",
                "PythonNon"
            ],
            enabled=["Alphabet", "OutlookRule", "Java", "JavaNon"])

        # check that the mock file changes were written
        self.assertEqual(
            4, len(self._rule_config._config[RulesConfig._ENABLED_ORDERED]))

        # initialize the gm
        alphabet_rule, outlook_rule = alphabet.get_rule(), outlook.get_rule()
        python_rule, pythonnon_rule = python.get_rule(), python2.get_rule()
        java_rule, javanon_rule = java.get_rule(), java2.get_rule()
        self._initialize(
            FullContentSet([
                alphabet_rule, outlook_rule, python_rule, pythonnon_rule,
                java_rule, javanon_rule
            ], [], []))

        # simulate a spoken "enable" command from the GrammarActivator:
        self._gm._change_rule_enabled("Python", True)
        """
        Afterwards, all of the following should be true:
        1. Python and PythonNon should each be in rules.toml exactly once
        2. Java and JavaNon should be in rules.toml zero times
        3. Alphabet and Outlook should still be in rules.toml
        """
        config = utilities.load_toml_file(
            TestGrammarManager._MOCK_PATH_RULES_CONFIG)
        enabled_ordered = config[RulesConfig._ENABLED_ORDERED]
        self.assertEqual(1, enabled_ordered.count("Python"))
        self.assertEqual(1, enabled_ordered.count("PythonNon"))
        self.assertEqual(0, enabled_ordered.count("Java"))
        self.assertEqual(0, enabled_ordered.count("JavaNon"))
        self.assertEqual(1, enabled_ordered.count("Alphabet"))
        self.assertEqual(1, enabled_ordered.count("OutlookRule"))
Ejemplo n.º 7
0
 def load_config(self):
     if self.use_real_config:
         self._config = utilities.load_toml_file(
             settings.SETTINGS["paths"]["CCR_CONFIG_PATH"])
     else:
         self._config = {}
Ejemplo n.º 8
0
def github_checkoutupdate_pull_request(new):
    # Function to fetch a PR
    try:
        Key("c-l/20").execute()
        url = read_selected_without_altering_clipboard()
        if url[0] == 0:
            split_string = url[1].split("/pull/")
            repo_url = split_string[0]
            pr_name = split_string[1].split("/")[0]
            CONFIG = load_toml_file(
                settings.SETTINGS["paths"]["GIT_REPO_LOCAL_REMOTE_PATH"])
            if not CONFIG:
                # logger.warn("Could not load bringme defaults")
                raise Exception(
                    "Could not load " +
                    settings.SETTINGS["paths"]["GIT_REPO_LOCAL_REMOTE_PATH"])

            items = rebuild_local_remote_items(CONFIG)
            if repo_url in items:
                local_directory = items[repo_url][0]
                local_directory = local_directory.replace("\\", "\\\\")
                directory_command = "cd " + local_directory
                TERMINAL_PATH = settings.SETTINGS["paths"]["TERMINAL_PATH"]
                AHK_PATH = settings.SETTINGS["paths"]["AHK_PATH"]
                ahk_installed = os.path.isfile(AHK_PATH)
                print "AHK_PATH = " + AHK_PATH
                if TERMINAL_PATH != "":
                    load_terminal = True  # set default value
                    # ready fetch command string to be appended to
                    fetch_command = ""
                    # find the equivalent ahk script with the same name as this one
                    ahk_script = __file__.replace(".pyc", ".ahk").replace(
                        ".py", ".ahk")
                    pattern_match = "MINGW64"  # the string we expect to find in the title of git bash when loaded
                    # if autohotkey is installed
                    if ahk_installed:
                        # open the script which checks that git bash window is open or not
                        p = Popen(
                            [AHK_PATH, ahk_script, "exists", pattern_match],
                            stdout=PIPE)
                        # retrieve the output from the ahk script
                        stdout, stderr = p.communicate()
                        # terminates the ahk script if not already done so
                        p.terminate()

                        # if an existing git bash window has been activated
                        if stdout == pattern_match + " activated":
                            # set the first portion of the fetch command
                            fetch_command += directory_command + " && "
                            load_terminal = False
                            print "Msg:" + ahk_script + " has activated window: " + pattern_match
                        # if an existing git bash window is not already open
                        elif stdout == pattern_match + " does not exist":
                            print "Msg:" + ahk_script + " has found no window: " + pattern_match
                            print "Load new instance of: " + pattern_match
                        else:
                            print "Error:" + ahk_script + " neither returned 'activated' nor 'does not exist'"
                            print "Fallback: load new instance of :" + pattern_match
                    if load_terminal:
                        # open up a new git bash terminal
                        terminal = Popen(TERMINAL_PATH, cwd=local_directory)
                        # if autohotkey is installed
                        if ahk_installed:
                            # open the script which checks that git bash windoow is ready or not for input
                            p = Popen([
                                AHK_PATH, ahk_script, "create", pattern_match
                            ],
                                      stdout=PIPE)
                            # retrieve the output from the AHK script
                            stdout, stderr = p.communicate()
                            # terminates the ahk script if not already done so
                            p.terminate()
                            # if the git bash terminal is not ready
                            if stdout != pattern_match + " ready":
                                raise Exception(
                                    "Error: git terminal took too long to load for script:"
                                    + ahk_script)
                        else:  # otherwise await the default number of seconds for the terminal to load
                            time.sleep(
                                settings.SETTINGS["gitbash"]["loading_time"])
                    else:
                        # Remove any text that's there in the existing terminal
                        Key("end/10, c-u/10").execute()
                    # adds to the fetch command string that which will fetch from the particular repository in question
                    fetch_command += "git fetch " + repo_url + ".git pull/" + pr_name + "/head"
                    # if fetching from a new pull request
                    if new:
                        branch_name_base = repo_url.replace(
                            "https://github.com/", "")
                        checkout_command = "git checkout -b " + branch_name_base + "/pull/" + pr_name + " FETCH_HEAD"
                        # type in the full command into the git bash window
                        Text(fetch_command + " && " +
                             checkout_command).execute()
                    else:  # otherwise if it is an update to an existing pull request
                        branch_name_base = repo_url.replace(
                            "https://github.com/", "")
                        checkout_command = "git checkout " + branch_name_base + "/pull/" + pr_name
                        # type in the full command into the git bash window
                        Text(fetch_command + " && " +
                             checkout_command).execute()
                        Key("enter").execute(
                        )  # checkout is safe enough so will run this
                        merge_command = "git merge FETCH_HEAD"
                        # allow time for the fetch commands complete before typing in the next one
                        time.sleep(
                            settings.SETTINGS["gitbash"]["fetching_time"])
                        Text(merge_command).execute()
                else:
                    raise Exception(
                        'TERMINAL_PATH in <user_dir>/.caster/data/settings.toml is not set'
                    )
            else:
                raise Exception(
                    "Repository URL: " + repo_url + " not found in " +
                    settings.SETTINGS["paths"]["GIT_REPO_LOCAL_REMOTE_PATH"])
    except Exception as e:
        print(e)
Ejemplo n.º 9
0
 def load(self):
     self._config = utilities.load_toml_file(self._config_path)