Ejemplo n.º 1
0
    def build_paths(
            self,
            values_map: CrumbArgsSequences,
            make_crumbs: bool = True) -> [Iterator[str], Iterator['Crumb']]:
        """ Return a list of paths from each tuple of args from `values_map`
        Parameters
        ----------
        values_map: list of sequences of 2-tuple
            Example: [[('subject_id', 'haensel'), ('candy', 'lollipos.path.png')],
                      [('subject_id', 'gretel'),  ('candy', 'jujube.png')],
                     ]

        make_crumbs: bool
            If `make_crumbs` is True will create a Crumb for
            each element of the result.
            Default: True.

        Returns
        -------
        paths: list of str or list of Crumb
        """
        if make_crumbs:
            yield from (self.replace(**dict(val)) for val in values_map)
        else:
            yield from (_build_path(self.path, arg_values=dict(val))
                        for val in values_map)
Ejemplo n.º 2
0
    def set_pattern(self, arg_name: str, arg_regex: str):
        """ Set the pattern `arg_regex` to the given argument `arg_name`."""
        if not _has_arg(self.path, arg_name):
            raise KeyError('Crumb argument {} is not present in {}.'.format(
                arg_name, self))

        self._path = _build_path(self._path,
                                 arg_values={},
                                 with_regex=True,
                                 regexes={arg_name: arg_regex})
Ejemplo n.º 3
0
    def update(self, **kwargs) -> 'Crumb':
        """ Set the crumb arguments in path to the given values in kwargs and update
        self accordingly.
        Parameters
        ----------
        kwargs: strings

        Returns
        -------
        crumb: Crumb
        """
        self._check_args(list(kwargs.keys()), self_args=self.all_args())

        for k, v in kwargs.items():
            if not isinstance(v, str):
                raise ValueError(
                    "Expected a string for the value of argument {}, "
                    "got {}.".format(k, v))

        path = _build_path(self.path, arg_values=kwargs, with_regex=True)
        _check(path)

        self._argval.update(**kwargs)
        return self
Ejemplo n.º 4
0
    def _extend_arg_values(self, arg_values: CrumbArgsSequence, arg_name: str,
                           arg_regex: str,
                           just_dirs: bool) -> CrumbArgsSequences:
        """ Return an extended copy of `arg_values` with valid values for `arg_name`."""
        path = self.path
        vals = []
        for aval in arg_values:
            #  create the part of the crumb path that is already specified
            nupath = _split(_build_path(path, arg_values=dict(aval)))[0]

            # THIS HAPPENS, LEAVE IT. TODO: make a test for this line
            if not os.path.exists(nupath):
                continue

            paths = list_subpaths(nupath,
                                  just_dirs=just_dirs,
                                  ignore=self._ignore,
                                  pattern=arg_regex,
                                  filter_func=self._match_filter)

            #  extend `val` tuples with the new list of values for `aval`
            vals.extend([aval + [(arg_name, sp)] for sp in paths])

        return vals
Ejemplo n.º 5
0
 def path(self) -> str:
     """Return the current crumb path string."""
     return _build_path(self._path,
                        arg_values=self.arg_values,
                        with_regex=True)