예제 #1
0
파일: dataset.py 프로젝트: datalad/datalad
def require_dataset(dataset, check_installed=True, purpose=None):
    """Helper function to resolve a dataset.

    This function tries to resolve a dataset given an input argument,
    or based on the process' working directory, if `None` is given.

    Parameters
    ----------
    dataset : None or path or Dataset
      Some value identifying a dataset or `None`. In the latter case
      a dataset will be searched based on the process working directory.
    check_installed : bool, optional
      If True, an optional check whether the resolved dataset is
      properly installed will be performed.
    purpose : str, optional
      This string will be inserted in error messages to make them more
      informative. The pattern is "... dataset for <STRING>".

    Returns
    -------
    Dataset
      If a dataset could be determined.

    Raises
    ------
    NoDatasetFound
      If not dataset could be determined.
    """
    if dataset is not None and not isinstance(dataset, Dataset):
        dataset = Dataset(dataset)

    if dataset is None:  # possible scenario of cmdline calls
        dspath = get_dataset_root(getpwd())
        if not dspath:
            raise NoDatasetFound(
                "No dataset found at '{}'{}.  Specify a dataset to work with "
                "by providing its path via the `dataset` option, "
                "or change the current working directory to be in a "
                "dataset.".format(
                    getpwd(), " for the purpose {!r}".format(purpose)
                    if purpose else ''))
        dataset = Dataset(dspath)

    assert (dataset is not None)
    lgr.debug(u"Resolved dataset%s: %s",
              u' to {}'.format(purpose) if purpose else '', dataset.path)

    if check_installed and not dataset.is_installed():
        raise NoDatasetFound(f"No installed dataset found at {dataset.path}")

    return dataset
예제 #2
0
 def _flyweight_preproc_path(cls, path):
     """Custom handling for few special abbreviations for datasets"""
     path_ = path
     if path in ('^', '^.'):
         dsroot = get_dataset_root(curdir)
         if dsroot is None:
             raise NoDatasetFound('No dataset contains path: {}'.format(
                 str(Path.cwd())))
         if path == '^':
             # get the topmost dataset from current location. Note that 'zsh'
             # might have its ideas on what to do with ^, so better use as -d^
             path_ = Dataset(dsroot).get_superdataset(topmost=True).path
         elif path == '^.':
             # the dataset containing current directory
             path_ = dsroot
     elif path == '///':
         # TODO: logic/UI on installing a default dataset could move here
         # from search?
         path_ = cfg.obtain('datalad.locations.default-dataset')
     if path != path_:
         lgr.debug("Resolved dataset alias %r to path %r", path, path_)
     return path_