def IsFieldDefined(self, fieldname: str, namespace_name: str) -> bool: """Returns true if fieldname is defined within the namespace. If the field ends with a digit, it is removed to check if it exists without it Args: fieldname: string. Name of a field, optionally with increment. namespace_name: string. """ fieldname_part, _ = entity_type_lib.SeparateFieldIncrement(fieldname) return fieldname_part in { field.name for field in self._namespace_map.get(namespace_name, []) }
def GetStatesByField(self, field_name: str) -> List[str]: """Returns a list of possible state strings for a field. TODO(b/188242279) handle namespacing for states correctly. Args: field_name: a fully qualifited field string. Returns: State string exactly as it was defined in the ontology config """ if not self.state_universe_reverse_map: print('StateUniverse undefined in ConfigUniverse') return None namespace, raw_field = entity_type_lib.SeparateFieldNamespace(field_name) std_field, _ = entity_type_lib.SeparateFieldIncrement(raw_field) return self.state_universe_reverse_map.get(namespace + '/' + std_field)
def _GetAllowedField( universe: pvt.ConfigUniverse, as_written_field_name: str, entity_type: Optional[entity_type_lib.EntityType] = None ) -> Optional[str]: """Returns the most likely qualified field name given the provided context. If an entity type is provided, the method validates that the field is valid for the type. If no type is provided, method will validate that the field exists given the amount of qualification provided. For instance, a fully qualified field `HVAC/run_status` would be validated against the HVAC namespace's fields, but `run_status` would be validated against the global namespace. Shorthand syntax will be interpreted correctly when the type is applied. For instance `run_status` shorthand (an unqualified field reference) would correctly validate against the HVAC namespace if the field was defined in that namespace and the provided type was a HVAC type. Args: universe: the ConfigUniverse to validate against as_written_field_name: the field name string as written in the config entity_type: the EntityType of the entity the field is deifned on """ # Field could be qualified or unqualified in the config. We want to know if entity_type and not entity_type.allow_undefined_fields: field_obj = entity_type.GetFieldFromConfigText(as_written_field_name) if field_obj: return entity_type_lib.BuildQualifiedField(field_obj) else: return None try: namespace, field_name = entity_type_lib.SeparateFieldNamespace( as_written_field_name) except TypeError: namespace = '' field_name = as_written_field_name std_field_name, _ = entity_type_lib.SeparateFieldIncrement(field_name) if universe.field_universe.IsFieldDefined(std_field_name, namespace): return namespace + '/' + as_written_field_name return None
def testParseFieldWithoutIncrement(self): field_name, increment = entity_type_lib.SeparateFieldIncrement( 'zone_occupancy_status') self.assertEqual(field_name, 'zone_occupancy_status') self.assertEqual(increment, '')