SPPAS 4.22

https://sppas.org/

Module sppas.src.wkps

Class sppasFileDataFilters

Description

The 'SPPAS file data filter system'.

Search in file data.

Example
Example
 >>> # Search for all checked TextGrid files in a path containing 'corpus'
 >>> f = sppasFileDataFilters(data)
 >>> f.path(contains='corpus') & f.file(state=True) & f.extension(iexact='textgrid')

Constructor

Create a sppasFileDataFilters instance.

Parameters
  • obj: (sppasWorkspace) The object to be filtered.
View Source
def __init__(self, obj):
    """Create a sppasFileDataFilters instance.

    :param obj: (sppasWorkspace) The object to be filtered.

    """
    super(sppasFileDataFilters, self).__init__(obj)

Public functions

path

Apply functions on all paths of the object.

Each argument is made of a function name and its expected value. Each function can be prefixed with 'not_', like in the next example.

Example
Example
 >>> f.path(startswith="c:\users\myname", not_endswith='a', logic_bool="and")
 >>> f.path(startswith="c:\users\myname") & f.path(not_endswith='a')
 >>> f.path(startswith="c:\users\myname") | f.path(startswith="ta")
Parameters
  • kwargs: logic_bool/any sppasPathCompare() method.
Returns
  • (sppasDataSet) Set of FileName() instances
View Source
def path(self, **kwargs):
    """Apply functions on all paths of the object.

        Each argument is made of a function name and its expected value.
        Each function can be prefixed with 'not_', like in the next example.

        :Example:

            >>> f.path(startswith="c:\\users\\myname", not_endswith='a', logic_bool="and")
            >>> f.path(startswith="c:\\users\\myname") & f.path(not_endswith='a')
            >>> f.path(startswith="c:\\users\\myname") | f.path(startswith="ta")

        :param kwargs: logic_bool/any sppasPathCompare() method.
        :returns: (sppasDataSet) Set of FileName() instances

        """
    comparator = sppasFileBaseCompare()
    sppasBaseFilters.test_args(comparator, **kwargs)
    logic_bool = sppasBaseFilters.fix_logic_bool(**kwargs)
    path_fct_values = sppasBaseFilters.fix_function_values(comparator, **kwargs)
    path_functions = sppasBaseFilters.fix_functions(comparator, **kwargs)
    data = sppasBaseSet()
    for path in self.obj.get_paths():
        is_matching = path.match(path_functions, logic_bool)
        if is_matching is True:
            for fr in path:
                for fn in fr:
                    data.append(fn, path_fct_values)
    return data

root

Apply functions on all roots of the object.

Each argument is made of a function name and its expected value. Each function can be prefixed with 'not_', like in the next example.

Example
Example
 >>> f.root(startswith="myfile", not_endswith='a', logic_bool="and")
 >>> f.root(startswith="myfile") & f.root(not_endswith='a')
 >>> f.root(startswith="myfile") | f.root(startswith="ta")
Parameters
  • kwargs: logic_bool/any sppasRootCompare() method.
Returns
  • (sppasDataSet) Set of FileName() instances
View Source
def root(self, **kwargs):
    """Apply functions on all roots of the object.

        Each argument is made of a function name and its expected value.
        Each function can be prefixed with 'not_', like in the next example.

        :Example:

            >>> f.root(startswith="myfile", not_endswith='a', logic_bool="and")
            >>> f.root(startswith="myfile") & f.root(not_endswith='a')
            >>> f.root(startswith="myfile") | f.root(startswith="ta")

        :param kwargs: logic_bool/any sppasRootCompare() method.
        :returns: (sppasDataSet) Set of FileName() instances

        """
    comparator = sppasFileBaseCompare()
    sppasBaseFilters.test_args(comparator, **kwargs)
    logic_bool = sppasBaseFilters.fix_logic_bool(**kwargs)
    path_fct_values = sppasBaseFilters.fix_function_values(comparator, **kwargs)
    path_functions = sppasBaseFilters.fix_functions(comparator, **kwargs)
    data = sppasBaseSet()
    for path in self.obj.get_paths():
        for root in path:
            is_matching = root.match(path_functions, logic_bool)
            if is_matching is True:
                for fn in root:
                    data.append(fn, path_fct_values)
    return data

