Example #1
0
 def test_quiet(self):
     assert CommandStrategy.find_redirection_file_path(self.to_tokens("stats >>")) == \
            (RedirectionType.quiet, None)
     assert CommandStrategy.find_redirection_file_path(self.to_tokens("exit >")) == \
            (RedirectionType.quiet, None)
     assert CommandStrategy.find_redirection_file_path(self.to_tokens("command >  ")) == \
            (RedirectionType.quiet, None)
Example #2
0
 def test_no_redirection(self):
     assert CommandStrategy.find_redirection_file_path(
         self.to_tokens("exit")) is None
     assert CommandStrategy.find_redirection_file_path(
         self.to_tokens("test something")) is None
     assert CommandStrategy.find_redirection_file_path(
         self.to_tokens('long command "with quotes\" inside"')) is None
Example #3
0
 def test_redirection(self):
     obj = CommandStrategy("exit > path/to/file.txt")
     assert obj.command_tokens == ["exit"]
     obj = CommandStrategy('exist intent "test" >> file/no/extension')
     assert obj.command_tokens == ["exist", "intent", '"test"']
     obj = CommandStrategy("test something /else/i  > ")
     assert obj.command_tokens == ["test", "something", "/else/i"]
Example #4
0
    def execute(self):
        """
        Implements the command `rule` which generates a certain number of
        examples according to a provided rule.
        """
        if len(self.command_tokens) < 3:
            self.print_wrapper.error_log("Missing some arguments\nUsage: " +
                                         self.usage_str)
            return

        unit_type = \
            CommandStrategy.get_unit_type_from_str(self.command_tokens[1])
        if unit_type is None:
            self.print_wrapper.error_log("Unknown unit type: '" +
                                         str(self.command_tokens[1]) + "'.")
            return

        try:
            [unit_name, variation_name] = \
                CommandStrategy.split_exact_unit_name(self.command_tokens[2])
        except SyntaxError:
            self.print_wrapper.error_log(
                "Unit identifier couldn't be interpreted. " + \
                "Did you mean to escape some hashtags '#'?"
            )
            return
        if variation_name is not None and variation_name != "":
            self.print_wrapper.error_log(
                "Variation name detected, while units cannot be " + \
                "declared with a variation. " + \
                "Did you mean to escape some hashtags '#'?"
            )
            return

        relevant_dict = AST.get_or_create()[unit_type]
        if unit_type == UnitType.alias:
            builder = AliasDefBuilder()
            builder.identifier = unit_name
            declaration = builder.create_concrete()
        elif unit_type == UnitType.slot:
            builder = SlotDefBuilder()
            builder.identifier = unit_name
            declaration = builder.create_concrete()
        else:  # intent
            builder = IntentDefBuilder()
            builder.identifier = unit_name
            declaration = builder.create_concrete()

        if unit_name in relevant_dict:
            self.print_wrapper.write(
                unit_type.name.capitalize() + " '" + unit_name + \
                "' was NOT defined, as it already is defined."
            )
            return
        relevant_dict[unit_name] = declaration
        self.print_wrapper.write(
            unit_type.name.capitalize() + " '" + unit_name + \
            "' was successfully declared."
        )
Example #5
0
 def test_truncate_redirection(self):
     assert CommandStrategy.find_redirection_file_path(self.to_tokens("stats > test.txt")) == \
            (RedirectionType.truncate, "test.txt")
     assert CommandStrategy.find_redirection_file_path(
         self.to_tokens("another rule > different/path.extension")) == (
             RedirectionType.truncate, "different/path.extension")
     assert CommandStrategy.find_redirection_file_path(
         self.to_tokens(
             'rule "with quotes\" and escapements" > /path/no/extension')
     ) == (RedirectionType.truncate, "/path/no/extension")
