def is_device_instance_id(value: Optional[str]) -> bool: """Determines whether a value is a device instance ID generated by Windows or not. See https://docs.microsoft.com/it-it/windows-hardware/drivers/install/instance-ids Typical IDs generated by Windows for a same device (but different interfaces) are, as follows: - 8&2F125EC6&0&0003 - 8&2f125ec6&0 - 8&2F125EC6&0&0002 """ return is_undefined_value(value) or INSTANCE_ID_CHARACTER in str(value)
def _parse_pnpid(self, pnpid: str) -> Tuple[str, str]: if is_undefined_value(pnpid): return (UNKNOWN_VALUE, UNKNOWN_VALUE) parts = pnpid.split("\\") if len(parts) >= 2: raw_id = parts[-1] match = PATTERN_UID.search(raw_id) processed_uid = raw_id if match: processed_uid = match.group(1) return (processed_uid.lower(), raw_id) return (UNKNOWN_VALUE, UNKNOWN_VALUE)
def test_is_undefined(self): a_component = get_test_class()() self.assertTrue(a_component.is_undefined) self.assertTrue(is_undefined_value(a_component.component_id)) a_component_with_undefined_values = get_test_class()() undefined_values = {k: generate_undefined_values() for k in a_component.field_names} a_component_with_undefined_values.set_data_values(undefined_values) self.assertTrue(a_component_with_undefined_values.is_undefined) a_defined_component = get_test_class()() valid_values = {k: generate_valid_values() for k in a_component.field_names} a_defined_component.set_data_values(valid_values) self.assertIsNotNone(a_defined_component.component_id) self.assertFalse(a_defined_component.is_undefined)
def instance_id(self) -> str: """Determines what may be the value most likely to be the instance_id of a component as generated by Windows.""" if is_device_instance_id(self.uid): return self.uid return self.uid if is_undefined_value(self.serial_number) else str( self.serial_number)
def contains_genuine_serial_number(self) -> bool: """Contains a genuine serial number and not an instance ID.""" serial_number = self.presumed_serial_number return not (is_undefined_value(serial_number) or is_device_instance_id(serial_number))
def test_is_value_undefined(self): self.assertTrue(is_undefined_value(generate_undefined_values())) self.assertFalse(is_undefined_value(generate_valid_values()))