Exemplo n.º 1
0
    def filepaths(self, named_filtr=None, glob_filter=None, re_filter=None,
                  skip_hidden=True):

        kwargs = {
            'rootpath': self.root_path,
            'dirpath': self.root_path,
            'exclude_globs': self.exclude_globs,
            'exclude_regulars': self.exclude_regulars,
            'exclude_hidden': skip_hidden
        }

        if named_filtr:
            if self.filter_globs.has_key(named_filtr):
                pattern = self.filter_globs[named_filtr]
                kwargs['match_func'] = lambda s: glob_match(s, pattern)

            elif self.filter_regulars.has_key(named_filtr):
                pattern = re.compile(self.filter_regulars[named_filtr])
                kwargs['match_func'] = lambda s: bool(pattern.match(s))

            else:
                raise InvalidFilter(named_filtr)

        elif glob_filter:
            kwargs['match_func'] = lambda s: glob_match(s, glob_filter)

        elif re_filter:
            kwargs['match_func'] = lambda s: bool(re_filter.match(s))

        for f in filtered_files(**kwargs):
            yield f
Exemplo n.º 2
0
Arquivo: search.py Projeto: lerouxb/ni
    def notify_file_add(self, filename=None, document=None):
        if not (filename or document):
            return False

        if filename and filename in self.files:
            return False

        if document and document in self.files:
            return False

        if self.search_type == SEARCH_SELECTION:
            return False  # new files don't affect selection searches
            # if filename == self.view.document.location:
            #    self.files.add(filename)
            #    self.notify_change(filename)
            #    return True

        elif self.search_type == SEARCH_DOCUMENT:
            return False  # new files don't affect document searches
            # if filename == self.view.document.location:
            #    self.files.add(filename)
            #    self.notify_change(filename)
            #    return True

        elif self.search_type == SEARCH_DOCUMENTS:
            if filename:
                self.files.add(filename)
                self.notify_change(filename)
            else:
                self.files.add(document)
                self.notify_change(document)
            return True

        elif self.search_type == SEARCH_WORKSPACE:
            if self.workspace_filter_glob:
                f = self.workspace_filter_glob
                found = self.workspace.contains(filename, glob_filter=f)
            else:
                f = self.workspace_filter_regex
                found = self.workspace.contains(filename, re_filter=f)
            if found:
                self.files.add(filename)
                self.notify_change(filename)
                return True

            # paths = [self.workspace.root_path, filename]
            # if os.path.commonprefix(paths) == self.workspace.root_path:
            # basename = os.path.basename(filename)
            # if glob_match(basename, pattern):
            #    self.files.add(filename)

        elif self.search_type == SEARCH_DIRECTORY:
            paths = [self.directory, filename]
            if os.path.commonprefix(paths) == self.directory:
                basename = os.path.basename(filename)
                if glob_match(basename, pattern):
                    self.files.add(filename)
                    return True

        return False
Exemplo n.º 3
0
Arquivo: search.py Projeto: lerouxb/ni
    def run(self):
        search = self.search

        view = search.view
        workspace = search.workspace
        directory = search.directory
        search_type = search.search_type

        # build the list of files we need to search

        if search_type == SEARCH_SELECTION:
            if view.document.location:
                search.files = set([view.document.location])
            else:
                search.files = set([view.document])

        elif search_type == SEARCH_DOCUMENT:
            if view.document.location:
                search.files = set([view.document.location])
            else:
                search.files = set([view.document])

        elif search_type == SEARCH_DOCUMENTS:
            search.files = set()
            for view in search.editor.views:
                if view.document.location:
                    search.files.add(view.document.location)
                else:
                    search.files.add(view.document)

        elif search_type == SEARCH_WORKSPACE:
            search.files = set()

            if search.workspace_filter_glob:
                pattern = search.workspace_filter_glob
                paths = workspace.filepaths(glob_filter=pattern, skip_hidden=search.skip_hidden)
            else:
                pattern = search.workspace_filter_regex
                paths = workspace.filepaths(re_filter=pattern, skip_hidden=search.skip_hidden)

            for filename in paths:
                if self.interrupted:
                    return
                search.files.add(filename)

        elif search_type == SEARCH_DIRECTORY:
            search.files = set()

            # sanity checks
            if not os.path.exists(directory):
                return
            if not os.path.isdir(directory):
                return

            pattern = search.directory_filter

            if search.is_recursive:
                kwargs = {
                    "rootpath": directory,
                    "dirpath": directory,
                    "exclude_globs": [],
                    "exclude_regulars": [],
                    "exclude_hidden": search.skip_hidden,
                }
                if pattern:
                    kwargs["match_func"] = lambda s: glob_match(s, pattern)
                else:
                    kwargs["match_func"] = lambda s: True

                for filename in filtered_files(**kwargs):
                    if self.interrupted:
                        return
                    search.files.add(filename)

            else:
                for filename in os.listdir(directory):
                    fullpath = os.path.join(directory, filename)
                    if os.path.isfile(fullpath):
                        if not pattern or glob_match(filename, pattern):
                            search.files.add(fullpath)

        print len(search.files), "files."

        # cache open files / views
        open_files = {}
        for view in search.editor.views:
            if view.document.location:
                open_files[view.document.location] = view
            else:
                # files without locations haven't been saved yet
                open_files[view.document] = view

        if search_type == SEARCH_SELECTION:
            pass  # forget about this one for now

        else:
            # go through the files one by one, search
            for fle in search.files:
                if self.interrupted:
                    return

                if isinstance(fle, Document):
                    document = fle
                    path = document.description

                    # this means we're searching an unsaved document
                    if open_files.has_key(document):
                        content = document.content

                    else:
                        continue

                else:
                    filename = fle
                    path = filename

                    # we're searching a document that has a location
                    if open_files.has_key(filename):
                        # read file from memory
                        doc = open_files[filename].document
                        content = doc.content

                    else:
                        # we don't have the file open, so we have to read it
                        # from disk
                        try:
                            try:
                                data = load_textfile(filename)
                                content = data["content"]
                            except BinaryFile:
                                # the file appears to be binary, so skip it
                                continue

                        except:
                            raise  # TODO: handle error nicely

                print path

                if search.use_regex:
                    found_matches = self.search_regex(path, content)
                else:
                    found_matches = self.search_simple(path, content)

                if found_matches:
                    pass  # signal redraw

        search.notify_done()
Exemplo n.º 4
0
 def test_glob_match_without_slash_false(self):
     assert glob_match(self.textfilename, 'binary*') == False
Exemplo n.º 5
0
 def test_glob_match_without_slash_true(self):
     assert glob_match(self.textfilename, 'text*')
Exemplo n.º 6
0
 def test_glob_match_with_slash_false(self):
     # there is ofcourse an incredibly low probability of a temp dir name
     # being created that happens to match
     assert glob_match(self.textfilename, '*/binary*') == False