Skip to content

API Reference

findtools.find_files

findtools.find_files

FileTypeCondition

Bases: object

Source code in src/findtools/find_files.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class FileTypeCondition(object):

    def __init__(self, filetype):
        if filetype in filetypes.keys():
            filetype = filetypes[filetype]
        self.type = filetype

    def match(self, pathname):
        '''
        Match type of the file system entry to satisfy a least of types.
        '''
        if self.type == 'directory' and not os.path.isdir(pathname):
            return False
        if self.type == 'file' and not os.path.isfile(pathname):
            return False
        if self.type == 'link' and not os.path.islink(pathname):
            return False
        return True

match(pathname)

Match type of the file system entry to satisfy a least of types.

Source code in src/findtools/find_files.py
43
44
45
46
47
48
49
50
51
52
53
def match(self, pathname):
    '''
    Match type of the file system entry to satisfy a least of types.
    '''
    if self.type == 'directory' and not os.path.isdir(pathname):
        return False
    if self.type == 'file' and not os.path.isfile(pathname):
        return False
    if self.type == 'link' and not os.path.islink(pathname):
        return False
    return True

Match

Bases: MatchAllPatternsAndTypes

Left for backwards compatibility. Use MatchPatterns which is more general.

Source code in src/findtools/find_files.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
class Match(MatchAllPatternsAndTypes):
    '''
    Left for backwards compatibility. Use MatchPatterns which is more general.
    '''

    def __init__(self, filetype=None, name=None):
        '''
        @param filetype: flag or name of the file type used as matching
                         condition

        @param name: a string for fnmatch pattern to match file name or
                     compiled regexp object.
        '''
        if filetype is None and name is None:
            raise ValueError('Either filetype or name argument is expected.')
        super(Match, self).__init__(
            filetypes=None if filetype is None else [filetype],
            names=None if name is None else [name],
        )

__init__(filetype=None, name=None)

@param filetype: flag or name of the file type used as matching condition

@param name: a string for fnmatch pattern to match file name or compiled regexp object.

Source code in src/findtools/find_files.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
def __init__(self, filetype=None, name=None):
    '''
    @param filetype: flag or name of the file type used as matching
                     condition

    @param name: a string for fnmatch pattern to match file name or
                 compiled regexp object.
    '''
    if filetype is None and name is None:
        raise ValueError('Either filetype or name argument is expected.')
    super(Match, self).__init__(
        filetypes=None if filetype is None else [filetype],
        names=None if name is None else [name],
    )

MatchAllPatternsAndTypes

Bases: object

Source code in src/findtools/find_files.py
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class MatchAllPatternsAndTypes(object):

    def __init__(self, filetypes=None, names=None):
        '''
        @param filetypes: a list of flags or names of the file type used as
                          matching condition

        @param names: a list of strings for fnmatch pattern to match file name
                      or compiled regexp object. A string surrounded with '/'
                      is interpreted as a regexp.
        '''
        # We need at least one of the criteria.
        assert names is not None or filetypes is not None
        # File type conditions.
        self.filetype_conditions = list()
        if filetypes is not None:
            assert type(filetypes) == list
            for filetype in filetypes:
                if isinstance(filetype, str):
                    filetype_condition = FileTypeCondition(filetype)
                    self.filetype_conditions.append(filetype_condition)
                else:
                    # Assume FileTypeCondition-like instance.
                    self.filetype_conditions.append(filetype)
        # File name conditions.
        self.name_patterns = list()
        if names is not None:
            assert type(names) == list
            for name in names:
                if isinstance(name, str):
                    if self.is_a_regexp(name):
                        # Don't forget to cut the '/' wrapping, which is
                        # useless for python re syntaxes.
                        self.name_patterns.append(re.compile(name[1:-1]))
                    else:
                        self.name_patterns.append(
                            self.compile_fnmatch_pattern(name)
                        )
                else:
                    # Assume it is already a compiled regexp.
                    self.name_patterns.append(name)
        # Initialize private attributes as empty.
        self._pathname = None
        self._root = None
        self._name = None

    def is_a_regexp(self, pattern):
        return pattern.startswith('/') and pattern.endswith('/')

    def compile_fnmatch_pattern(self, pattern):
        return re.compile(fnmatch.translate(pattern))

    def matches(self, root, name):
        self._root = root
        self._name = name
        self._pathname = mkpathname(root, name)
        if self.filetype_conditions and not self.match_type():
            return False
        if self.name_patterns and not self.match_name():
            return False
        return True

    def __call__(self, root, name):
        return self.matches(root, name)

    def match_type(self):
        '''
        Match type of the file system entry to satisfy all file type condition.
        '''
        return all([(type_condition.match(self._pathname) is True)
                    for type_condition in self.filetype_conditions])

    def match_name(self):
        '''
        Match name of the file system entry to satisfy all of name patterns.
        '''
        return all([(pattern.match(self._name) is not None)
                   for pattern in self.name_patterns])

