Ejemplo n.º 1
0
    def _InsertEffectiveUnit(self, measurement_type, unit):
        """Inserts a unit into this namespace.

    Does not check for uniqueness
    within the namespace.

    If the unit already exists in the measurement, adds a
    DuplicateUnitDefinitionError to the findings and the duplicate is not
    inserted.

    Args:
      measurement_type: Name of the measurement subfield.
      unit: unit object to attempt to insert.
    """
        self.ValidateMeasurementType(measurement_type, unit)
        measurement_units = self._units_by_measurement.setdefault(
            measurement_type, {})
        if unit.name in measurement_units:
            self.AddFinding(
                findings_lib.DuplicateUnitDefinitionError(
                    self, unit, measurement_units[unit.name].file_context))
            return
        measurement_units[unit.name] = unit
        self._units_by_name[unit.name] = unit
        # unit_key is an opaque ID that is unique within the namespace. It is only
        # used by the backward compatibility checking, which needs all of the units
        # to be in a single dict.
        unit_key = '{0}-{1}'.format(measurement_type, unit.name)
        self.units[unit_key] = unit
Ejemplo n.º 2
0
    def testUnitUniverseGetFindings(self):
        context = findings_lib.FileContext('{0}/file.yaml'.format(_GOOD_PATH))
        folder = unit_lib.UnitFolder(_GOOD_PATH)
        folder.AddFinding(
            findings_lib.InconsistentFileLocationError('', context))
        namespace = folder.local_namespace
        namespace.AddFinding(
            findings_lib.DuplicateUnitDefinitionError(
                unit_lib.Unit('unit', 'measurement'), 'namespace'))
        unit = unit_lib.Unit('unit', 'measurement')
        unit.AddFinding(
            findings_lib.UnknownUnitTagError(unit.name, 'tag', context))
        namespace.InsertUnit(unit)
        unit_universe = unit_lib.UnitUniverse([folder])

        findings = unit_universe.GetFindings()

        self.assertLen(findings, 3)
        self.assertTrue(
            unit_universe.HasFindingTypes([
                findings_lib.InconsistentFileLocationError,
                findings_lib.DuplicateUnitDefinitionError,
                findings_lib.UnknownUnitTagError,
            ]))
        self.assertFalse(unit_universe.IsValid())
Ejemplo n.º 3
0
    def InsertUnit(self, unit):
        """Inserts a unit into this namespace.

    If the unit already exists in the namespace, adds a
    DuplicateUnitDefinitionError to the findings and the duplicate is not
    inserted.

    Args:
      unit: unit object to attempt to insert.
    """
        self.ValidateMeasurementType(unit)
        if unit.name in self.units:
            self.AddFinding(
                findings_lib.DuplicateUnitDefinitionError(
                    unit, self.namespace))
            return
        self.units[unit.name] = unit
Ejemplo n.º 4
0
    def InsertUnit(self, measurement_type, unit):
        """Inserts a unit into this namespace.

    If the unit already exists in the global namespace, adds a
    DuplicateUnitDefinitionError to the findings and the duplicate is not
    inserted. If the unit is being inserted into a namespace other than the
    global namespace, an InvalidUnitNamespaceError will be added to findings.

    Args:
      measurement_type: Name of the measurement subfield.
      unit: unit object to attempt to insert.
    """
        if unit.name != 'no_units' and unit.name in self._units_by_name:
            self.AddFinding(
                findings_lib.DuplicateUnitDefinitionError(
                    self, unit, self._units_by_name[unit.name].file_context))
            return
        # Assert namespace is global namespace otherwise add finding.
        elif self.parent_namespace is not None:
            self.AddFinding(
                findings_lib.InvalidUnitNamespaceError(self.namespace,
                                                       unit.file_context))
        self._InsertEffectiveUnit(measurement_type, unit)