Ejemplo n.º 1
0
 def delete_variation(self, variation_name):
     """
     Deletes the variation named `variation_name`.
     @raises: - `KeyError` if the variation doesn't exist.
     """
     if not self.has_variation(variation_name):
         raise KeyError(
             "Tried to delete variation '" + str(variation_name) + \
             "' of " + self.full_name + ", but it wasn't defined."
         )
     Stats.get_or_create().one_variation_unit_removed(self.unit_type)
     del self._variation_rules[variation_name]
     self._recompute_all_rules()
Ejemplo n.º 2
0
    def _parse_rule_line(self, lexical_tokens):
        """
        Handles a line that is a rule within a unit definition.
        Adds the rule to the currently parsed unit.
        """
        if (self._last_indentation is not None
                and lexical_tokens[0].text != self._last_indentation):
            self.input_file_manager.syntax_error("Inconsistent indentation.")
        if self._current_unit_declaration is None:
            self.input_file_manager.syntax_error(
                "Detected a rule outside a unit declaration.")

        rule = self._parse_rule(lexical_tokens[1:])
        self._current_unit_declaration.add_rule(rule,
                                                self._current_variation_name)

        Stats.get_or_create().new_rule_parsed()
Ejemplo n.º 3
0
 def get_stats_as_str(self):
     stats = Stats.get_or_create()
     result = '\t' + str(stats.get_nb_files()) + " files parsed\n" + \
              '\t' + str(stats.get_nb_declarations()) + " declarations: " + \
              str(stats.get_nb_intents()) + " intents, " + \
              str(stats.get_nb_slots()) + " slots and " + \
              str(stats.get_nb_aliases()) + " aliases\n" + \
              '\t' + str(stats.get_nb_rules()) + " rules"
     return result
Ejemplo n.º 4
0
    def open_file(self, file_path):
        """
        Opens the file at `file_path` if and only if it wasn't opened before.
        Stores the currently read file for further reading if needed.
        `file_path` is understood with respect to the currently being parsed
        file (rather than working directory),
        unless it is an absolute path or there is no file currently being
        parsed.
        @raises: - `FileAlreadyOpened` if the file at `file_path`
                   was already opened.
        """
        file_path = cast_to_unicode(file_path)
        if not os.path.isabs(file_path):
            if self._current_file is None:
                file_path = cast_to_unicode(os.path.abspath(file_path))
            else:
                file_path = \
                    os.path.join(
                        cast_to_unicode(os.path.dirname(self._current_file.name)),
                        file_path
                    )

        opened_file_paths = [f.name for f in self._opened_files]
        if file_path in opened_file_paths:
            raise FileAlreadyOpened("Tried to read file '" + file_path +
                                    "' several times.")

        if self._current_file is not None:
            self._opened_files.append(self._current_file)
        try:
            self._current_file = LineCountFileWrapper(file_path)
            Stats.get_or_create().new_file_parsed()
        except IOError as e:
            if len(self._opened_files) > 0:
                self._current_file = self._opened_files.pop()
            raise e
Ejemplo n.º 5
0
    def __init__(self):
        self._alias_definitions = dict()
        self._slot_definitions = dict()
        self._intent_definitions = dict()

        self.stats = Stats.get_or_create()
Ejemplo n.º 6
0
 def test_singleton(self):
     first = Stats.get_or_create()
     second = Stats.get_or_create()
     assert first == second
     reset = Stats.reset_instance()
     assert first != reset
Ejemplo n.º 7
0
 def execute(self):
     """Implements the command `stats`, printing parsing statistics."""
     self.print_wrapper.write(str(Stats.get_or_create()))