Пример #1
0
 def _exec_search(self, sub, flags):
     if self.editor is None:
         return
     regex, case_sensitive, whole_word, in_selection = flags
     tc = self.editor.textCursor()
     assert isinstance(tc, QtGui.QTextCursor)
     if in_selection and tc.hasSelection():
         text = tc.selectedText()
         self._offset = tc.selectionStart()
     else:
         text = self.editor.toPlainText()
         self._offset = 0
     request_data = {
         'string': text,
         'sub': sub,
         'regex': regex,
         'whole_word': whole_word,
         'case_sensitive': case_sensitive
     }
     try:
         self.editor.backend.send_request(findall, request_data,
                                          self._on_results_available)
     except AttributeError:
         self._on_results_available(findall(request_data))
     except NotRunning:
         QtCore.QTimer.singleShot(100, self.request_search)
Пример #2
0
 def _exec_search(self, sub, flags):
     if self.editor is None:
         return
     regex, case_sensitive, whole_word, in_selection = flags
     tc = self.editor.textCursor()
     assert isinstance(tc, QtGui.QTextCursor)
     if in_selection and tc.hasSelection():
         text = tc.selectedText()
         self._offset = tc.selectionStart()
     else:
         text = self.editor.toPlainText()
         self._offset = 0
     request_data = {
         'string': text,
         'sub': sub,
         'regex': regex,
         'whole_word': whole_word,
         'case_sensitive': case_sensitive
     }
     try:
         self.editor.backend.send_request(findall, request_data,
                                          self._on_results_available)
     except AttributeError:
         self._on_results_available(findall(request_data))
     except NotRunning:
         QtCore.QTimer.singleShot(100, self.request_search)
Пример #3
0
def search_file(path, search_settings):
    """
    Search occurrences in a file.

    :param path: path of the file to search
    """
    results = []
    string = ''
    for encoding in Cache().preferred_encodings:
        try:
            with open(path, encoding=encoding) as f:
                string = f.read()
        except OSError:
            return
        except UnicodeDecodeError:
            print('failed to open file %r with encoding %s' % (path, encoding))
            continue
        else:
            print('file %r opened with encoding %s' % (path, encoding))
            break
    if string:
        data = {
            'string': None,
            'sub': search_settings['find'],
            'regex': search_settings['regex'],
            'whole_word': search_settings['whole_words_only'],
            'case_sensitive': search_settings['case_sensitive']
        }
        for line_nbr, line in enumerate(string.splitlines()):
            data['string'] = line
            occurrences = findall(data)
            if occurrences:
                results.append((line_nbr, line, occurrences))
    return results
Пример #4
0
def search_file(path, search_settings):
    """
    Search occurrences in a file.

    :param path: path of the file to search
    """
    results = []
    string = ''
    for encoding in Cache().preferred_encodings:
        try:
            with open(path, encoding=encoding) as f:
                string = f.read()
        except OSError:
            return
        except UnicodeDecodeError:
            print('failed to open file %r with encoding %s' % (path, encoding))
            continue
        else:
            print('file %r opened with encoding %s' % (path, encoding))
            break
    if string:
        data = {
            'string': None,
            'sub': search_settings['find'],
            'regex': search_settings['regex'],
            'whole_word': search_settings['whole_words_only'],
            'case_sensitive': search_settings['case_sensitive']
        }
        for line_nbr, line in enumerate(string.splitlines()):
            data['string'] = line
            occurrences = findall(data)
            if occurrences:
                results.append((line_nbr, line, occurrences))
    return results
Пример #5
0
def test_find_all(data, nb_expected):
    status, results = workers.findall(data)
    assert len(results) == nb_expected
Пример #6
0
def test_find_all(data, nb_expected):
    results = workers.findall(data)
    assert len(results) == nb_expected