Exemplo n.º 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)
Exemplo n.º 2
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"]
Exemplo n.º 3
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
Exemplo n.º 4
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")
Exemplo n.º 5
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"]
Exemplo n.º 6
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.")
Exemplo n.º 7
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))
Exemplo n.º 8
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
Exemplo n.º 9
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
Exemplo n.º 10
0
    def execute(self, facade):
        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]
        modifier_value = CommandStrategy.split_exact_unit_name(
            self.command_tokens[4])

        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(facade.parser, unit_type, unit_name,
                               modifier_name, modifier_value)
        else:
            count = 0
            for unit_name in self.next_matching_unit_name(
                    facade.parser, unit_type, unit_regex):
                self._set_modifier(facade.parser, unit_type, unit_name,
                                   modifier_name, modifier_value)
                count += 1
            if count == 0:
                self.print_wrapper.write("No " + unit_type.name + " matched.")
Exemplo n.º 11
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
Exemplo n.º 12
0
    def execute(self, facade):
        """
        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:
                facade.parser.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.")
Exemplo n.º 13
0
    def execute(self, facade):
        # 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(facade.parser, unit_type, unit_name, variation_name,
                           rule_str)
        else:
            count = 0
            for unit_name in self.next_matching_unit_name(facade.parser,
                                                          unit_type,
                                                          unit_regex):
                self._add_rule(facade.parser, unit_type, unit_name, None,
                               rule_str)
                count += 1
            if count == 0:
                self.print_wrapper.write("No " + unit_type.name + " matched.")
Exemplo n.º 14
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"
     ]
Exemplo n.º 15
0
 def test(self):
     CommandStrategy("").finish_execution(None)
Exemplo n.º 16
0
 def test_empty(self):
     assert CommandStrategy.find_redirection_file_path(
         self.to_tokens("")) is None
Exemplo n.º 17
0
 def to_tokens(text):
     return CommandStrategy.tokenize(text)
Exemplo n.º 18
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")
Exemplo n.º 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$^")
Exemplo n.º 20
0
 def test_empty(self):
     assert not CommandStrategy._is_end_regex("")
Exemplo n.º 21
0
 def test_escapement(self):
     assert CommandStrategy.tokenize('test "escaped \\" was here"') == \
            ["test", '"escaped \\" was here"']
Exemplo n.º 22
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"']
Exemplo n.º 23
0
 def test_empty_command(self):
     assert CommandStrategy("").get_regex_name("") is None
Exemplo n.º 24
0
    def execute(self, facade):
        """
        Implements the command `generate` which generates all possible examples
        of a certain unit, formatted according to a certain adapter.
        """
        if len(self.command_tokens) == 1:
            facade.run_generation()
            return
        if len(self.command_tokens) == 2:
            try:
                facade.run_generation(self.command_tokens[1])
            except ValueError:
                self.print_wrapper.write("Unknown adapter: '" +
                                         self.command_tokens[1] + "'")
            return
        if len(self.command_tokens) < 4:
            self.print_wrapper.error_log("Missing some arguments\nUsage: " +
                                         self.usage_str)
            return

        adapter_str = self.command_tokens[1]
        try:
            adapter = create_adapter(adapter_str)
        except ValueError:
            self.print_wrapper.error_log("Unknown adapter '" + adapter_str + \
                                         "'.")

        if len(self.command_tokens) == 5:
            try:
                self.nb_examples = int(self.command_tokens[-1])
            except ValueError:
                self.print_wrapper.error_log("The number of examples to be " +
                                             "generated is invalid: it must " +
                                             "be an integer (no other " +
                                             "characters allowed).")
                return

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

        unit_regex = self.get_regex_name(self.command_tokens[3])
        if unit_regex is None:
            try:
                [unit_name, variation_name] = \
                    CommandStrategy.split_exact_unit_name(self.command_tokens[3])
            except SyntaxError:
                self.print_wrapper.error_log("Unit identifier couldn't be " + \
                                             "interpreted. Did you mean to " + \
                                             "escape some hashtags '#'?")
                return
            self._generate_unit(facade, adapter, unit_type, unit_name,
                                variation_name)
        else:
            count = 0
            for unit_name in self.next_matching_unit_name(
                    facade.parser, unit_type, unit_regex):
                self._generate_unit(facade, adapter, unit_type, unit_name)
                count += 1
            if count == 0:
                self.print_wrapper.write("No " + unit_type.name + " matched.")
        self.finish_execution(facade)
Exemplo n.º 25
0
 def test_should_be_overriden(self):
     with pytest.raises(NotImplementedError):
         CommandStrategy("").execute_on_unit(None, None, None)
Exemplo n.º 26
0
 def test(self):
     assert not CommandStrategy("").should_exit()
Exemplo n.º 27
0
 def test_flush(self):
     CommandStrategy("").flush_output()
Exemplo n.º 28
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
Exemplo n.º 29
0
 def test(self):
     with pytest.raises(NotImplementedError):
         CommandStrategy("NOTHING alias a, b, c").execute(None)
Exemplo n.º 30
0
 def test_empty(self):
     assert CommandStrategy.tokenize("") == []