file

Apply functions on all files of the object.

Each argument is made of a function name and its expected value. Each function can be prefixed with 'not_', like in the next example.

  • Examples
Example
 >>> f.file(state=State().UNUSED)
 >>> f.file(contains="dlg")
Parameters
  • kwargs: logic_bool/any sppasFileStateCompare() method.
Returns
  • (sppasDataSet)
View Source
def file(self, **kwargs):
    """Apply functions on all files of the object.

        Each argument is made of a function name and its expected value.
        Each function can be prefixed with 'not_', like in the next example.

        :Examples:

            >>> f.file(state=State().UNUSED)
            >>> f.file(contains="dlg")

        :param kwargs: logic_bool/any sppasFileStateCompare() method.
        :returns: (sppasDataSet)

        """
    comparator = sppasFileBaseCompare()
    return self.__search_fn(comparator, **kwargs)

name

Apply functions on all names of the files of the object.

Each argument is made of a function name and its expected value. Each function can be prefixed with 'not_', like in the next example.

Example
Example
 >>> f.name(iexact="myfile-phon", not_startswith='a', logic_bool="and")
 >>> f.name(iexact="myfile-phon") & f.name(not_startswith='a')
 >>> f.name(iexact="myfile-phon") | f.name(startswith="ta")
Parameters
  • kwargs: logic_bool/any sppasFileNameCompare() method.
Returns
  • (sppasDataSet) Set of FileName() instances
View Source
def name(self, **kwargs):
    """Apply functions on all names of the files of the object.

        Each argument is made of a function name and its expected value.
        Each function can be prefixed with 'not_', like in the next example.

        :Example:

            >>> f.name(iexact="myfile-phon", not_startswith='a', logic_bool="and")
            >>> f.name(iexact="myfile-phon") & f.name(not_startswith='a')
            >>> f.name(iexact="myfile-phon") | f.name(startswith="ta")

        :param kwargs: logic_bool/any sppasFileNameCompare() method.
        :returns: (sppasDataSet) Set of FileName() instances

        """
    comparator = sppasFileNameCompare()
    return self.__search_fn(comparator, **kwargs)

extension

Apply functions on all extensions of the files of the object.

Each argument is made of a function name and its expected value. Each function can be prefixed with 'not_', like in the next example.

Example
Example
 >>> f.extension(startswith=".TEXT", not_endswith='a', logic_bool="and")
 >>> f.extension(startswith=".TEXT") & f.extension(not_endswith='a')
 >>> f.extension(startswith=".TEXT") | f.extension(startswith="ta")
Parameters
  • kwargs: logic_bool/any sppasFileExtCompare() method.
Returns
  • (sppasDataSet)
View Source
def extension(self, **kwargs):
    """Apply functions on all extensions of the files of the object.

        Each argument is made of a function name and its expected value.
        Each function can be prefixed with 'not_', like in the next example.

        :Example:

            >>> f.extension(startswith=".TEXT", not_endswith='a', logic_bool="and")
            >>> f.extension(startswith=".TEXT") & f.extension(not_endswith='a')
            >>> f.extension(startswith=".TEXT") | f.extension(startswith="ta")

        :param kwargs: logic_bool/any sppasFileExtCompare() method.
        :returns: (sppasDataSet)

        """
    comparator = sppasFileExtCompare()
    return self.__search_fn(comparator, **kwargs)

ref

Apply functions on all file properties of the object.

Return any fileroot for which at least one of its references is matching the filter.

Example
Example
 >>> f.ref(startswith="toto", endswith="tutu", logic_bool="and")
 >>> f.ref(startswith="toto") & f.ref(endswith="tutu")
 >>> f.ref(startswith="toto") | f.ref(contains="tutu")
Parameters
  • kwargs: logic_bool/any sppasFileStateCompare() method.
Returns
  • (sppasDataSet) Set of FileName() instances
