# Buggy code
def GetEntries(
    self, parser_mediator, key=None, registry_type=None, codepage="cp1252", **kwargs
):
    """Collect Values under USBStor and return an event object for each one.

    Args:
      parser_mediator: A parser mediator object (instance of ParserMediator).
      key: Optional Registry key (instance of winreg.WinRegKey).
           The default is None.
      registry_type: Optional Registry type string. The default is None.
    """
    for subkey in key.GetSubkeys():
        text_dict = {}
        text_dict["subkey_name"] = subkey.name

        # Time last USB device of this class was first inserted.
        event_object = windows_events.WindowsRegistryEvent(
            subkey.last_written_timestamp,
            key.path,
            text_dict,
            usage=eventdata.EventTimestamp.FIRST_CONNECTED,
            offset=key.offset,
            registry_type=registry_type,
            source_append=": USBStor Entries",
        )
        parser_mediator.ProduceEvent(event_object)

        # TODO: Determine if these 4 fields always exist.
        try:
            device_type, vendor, product, revision = subkey.name.split("&")
        except ValueError as exception:
            logging.warning(
                "Unable to split string: {0:s} with error: {1:s}".format(
                    subkey.name, exception
                )
            )

        text_dict["device_type"] = device_type
        text_dict["vendor"] = vendor
        text_dict["product"] = product
        text_dict["revision"] = revision

        for devicekey in subkey.GetSubkeys():
            text_dict["serial"] = devicekey.name

            friendly_name_value = devicekey.GetValue("FriendlyName")
            if friendly_name_value:
                text_dict["friendly_name"] = friendly_name_value.data
            else:
                text_dict.pop("friendly_name", None)

            # ParentIdPrefix applies to Windows XP Only.
            parent_id_prefix_value = devicekey.GetValue("ParentIdPrefix")
            if parent_id_prefix_value:
                text_dict["parent_id_prefix"] = parent_id_prefix_value.data
            else:
                text_dict.pop("parent_id_prefix", None)

            # Win7 - Last Connection.
            # Vista/XP - Time of an insert.
            event_object = windows_events.WindowsRegistryEvent(
                devicekey.last_written_timestamp,
                key.path,
                text_dict,
                usage=eventdata.EventTimestamp.LAST_CONNECTED,
                offset=key.offset,
                registry_type=registry_type,
                source_append=": USBStor Entries",
            )
            parser_mediator.ProduceEvent(event_object)

            # Build list of first Insertion times.
            first_insert = []
            device_parameter_key = devicekey.GetSubkey("Device Parameters")
            if device_parameter_key:
                first_insert.append(device_parameter_key.last_written_timestamp)

            log_configuration_key = devicekey.GetSubkey("LogConf")
            if (
                log_configuration_key
                and log_configuration_key.last_written_timestamp not in first_insert
            ):
                first_insert.append(log_configuration_key.last_written_timestamp)

            properties_key = devicekey.GetSubkey("Properties")
            if (
                properties_key
                and properties_key.last_written_timestamp not in first_insert
            ):
                first_insert.append(properties_key.last_written_timestamp)

            # Add first Insertion times.
            for timestamp in first_insert:
                event_object = windows_events.WindowsRegistryEvent(
                    timestamp,
                    key.path,
                    text_dict,
                    usage=eventdata.EventTimestamp.LAST_CONNECTED,
                    offset=key.offset,
                    registry_type=registry_type,
                    source_append=": USBStor Entries",
                )
                parser_mediator.ProduceEvent(event_object)


# Traceback (UnboundLocalError)
2015-02-15 20:50:31,310 [WARNING] (Worker_1  ) PID:3764 <usbstor> Unable to split string: Disk&amp;Ven_Kingston&amp;Prod_DT_Rubber_3.0 with error: need more than 3 values to unpack
2015-02-15 20:50:31,313 [WARNING] (Worker_1  ) PID:3764 <worker> [winreg] Unable to process file: type: OS, location: /cases/svo1/HD/20140823_SVO1_HD.E01
type: EWF
type: TSK_PARTITION, location: /p2, part index: 3, start offset: 0x06500000
type: VSHADOW, store index: 0
type: TSK, inode: 44703, location: /temp/system
with error: local variable 'device_type' referenced before assignment.
2015-02-15 20:50:31,313 [ERROR] (Worker_1  ) PID:3764 <worker> local variable 'device_type' referenced before assignment
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/plaso/engine/worker.py", line 123, in _ParseFileEntryWithParser
parser_object.Parse(self._parser_context, file_entry)
File "/usr/lib/python2.7/dist-packages/plaso/parsers/winreg.py", line 331, in Parse
codepage=parser_context.codepage, parser_chain=parser_chain)
File "/usr/lib/python2.7/dist-packages/plaso/parsers/winreg_plugins/interface.py", line 206, in Process
parser_chain=parser_chain, **kwargs)
File "/usr/lib/python2.7/dist-packages/plaso/parsers/winreg_plugins/usbstor.py", line 75, in GetEntries
text_dict['device_type'] = device_type
UnboundLocalError: local variable 'device_type' referenced before assignment