__init__(filetypes=None, names=None)

@param filetypes: a list of flags or names of the file type used as matching condition

@param names: a list of strings for fnmatch pattern to match file name or compiled regexp object. A string surrounded with '/' is interpreted as a regexp.

Source code in src/findtools/find_files.py
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def __init__(self, filetypes=None, names=None):
    '''
    @param filetypes: a list of flags or names of the file type used as
                      matching condition

    @param names: a list of strings for fnmatch pattern to match file name
                  or compiled regexp object. A string surrounded with '/'
                  is interpreted as a regexp.
    '''
    # We need at least one of the criteria.
    assert names is not None or filetypes is not None
    # File type conditions.
    self.filetype_conditions = list()
    if filetypes is not None:
        assert type(filetypes) == list
        for filetype in filetypes:
            if isinstance(filetype, str):
                filetype_condition = FileTypeCondition(filetype)
                self.filetype_conditions.append(filetype_condition)
            else:
                # Assume FileTypeCondition-like instance.
                self.filetype_conditions.append(filetype)
    # File name conditions.
    self.name_patterns = list()
    if names is not None:
        assert type(names) == list
        for name in names:
            if isinstance(name, str):
                if self.is_a_regexp(name):
                    # Don't forget to cut the '/' wrapping, which is
                    # useless for python re syntaxes.
                    self.name_patterns.append(re.compile(name[1:-1]))
                else:
                    self.name_patterns.append(
                        self.compile_fnmatch_pattern(name)
                    )
            else:
                # Assume it is already a compiled regexp.
                self.name_patterns.append(name)
    # Initialize private attributes as empty.
    self._pathname = None
    self._root = None
    self._name = None

match_name()

Match name of the file system entry to satisfy all of name patterns.

Source code in src/findtools/find_files.py
128
129
130
131
132
133
def match_name(self):
    '''
    Match name of the file system entry to satisfy all of name patterns.
    '''
    return all([(pattern.match(self._name) is not None)
               for pattern in self.name_patterns])

match_type()

Match type of the file system entry to satisfy all file type condition.

Source code in src/findtools/find_files.py
121
122
123
124
125
126
def match_type(self):
    '''
    Match type of the file system entry to satisfy all file type condition.
    '''
    return all([(type_condition.match(self._pathname) is True)
                for type_condition in self.filetype_conditions])

MatchAnyPatternsAndTypes

Bases: MatchAllPatternsAndTypes

Overrides 'all' -> to 'any'.

Source code in src/findtools/find_files.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
class MatchAnyPatternsAndTypes(MatchAllPatternsAndTypes):
    '''Overrides 'all' -> to 'any'.'''

    def match_type(self):
        '''
        Match type of the file system entry to satisfy all file type condition.
        '''
        return any([(type_condition.match(self._pathname) is True)
                    for type_condition in self.filetype_conditions])

    def match_name(self):
        '''
        Match name of the file system entry to satisfy all of name patterns.
        '''
        return any([(pattern.match(self._name) is not None)
                   for pattern in self.name_patterns])

match_name()

Match name of the file system entry to satisfy all of name patterns.

Source code in src/findtools/find_files.py
146
147
148
149
150
151
def match_name(self):
    '''
    Match name of the file system entry to satisfy all of name patterns.
    '''
    return any([(pattern.match(self._name) is not None)
               for pattern in self.name_patterns])

match_type()

Match type of the file system entry to satisfy all file type condition.

Source code in src/findtools/find_files.py
139
140
141
142
143
144
def match_type(self):
    '''
    Match type of the file system entry to satisfy all file type condition.
    '''
    return any([(type_condition.match(self._pathname) is True)
                for type_condition in self.filetype_conditions])

findtools.core

findtools.core