View Source
def ref(self, **kwargs):
    """Apply functions on all file properties of the object.

        Return any fileroot for which at least one of its references is
        matching the filter.

        :Example:

            >>> f.ref(startswith="toto", endswith="tutu", logic_bool="and")
            >>> f.ref(startswith="toto") & f.ref(endswith="tutu")
            >>> f.ref(startswith="toto") | f.ref(contains="tutu")

        :param kwargs: logic_bool/any sppasFileStateCompare() method.
        :returns: (sppasDataSet) Set of FileName() instances

        """
    comparator = sppasFileRefCompare()
    sppasBaseFilters.test_args(comparator, **kwargs)
    logic_bool = sppasBaseFilters.fix_logic_bool(**kwargs)
    ref_fct_values = sppasBaseFilters.fix_function_values(comparator, **kwargs)
    ref_functions = sppasBaseFilters.fix_functions(comparator, **kwargs)
    data = sppasBaseSet()
    for path in self.obj.get_paths():
        for fr in path:
            for ref in fr.references:
                is_matching = ref.match(ref_functions, logic_bool)
                if is_matching is True:
                    for fn in fr:
                        data.append(fn, ref_fct_values)
    return data

att

Apply functions on attributes of references of files of the object.

Each argument is made of a function name and its expected value. Each function can be prefixed with 'not_', like in the next example.

The given value is a tuple with (identifier, value) of the attribute.

Example
Example
 >>> f.att(equals=("age", "14"))
Parameters
  • kwargs: logic_bool/any sppasAttCompare() method.
Returns
  • (sppasDataSet) Set of FileName() instances
View Source
def att(self, **kwargs):
    """Apply functions on attributes of references of files of the object.

        Each argument is made of a function name and its expected value.
        Each function can be prefixed with 'not_', like in the next example.

        The given value is a tuple with (identifier, value) of the attribute.

        :Example:

        >>> f.att(equals=("age", "14"))

        :param kwargs: logic_bool/any sppasAttCompare() method.
        :returns: (sppasDataSet) Set of FileName() instances

        """
    comparator = sppasRefAttributeCompare()
    sppasBaseFilters.test_args(comparator, **kwargs)
    logic_bool = sppasBaseFilters.fix_logic_bool(**kwargs)
    att_fct_values = sppasBaseFilters.fix_function_values(comparator, **kwargs)
    att_functions = sppasBaseFilters.fix_functions(comparator, **kwargs)
    data = sppasBaseSet()
    for fp in self.obj.get_paths():
        for fr in fp:
            matches = [False] * len(att_functions)
            for ref in fr.get_references():
                for i, a in enumerate(att_functions):
                    func, value, logical_not = a
                    for att in ref:
                        mm = False
                        try:
                            searched = sppasRefAttribute(value[0], value[1], att.get_value_type())
                            if att.get_id() == searched.get_id():
                                if logical_not is True:
                                    mm = not func(att, searched.get_typed_value())
                                else:
                                    mm = func(att, searched.get_typed_value())
                            if mm is True:
                                matches[i] = True
                                break
                        except ValueError:
                            continue
            if logic_bool == 'and':
                is_matching = all(matches)
            else:
                is_matching = any(matches)
            if is_matching is True:
                for fn in fr:
                    data.append(fn, att_fct_values)
    return data

Protected functions

__search_fn

Apply functions on files.

Each argument is made of a function name and its expected value. Each function can be prefixed with 'not_', like in the next example.

Parameters
  • comparator: (sppasBaseCompare)
  • kwargs: logic_bool/any sppasFileNameCompare() method.
Returns
  • (sppasDataSet) Set of FileName() instances
View Source
def __search_fn(self, comparator, **kwargs):
    """Apply functions on files.

        Each argument is made of a function name and its expected value.
        Each function can be prefixed with 'not_', like in the next example.

        :param comparator: (sppasBaseCompare)
        :param kwargs: logic_bool/any sppasFileNameCompare() method.
        :returns: (sppasDataSet) Set of FileName() instances

        """
    sppasBaseFilters.test_args(comparator, **kwargs)
    logic_bool = sppasBaseFilters.fix_logic_bool(**kwargs)
    path_fct_values = sppasBaseFilters.fix_function_values(comparator, **kwargs)
    path_functions = sppasBaseFilters.fix_functions(comparator, **kwargs)
    data = sppasBaseSet()
    for path in self.obj.get_paths():
        for fr in path:
            for fn in fr:
                is_matching = fn.match(path_functions, logic_bool)
                if is_matching is True:
                    data.append(fn, path_fct_values)
    return data