# Fixed code
def GetEntries(
    self, parser_mediator, key=None, registry_type=None, codepage="cp1252", **kwargs
):
    """Collect Values under USBStor and return an event object for each one.

    Args:
      parser_mediator: A parser mediator object (instance of ParserMediator).
      key: Optional Registry key (instance of winreg.WinRegKey).
           The default is None.
      registry_type: Optional Registry type string. The default is None.
    """
    for subkey in key.GetSubkeys():
        text_dict = {}
        text_dict["subkey_name"] = subkey.name

        # Time last USB device of this class was first inserted.
        event_object = windows_events.WindowsRegistryEvent(
            subkey.last_written_timestamp,
            key.path,
            text_dict,
            usage=eventdata.EventTimestamp.FIRST_CONNECTED,
            offset=key.offset,
            registry_type=registry_type,
            source_append=": USBStor Entries",
        )
        parser_mediator.ProduceEvent(event_object)

        name_values = subkey.name.split("&")
        number_of_name_values = len(name_values)

        # Normally we expect 4 fields here however that is not always the case.
        if number_of_name_values != 4:
            logging.warning(
                "Expected 4 &-separated values in: {0:s}".format(subkey.name)
            )

        if number_of_name_values >= 1:
            text_dict["device_type"] = name_values[0]
        if number_of_name_values >= 2:
            text_dict["vendor"] = name_values[1]
        if number_of_name_values >= 3:
            text_dict["product"] = name_values[2]
        if number_of_name_values >= 4:
            text_dict["revision"] = name_values[3]

        for devicekey in subkey.GetSubkeys():
            text_dict["serial"] = devicekey.name

            friendly_name_value = devicekey.GetValue("FriendlyName")
            if friendly_name_value:
                text_dict["friendly_name"] = friendly_name_value.data
            else:
                text_dict.pop("friendly_name", None)

            # ParentIdPrefix applies to Windows XP Only.
            parent_id_prefix_value = devicekey.GetValue("ParentIdPrefix")
            if parent_id_prefix_value:
                text_dict["parent_id_prefix"] = parent_id_prefix_value.data
            else:
                text_dict.pop("parent_id_prefix", None)

            # Win7 - Last Connection.
            # Vista/XP - Time of an insert.
            event_object = windows_events.WindowsRegistryEvent(
                devicekey.last_written_timestamp,
                key.path,
                text_dict,
                usage=eventdata.EventTimestamp.LAST_CONNECTED,
                offset=key.offset,
                registry_type=registry_type,
                source_append=": USBStor Entries",
            )
            parser_mediator.ProduceEvent(event_object)

            # Build list of first Insertion times.
            first_insert = []
            device_parameter_key = devicekey.GetSubkey("Device Parameters")
            if device_parameter_key:
                first_insert.append(device_parameter_key.last_written_timestamp)

            log_configuration_key = devicekey.GetSubkey("LogConf")
            if (
                log_configuration_key
                and log_configuration_key.last_written_timestamp not in first_insert
            ):
                first_insert.append(log_configuration_key.last_written_timestamp)

            properties_key = devicekey.GetSubkey("Properties")
            if (
                properties_key
                and properties_key.last_written_timestamp not in first_insert
            ):
                first_insert.append(properties_key.last_written_timestamp)

            # Add first Insertion times.
            for timestamp in first_insert:
                event_object = windows_events.WindowsRegistryEvent(
                    timestamp,
                    key.path,
                    text_dict,
                    usage=eventdata.EventTimestamp.LAST_CONNECTED,
                    offset=key.offset,
                    registry_type=registry_type,
                    source_append=": USBStor Entries",
                )
                parser_mediator.ProduceEvent(event_object)