aboutsummaryrefslogtreecommitdiff
path: root/packages/finder/plugin/searcher/file/name.py
blob: 9d0217362624154945627a18930d6fe2858cb931 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Finder searcher and browser REPL.
#
# Copyright (C) 2024 Silvio Rhatto <rhatto@riseup.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import re

class FinderPluginFileNameSearcher():
    def __init__(self, path):
        self.set_path(path)
        self.items = []

    def set_path(self, path):
        self.path  = path
        self.items = []

    async def finder(self):
      path           = self.path
      ignore_folders = [ '.git', '__pycache__' ]
      ignore_files   = [ '.gitignore', '.gitattributes', '.gitmodules' ]

      for parent, dirs, files in os.walk(self.path):
          # Ignore folders
          for ignore_folder in ignore_folders:
             if ignore_folder in dirs:
                 dirs.remove(ignore_folder)

          # Currently we're not listing folders
          #for folder in dirs:
          #    self.items.append(os.path.join(parent, folder))

          for file in files:
              # Ignore files
              for ignore_file in ignore_files:
                  if file == ignore_file:
                      continue

              # Ignore hidden files
              if file.startswith('.'):
                  continue

              self.items.append(os.path.join(parent, file))

      return self.items

    def filter(self, text = ''):
        if text == '' or text is None:
            return self.items

        return filter(lambda s: re.match(".*" + re.escape(text) + ".*", s), self.items)