Example #6
0
 def test_long_commands(self):
     assert CommandStrategy.tokenize('rule "~[a rule] tested"') == \
            ["rule", '"~[a rule] tested"']
     assert \
         CommandStrategy.tokenize(
             'set-modifier alias "something else" casegen "True"\t'
         ) == [
             "set-modifier", "alias",
             '"something else"', "casegen", '"True"'
         ]
Example #7
0
 def test(self):
     assert CommandStrategy.split_exact_unit_name('"quoted"') == \
            ["quoted", None]
     assert CommandStrategy.split_exact_unit_name('"the quotes"') == \
            ["the quotes", None]
     assert CommandStrategy.split_exact_unit_name(r'"escaped\""') == \
            ['escaped"', None]
     assert CommandStrategy.split_exact_unit_name('"test#var"') == \
            ["test", "var"]
     assert CommandStrategy.split_exact_unit_name(r'"unit\#hashtag#var"') == \
            ["unit#hashtag", "var"]
Example #8
0
    def execute(self):
        if len(self.command_tokens) < 5:
            self.print_wrapper.error_log(
                "Missing some arguments\nUsage: " + self.usage_str
            )
            return

        unit_type = CommandStrategy.get_unit_type_from_str(self.command_tokens[1])
        if unit_type is None:
            self.print_wrapper.error_log(
                "Unknown unit type: '" + str(self.command_tokens[1]) + "'."
            )
            return

        unit_regex = self.get_regex_name(self.command_tokens[2])

        modifier_name = self.command_tokens[3]
        # TODO this is not the best way to get the value
        modifier_value = \
            CommandStrategy.split_exact_unit_name(self.command_tokens[4])[0]

        if unit_regex is None:
            try:
                [unit_name, variation_name] = \
                    CommandStrategy.split_exact_unit_name(
                        self.command_tokens[2]
                    )
            except SyntaxError:
                self.print_wrapper.error_log(
                    "Unit identifier couldn't be interpreted. " + \
                    "Did you mean to escape some hashtags '#'?"
                )
                return
            if variation_name is not None:
                self.print_wrapper.error_log(
                    "Cannot set a modifier for the variation of a unit."
                )
                return
            self._set_modifier(
                unit_type, unit_name, modifier_name, modifier_value
            )
        else:
            count = 0
            for unit_name in self.next_matching_unit_name(
                unit_type, unit_regex
            ):
                self._set_modifier(
                    unit_type, unit_name, modifier_name, modifier_value
                )
                count += 1
            if count == 0:
                self.print_wrapper.write("No " + unit_type.name + " matched.")
Example #9
0
    def execute(self, facade):
        """
        Implements the command `rule` which generates a certain number of
        examples according to a provided rule.
        """
        if len(self.command_tokens) < 3:
            self.print_wrapper.error_log("Missing some arguments\nUsage: " +
                                         self.usage_str)
            return

        unit_type = CommandStrategy.get_unit_type_from_str(self.command_tokens[1])
        if unit_type is None:
            self.print_wrapper.error_log("Unknown unit type: '" +
                                         str(self.command_tokens[1]) + "'.")
            return

        try:
            [unit_name, variation_name] = \
                CommandStrategy.split_exact_unit_name(self.command_tokens[2])
        except SyntaxError:
            self.print_wrapper.error_log("Unit identifier couldn't be " + \
                                         "interpreted. Did you mean to " + \
                                         "escape some hashtags '#'?")
            return
        if variation_name is not None and variation_name != "":
            self.print_wrapper.error_log("Variation name detected, while " + \
                                         "units cannot be declared with a " + \
                                         "variation. Did you mean to escape " + \
                                         "some hashtags '#'?")
            return

        if unit_type == UnitType.alias:
            declaration = AliasDefinition(unit_name, None)
            relevant_dict = facade.parser.alias_definitions
        elif unit_type == UnitType.slot:
            declaration = SlotDefinition(unit_name, None)
            relevant_dict = facade.parser.slot_definitions
        else:  # intent
            declaration = IntentDefinition(unit_name, None)
            relevant_dict = facade.parser.intent_definitions

        if unit_name in relevant_dict:
            self.print_wrapper.write(unit_type.name.capitalize() + " '" +
                                     unit_name + "' is already defined.")
            return
        relevant_dict[unit_name] = declaration
        self.print_wrapper.write(unit_type.name.capitalize() + " '" +
                                 unit_name + "' was successfully declared.")
