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()
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()
def test_str(self): str_repr = str(Stats.reset_instance()) assert "Statistics:" in str_repr assert "Parsed files: 0" in str_repr assert "Declared units: 0 (0 variations)" in str_repr assert "Declared intents: 0 (0 variations)" in str_repr assert "Declared slots: 0 (0 variations)" in str_repr assert "Declared aliases: 0 (0 variations)" in str_repr assert "Parsed rules: 0" in str_repr
def test_new_rules(self): instance = Stats.reset_instance() assert instance.nb_rules_parsed == 0 instance.new_rules_parsed(2) assert instance.nb_rules_parsed == 2 instance.new_rules_parsed(5) assert instance.nb_rules_parsed == 7
def test_new_rule(self): instance = Stats.reset_instance() assert instance.nb_rules_parsed == 0 instance.new_rule_parsed() assert instance.nb_rules_parsed == 1 instance.new_rule_parsed() assert instance.nb_rules_parsed == 2
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
def test_remove_variations(self): instance = Stats.reset_instance() assert instance.nb_variation_units == 0 assert instance.nb_variation_intents == 0 assert instance.nb_variation_slots == 0 assert instance.nb_variation_aliases == 0 for _ in range(3): instance.new_variation_unit_declared(UnitType.alias) instance.several_variation_units_removed(UnitType.alias, 3) assert instance.nb_variation_units == 0 assert instance.nb_variation_aliases == 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
def test_remove_rules(self): instance = Stats.reset_instance() instance.new_rules_parsed(5) assert instance.nb_rules_parsed == 5 instance.one_rule_removed() assert instance.nb_rules_parsed == 4 instance.several_rules_removed(4) assert instance.nb_rules_parsed == 0 with pytest.raises(InvalidStatsState): instance.one_rule_removed() with pytest.raises(InvalidStatsState): instance.several_rules_removed(2)
def test_remove_variation(self): instance = Stats.reset_instance() assert instance.nb_variation_units == 0 assert instance.nb_variation_intents == 0 assert instance.nb_variation_slots == 0 assert instance.nb_variation_aliases == 0 instance.new_variation_unit_declared(UnitType.alias) instance.one_variation_unit_removed(UnitType.alias) assert instance.nb_variation_units == 0 assert instance.nb_variation_aliases == 0 instance.new_variation_unit_declared(UnitType.slot) instance.one_variation_unit_removed(UnitType.slot) assert instance.nb_variation_units == 0 assert instance.nb_variation_slots == 0 instance.new_variation_unit_declared(UnitType.intent) instance.one_variation_unit_removed(UnitType.intent) assert instance.nb_variation_units == 0 assert instance.nb_variation_intents == 0 with pytest.raises(TypeError): instance.one_variation_unit_removed("invalid") with pytest.raises(InvalidStatsState): instance.one_variation_unit_removed(UnitType.alias) with pytest.raises(InvalidStatsState): instance.one_variation_unit_removed(UnitType.slot) with pytest.raises(InvalidStatsState): instance.one_variation_unit_removed(UnitType.intent) instance.new_variation_unit_declared(UnitType.alias) instance.nb_variation_aliases = 0 with pytest.raises(InvalidStatsState): instance.one_variation_unit_removed(UnitType.alias) instance.new_variation_unit_declared(UnitType.slot) instance.nb_variation_slots = 0 with pytest.raises(InvalidStatsState): instance.one_variation_unit_removed(UnitType.slot) instance.new_variation_unit_declared(UnitType.intent) instance.nb_variation_intents = 0 with pytest.raises(InvalidStatsState): instance.one_variation_unit_removed(UnitType.intent)
def test_new_variations(self): instance = Stats.reset_instance() assert instance.nb_variation_units == 0 assert instance.nb_variation_intents == 0 assert instance.nb_slots_declared == 0 assert instance.nb_aliases_declared == 0 instance.new_variation_unit_declared(UnitType.intent) assert instance.nb_variation_units == 1 assert instance.nb_variation_intents == 1 instance.new_variation_unit_declared(UnitType.slot) assert instance.nb_variation_units == 2 assert instance.nb_variation_intents == 1 instance.new_variation_unit_declared(UnitType.alias) assert instance.nb_variation_units == 3 assert instance.nb_variation_aliases == 1 with pytest.raises(TypeError): instance.new_variation_unit_declared("invalid")
def test_init(self): instance = Stats.reset_instance() assert hasattr(instance, "nb_input_files_parsed") assert instance.nb_input_files_parsed == 0 assert hasattr(instance, "nb_units_declared") assert instance.nb_units_declared == 0 assert hasattr(instance, "nb_intents_declared") assert instance.nb_intents_declared == 0 assert hasattr(instance, "nb_slots_declared") assert instance.nb_slots_declared == 0 assert hasattr(instance, "nb_aliases_declared") assert instance.nb_aliases_declared == 0 assert hasattr(instance, "nb_variation_units") assert instance.nb_variation_units == 0 assert hasattr(instance, "nb_variation_intents") assert instance.nb_variation_intents == 0 assert hasattr(instance, "nb_variation_slots") assert instance.nb_variation_slots == 0 assert hasattr(instance, "nb_variation_aliases") assert instance.nb_variation_aliases == 0 assert hasattr(instance, "nb_rules_parsed") assert instance.nb_rules_parsed == 0
def reset_system(cls, *args, **kwargs): Stats.reset_instance() Deprecations.reset_instance() AST.reset_instance() InputFileManager.reset_instance(None) return cls.reset_instance(*args, **kwargs)
def __init__(self): self._alias_definitions = dict() self._slot_definitions = dict() self._intent_definitions = dict() self.stats = Stats.get_or_create()
def test_singleton(self): first = Stats.get_or_create() second = Stats.get_or_create() assert first == second reset = Stats.reset_instance() assert first != reset
def execute(self): """Implements the command `stats`, printing parsing statistics.""" self.print_wrapper.write(str(Stats.get_or_create()))