code: def _install_subds_from_flexible_source(ds, sm, **kwargs):
      """Tries to obtain a given subdataset from several meaningful locations
  
      Parameters
      ----------
      ds : Dataset
        Parent dataset of to-be-installed subdataset.
      sm : dict
        Submodule record as produced by `subdatasets()`.
      **kwargs
        Passed onto clone()
      """
      sm_path = op.relpath(sm["path"], start=sm["parentds"])
      # compose a list of candidate clone URLs
      clone_urls = _get_flexible_source_candidates_for_submodule(ds, sm)
  
      # prevent inevitable exception from `clone`
      dest_path = op.join(ds.path, sm_path)
      clone_urls_ = [
          src["url"] for src in clone_urls if src["url"] != dest_path
      ]
  
      if not clone_urls:
          # yield error
          yield get_status_dict(
              action="install",
              ds=ds,
              status="error",
              message=(
                  "Have got no candidates to install subdataset %s from.",
                  sm_path,
              ),
              logger=lgr,
          )
          return
  
      for res in clone_dataset(
          clone_urls_,
          Dataset(dest_path),
          cfg=ds.config,
          checkout_gitsha=sm["gitshasum"],
          **kwargs,
      ):
          if (
              res.get("action", None) == "install"
              and res.get("status", None) == "ok"
              and res.get("type", None) == "dataset"
              and res.get("path", None) == dest_path
          ):
              _fixup_submodule_dotgit_setup(ds, sm_path)
  
              section_name = "submodule.{}".format(sm["gitmodule_name"])
              # register the submodule as "active" in the superdataset
              ds.config.set(
                  "{}.active".format(section_name),
                  "true",
                  reload=False,
                  force=True,
                  scope="local",
              )
              ds.config.set(
                  "{}.url".format(section_name),
                  # record the actual source URL of the successful clone
                  # and not a funky prediction based on the parent ds
                  # like ds.repo.update_submodule() would do (does not
                  # accept a URL)
                  res["source"]["giturl"],
                  reload=True,
                  force=True,
                  scope="local",
              )
          yield res
  
      subds = Dataset(dest_path)
      if not subds.is_installed():
          lgr.debug(
              "Desired subdataset %s did not materialize, stopping",
              subds,
          )
          return
  
      # check whether clone URL generators were involved
      cand_cfg = [
          rec for rec in clone_urls if rec.get("from_config", False)
      ]
      if cand_cfg:
          # get a handle on the configuration that is specified in the
          # dataset itself (local and dataset)
          super_cfg = ConfigManager(dataset=ds, source="branch-local")
          need_reload = False
          for rec in cand_cfg:
              # check whether any of this configuration originated from the
              # superdataset. if so, inherit the config in the new subdataset
              # clone unless that config is already specified in the new
              # subdataset which can happen during postclone_cfg routines.
              # if not, keep things clean in order to be able to move with any
              # outside configuration change
              for c in (
                  "datalad.get.subdataset-source-candidate-{}{}".format(
                      rec["cost"], rec["name"]
                  ),
                  "datalad.get.subdataset-source-candidate-{}".format(
                      rec["name"]
                  ),
              ):
                  if (
                      c in super_cfg.keys()
                      and c not in subds.config.keys()
                  ):
                      subds.config.set(
                          c,
                          super_cfg.get(c),
                          scope="local",
                          reload=False,
                      )
                      need_reload = True
                      break
          if need_reload:
              subds.config.reload(force=True)
  
imports: import logging
  import re
  import os.path
  from datalad.config import ConfigManager
  from datalad.interface.base import Interface, eval_results
  from datalad.interface.base import build_doc
  from datalad.interface.results import get_status_dict, results_from_paths, annexjson2result, success_status_map, results_from_annex_noinfo
  from datalad.interface.common_opts import recursion_flag, location_description, jobs_opt, reckless_opt
  from datalad.interface.results import is_ok_dataset
  from datalad.support.constraints import EnsureInt, EnsureChoice, EnsureStr, EnsureNone
  from datalad.support.collections import ReadOnlyDict
  from datalad.support.param import Parameter
  from datalad.support.annexrepo import AnnexRepo
  from datalad.support.gitrepo import GitRepo, _fixup_submodule_dotgit_setup
  from datalad.support.exceptions import CapturedException, CommandError, InsufficientArgumentsError
  from datalad.support.network import URL, RI, urlquote
  from datalad.support.parallel import ProducerConsumerProgressLog
  from datalad.utils import unique, Path, get_dataset_root, shortened_repr
  from datalad.local.subdatasets import Subdatasets
  from datalad.distribution.dataset import Dataset, EnsureDataset, datasetmethod, require_dataset
  from datalad.core.distributed.clone import clone_dataset
  from datalad.distribution.utils import _get_flexible_source_candidates
label_desc: Use image node_id 3 for calling a global function with example usage: _install_subds_from_flexible_source(ds, sm) without return types