Example #10
0
    def execute(self, facade):
        """
        Implements the command `rule` which generates a certain number of
        examples according to a provided rule.
        """
        if len(self.command_tokens) < 2:
            self.print_wrapper.error_log("Missing some arguments\nUsage: " +
                                         'rule "<rule>" [<number-of-examples]')
            return

        rule_str = CommandStrategy.remove_quotes(self.command_tokens[1])
        nb_examples = None
        if len(self.command_tokens) >= 3:
            try:
                nb_examples = int(self.command_tokens[2])
            except ValueError:
                self.print_wrapper.error_log("The number of examples asked (" +
                                             self.command_tokens[2] + ") is " +
                                             "a valid integer.")

        rule_tokens = facade.parser.tokenizer.tokenize(rule_str)
        rule = facade.parser.tokens_to_sub_rules(rule_tokens)
        definition = AliasDefinition("INTERNAL", None, [rule])
        try:
            examples = definition.generate_nb_examples(nb_examples)
            self.print_wrapper.write("Generated examples:")
            for ex in examples:
                self.print_wrapper.write(ex.text.replace(ENTITY_MARKER, ""))
        except KeyError as e:
            self.print_wrapper.error_log("Upon generation: " + str(e))
Example #11
0
    def execute(self):
        """
        Implements the command `rule` which generates a certain number of
        examples according to a provided rule.
        """
        if len(self.command_tokens) < 2:
            self.print_wrapper.error_log("Missing some arguments\nUsage: " +
                                         'rule "<rule>" [<number-of-examples]')
            return

        rule_str = CommandStrategy.remove_quotes(self.command_tokens[1])
        nb_examples = None
        if len(self.command_tokens) >= 3:
            try:
                nb_examples = int(self.command_tokens[2])
            except ValueError:
                self.print_wrapper.error_log(
                    "The number of examples asked (" + \
                    self.command_tokens[2] + ") is a valid integer."
                )

        parser = Parser(None)
        rule_tokens = parser.lexer.lex("\t" + rule_str)
        # pylint: disable=protected-access
        rule = parser._parse_rule(rule_tokens[1:])

        if nb_examples is None:
            examples = rule.generate_all()
        else:
            examples = rule.generate_nb_possibilities(nb_examples)
        self.print_wrapper.write("Generated examples:")
        for ex in examples:
            self.print_wrapper.write(str(ex))
Example #12
0
 def test_correct_str(self):
     assert CommandStrategy.get_unit_type_from_str(
         "alias") == UnitType.alias
     assert CommandStrategy.get_unit_type_from_str(
         "AliaS") == UnitType.alias
     assert CommandStrategy.get_unit_type_from_str('~') == UnitType.alias
     assert CommandStrategy.get_unit_type_from_str("slot") == UnitType.slot
     assert CommandStrategy.get_unit_type_from_str("SLOT") == UnitType.slot
     assert CommandStrategy.get_unit_type_from_str('@') == UnitType.slot
     assert CommandStrategy.get_unit_type_from_str(
         "intent") == UnitType.intent
     assert CommandStrategy.get_unit_type_from_str(
         "iNtENt") == UnitType.intent
     assert CommandStrategy.get_unit_type_from_str('%') == UnitType.intent
