<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _apply_with_plugs(self, subplugs, error_on_unknown):
 """Substitute plugs for placeholders for this phase. Args: subplugs: dict of plug name to plug class, plug classes to replace. error_on_unknown: bool, if True, then error when an unknown plug name is provided. Raises: openhtf.plugs.InvalidPlugError if for one of the plug names one of the following is true: - error_on_unknown is True and the plug name is not registered. - The new plug subclass is not a subclass of the original. - The original plug class is not a placeholder or automatic placeholder. Returns: PhaseDescriptor with updated plugs. """

    plugs_by_name = {plug.name: plug for plug in self.plugs}
    new_plugs = dict(plugs_by_name)

    for name, sub_class in six.iteritems(subplugs):
      original_plug = plugs_by_name.get(name)
      accept_substitute = True
      if original_plug is None:
        if not error_on_unknown:
          continue
        accept_substitute = False
      elif isinstance(original_plug.cls, openhtf.plugs.PlugPlaceholder):
        accept_substitute = issubclass(sub_class, original_plug.cls.base_class)
      else:
        # Check __dict__ to see if the attribute is explicitly defined in the
        # class, rather than being defined in a parent class.
        accept_substitute = ('auto_placeholder' in original_plug.cls.__dict__
                             and original_plug.cls.auto_placeholder
                             and issubclass(sub_class, original_plug.cls))

      if not accept_substitute:
        raise openhtf.plugs.InvalidPlugError(
            'Could not find valid placeholder for substitute plug %s '
            'required for phase %s' % (name, self.name))
      new_plugs[name] = mutablerecords.CopyRecord(original_plug, cls=sub_class)

    return mutablerecords.CopyRecord(
        self,
        plugs=list(new_plugs.values()),
        options=self.options.format_strings(**subplugs),
        measurements=[m.with_args(**subplugs) for m in self.measurements])