コード例 #1
0
ファイル: rule_command.py プロジェクト: lei1993/oa_qa
    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))
コード例 #2
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))
コード例 #3
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."
                )
コード例 #4
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.")
コード例 #5
0
 def test(self):
     assert CommandStrategy.remove_quotes('"quoted"') == "quoted"
     assert CommandStrategy.remove_quotes('"the quotes"') == "the quotes"
     assert CommandStrategy.remove_quotes(r'"escaped\""') == 'escaped"'