Example #13
0
 def test_regex(self):
     assert CommandStrategy("").get_regex_name("/regex/") == \
            re.compile("regex")
     assert CommandStrategy("").get_regex_name("/test.*/") == \
            re.compile("test.*")
     assert CommandStrategy("").get_regex_name("/some[0-9]/i") == \
            re.compile("some[0-9]", re.IGNORECASE)
     obj = CommandStrategy("")
     assert obj.get_regex_name("/test/g") == re.compile("test")
     assert obj._is_regex_global
     obj = CommandStrategy("")
     assert obj.get_regex_name("/$x+^/ig") == re.compile(
         "$x+^", re.IGNORECASE)
     assert obj._is_regex_global
Example #14
0
    def execute(self):
        """
        Implements the command `unhide` which restores a unit definition that
        was hidden from the AST.
        """
        if len(self.command_tokens) < 3:
            self.print_wrapper.error_log(
                "Missing some arguments\nUsage: " +
                'unhide <unit-type> "<unit-name>"'
            )
            return

        unit_type = CommandStrategy.get_unit_type_from_str(self.command_tokens[1])
        if unit_type is None:
            self.print_wrapper.error_log(
                "Unknown unit type: '" + str(self.command_tokens[1]) + "'."
            )
            return

        unit_regex = self.get_regex_name(self.command_tokens[2])
        if unit_regex is None:
            try:
                [unit_name, variation_name] = \
                    CommandStrategy.split_exact_unit_name(self.command_tokens[2])
            except SyntaxError:
                self.print_wrapper.error_log(
                    "Unit identifier couldn't be interpreted. " + \
                    "Did you mean to escape some hashtags '#'?"
                )
                return
            self.execute_on_unit(unit_type, unit_name, variation_name)
        else:
            unit_names = [
                unit_name
                for unit_name in HideCommand.stored_units[unit_type.name]
                if unit_regex.match(unit_name)
            ]
            if len(unit_names) == 0:
                self.print_wrapper.write("No " + unit_type.name + " matched.")

            for unit_name in unit_names:
                self.execute_on_unit(unit_type, unit_name)
Example #15
0
    def execute(self):
        """
        Implements the command `rename` which renames a unit
        into something else. Displays an error if the unit wasn't found.
        """
        if len(self.command_tokens) < 4:
            self.print_wrapper.error_log(
                "Missing some arguments\nUsage: " + \
                'rename <unit-type> "<old-name>" ' + '"<new-name>"')
            return
        unit_type = \
            CommandStrategy.get_unit_type_from_str(self.command_tokens[1])

        if unit_type is None:
            self.print_wrapper.error_log(
                "Unknown unit type: '" + str(self.command_tokens[1]) + "'."
            )
        else:
            old_name = CommandStrategy.remove_quotes(self.command_tokens[2])
            new_name = CommandStrategy.remove_quotes(self.command_tokens[3])
            if new_name == "":
                self.print_wrapper.error_log(
                    "An empty name is not a valid " + unit_type.name + " name."
                )
                return

            try:
                AST.get_or_create().rename_unit(unit_type, old_name, new_name)
                self.print_wrapper.write(
                    unit_type.name.capitalize() + " '" + old_name + \
                    "' was successfully renamed to '" + new_name + "'.")
            except KeyError:
                self.print_wrapper.error_log(
                    "Couldn't find a unit named '" + str(old_name) + "'."
                )
            except ValueError:
                self.print_wrapper.error_log(
                    unit_type.name.capitalize() + " '" + new_name + \
                    "' is already in use."
                )
Example #16
0
 def test_wrong_str(self):
     assert CommandStrategy.get_unit_type_from_str("") is None
     assert CommandStrategy.get_unit_type_from_str("t") is None
     assert CommandStrategy.get_unit_type_from_str("test") is None
     assert CommandStrategy.get_unit_type_from_str("SOMETHING") is None
     assert CommandStrategy.get_unit_type_from_str("\t\t ") is None
     assert CommandStrategy.get_unit_type_from_str("@%~") is None
