Beispiel #1
0
def _expression_or_file_list(expression=None, file_list=None):
    if isinstance(expression, te.Expression) or expression is None:
        pass
    elif fu.is_directory(expression):
        file_list = _file_list(expression=expression, sort=True)
        expression = None
    elif isinstance(expression, str):
        expression = te.Expression(expression)
    else:
        raise ValueError('The expression %r is not valid!' % expression)

    if file_list is not None and not isinstance(file_list, list):
        raise ValueError('The file_list %r is not a list or None!' % file_list)

    if expression is None and file_list is None:
        raise ValueError('Expresson and file_list cannot both be None!')

    return expression, file_list
Beispiel #2
0
def _file_list(expression=None, file_list=None, sort=True, verbose=False):
    """Returns the list of files that match the tag expression.
  
  Arguments
  ---------
  expression :str
    The regular expression the file names should match.
  sort : bool
    If True, sort files naturally.
  verbose : bool
    If True, print warning if no files exists.
  
  Returns
  -------
  file_list : list of str
    The list of files that matched the expression.
  """
    if isinstance(file_list, list):
        return file_list

    if isinstance(expression, te.Expression):
        fl = glob.glob(expression.glob())
    elif fu.is_directory(expression):
        expression = fu.join(expression, '*')
        fl = glob.glob(expression)
    else:
        e = te.Expression(expression)
        fl = glob.glob(e.glob())

    if verbose and len(fl) == 0:
        warnings.warn('No files found matching %s !' % expression)
        return []

    if sort:
        fl = natsort.natsorted(fl)

    return fl
Beispiel #3
0
def _expression_and_file_list(expression=None, file_list=None):
    if isinstance(expression, te.Expression) or expression is None:
        pass
    elif fu.is_directory(expression):
        if file_list is None:
            file_list = glob.glob(fu.join(expression, '*'))
        expression = te.detect(file_list)
    elif isinstance(expression, (str, list)):
        expression = te.Expression(expression)
    else:
        raise ValueError('Expression %r is not valid!' % expression)

    if file_list is None:
        if expression is None:
            raise ValueError(
                'Either expression or file_list need to be specified!')
        file_list = glob.glob(expression.glob())
    elif isinstance(file_list, list):
        if expression is None:
            expression = te.detect(file_list)
    else:
        raise ValueError('The file_list %r is not value!' % file_list)

    return expression, file_list
Beispiel #4
0
def is_file_list(expression,
                 exists=False,
                 tag_names=None,
                 n_tags=-1,
                 verbose=False):
    """Checks if the expression is a valid file list.
  
  Arguments
  ---------
  expression : str
    The regular expression to check.
  exists : bool
    If True, check if at least one file exists.
  tag_names :  list of str or None
    List of tag names expected to be present in the expression.
  n_Tags : int or None
    Number of tags to expect.
  verbose : bool
    If True, print reason why the epxression does not represent the desired file list.
    
  Returns
  -------
  is_expression : bool
    Returns True if the expression fullfills the desired criteria and at least one file matching the expression exists.
  """
    if isinstance(expression, Source):
        return True

    if not isinstance(expression, (str, te.Expression)):
        if verbose:
            warnings.warn(
                'The expression %r is not a string or valid Source!' %
                expression)
        return False

    if fu.is_directory(expression):
        if exists:
            if len(os.listdir(expression)) == 0:
                if verbose:
                    warnings.warn('No files exists in the directory %s!' %
                                  expression)
                return False
            else:
                return True
        else:
            return True

    if tag_names is not None or n_tags is not None:
        t = te.Expression(expression) if not isinstance(
            expression, te.Expression) else expression
        if n_tags is not None:
            if n_tags < 0 and -n_tags > t.ntags():
                if verbose:
                    warnings.warn(
                        'Expression has not required number %d of tags, but %d!'
                        % (n_tags, t.ntags()))
                return False
            elif n_tags >= 0 and n_tags != t.ntags():
                if verbose:
                    warnings.warn(
                        'Expression has not required number %d of tags, but %d!'
                        % (n_tags, t.ntags()))
                return False

        if tag_names is not None:
            if tag_names != t.tag_names():
                if verbose:
                    warnings.warn(
                        'Expression has not required tags %r, but %r!' %
                        (tag_names, t.tag_names()))
                return False

    if exists:
        f = _first_file(expression)
        if f is None:
            if verbose:
                warnings.warn('Expression does not point to any files!')
            return False

    return True