Example #17
0
    def execute(self):
        # TODO support variations
        if len(self.command_tokens) < 4:
            self.print_wrapper.error_log("Missing some arguments\nUsage: " +
                                         self.usage_str)
            return

        unit_type = \
            CommandStrategy.get_unit_type_from_str(self.command_tokens[1])
        if unit_type is None:
            self.print_wrapper.error_log("Unknown unit type: '" +
                                         str(self.command_tokens[1]) + "'.")
            return

        unit_regex = self.get_regex_name(self.command_tokens[2])
        rule_str = CommandStrategy.remove_quotes(self.command_tokens[3])
        if unit_regex is None:
            try:
                [unit_name, variation_name] = \
                    CommandStrategy.split_exact_unit_name(
                        self.command_tokens[2]
                    )
            except SyntaxError:
                self.print_wrapper.error_log(
                    "Unit identifier couldn't be interpreted. " + \
                    "Did you mean to escape some hashtags '#'?"
                )
                return
            self._add_rule(unit_type, unit_name, variation_name, rule_str)
        else:
            count = 0
            for unit_name in self.next_matching_unit_name(
                    unit_type, unit_regex):
                self._add_rule(unit_type, unit_name, None, rule_str)
                count += 1
            if count == 0:
                self.print_wrapper.write("No " + unit_type.name + " matched.")
Example #18
0
 def test_short_commands(self):
     assert CommandStrategy.tokenize("exit") == ["exit"]
     assert CommandStrategy.tokenize("stats  ") == ["stats"]
     assert CommandStrategy.tokenize("NOT-command") == ["NOT-command"]
     assert CommandStrategy.tokenize("NOT COMMAND") == ["NOT", "COMMAND"]
     assert CommandStrategy.tokenize('word "a name"') == [
         "word", '"a name"'
     ]
     assert CommandStrategy.tokenize(' open "quote a') == [
         "open", '"quote a'
     ]
     assert CommandStrategy.tokenize("regex /with space/i") == [
         "regex", "/with space/i"
     ]
Example #19
0
 def test_not_regex(self):
     assert not CommandStrategy._is_end_regex("test")
     assert not CommandStrategy._is_end_regex("something")
     assert not CommandStrategy._is_end_regex("a longer thing")
     assert not CommandStrategy._is_end_regex("/special characters$^")
Example #20
0
 def test_empty(self):
     assert not CommandStrategy._is_end_regex("")
Example #21
0
 def test_escapement(self):
     assert CommandStrategy.tokenize('test "escaped \\" was here"') == \
            ["test", '"escaped \\" was here"']
Example #22
0
 def test_empty(self):
     assert CommandStrategy.tokenize("") == []
Example #23
0
 def test_no_regex(self):
     assert CommandStrategy("").get_regex_name("exit") is None
     assert CommandStrategy("").get_regex_name('"alias"') is None
     assert CommandStrategy("").get_regex_name('"something with/slash"') \
            is None
Example #24
0
 def test_should_be_overriden(self):
     with pytest.raises(NotImplementedError):
         CommandStrategy("").execute_on_unit(None, None, None)
Example #25
0
 def test(self):
     with pytest.raises(NotImplementedError):
         CommandStrategy("NOTHING alias a, b, c").execute()
Example #26
0
 def test(self):
     assert not CommandStrategy("").should_exit()
Example #27
0
 def test_flush(self):
     CommandStrategy("").flush_output()
Example #28
0
 def test_regexes(self):
     assert CommandStrategy._is_end_regex("/something.*/")
     assert CommandStrategy._is_end_regex("/something else/i")
     assert CommandStrategy._is_end_regex("another /g")
     assert CommandStrategy._is_end_regex("/a last thing/ig")
Example #29
0
 def test(self):
     CommandStrategy("").finish_execution()
Example #30
0
 def test_empty_command(self):
     assert CommandStrategy("").get_regex_name("") is None