Menu

[8f66b6]: / SearchAsync.py  Maximize  Restore  History

Download this file

159 lines (127 with data), 6.8 kB

  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
 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
"""
Copyright (C) 2012 Oliver Tengler
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import re
from typing import Pattern, Iterator, Tuple, cast
from PyQt5.QtCore import QObject
from tools import AsynchronousTask
from tools.FileTools import fopen
from fulltextindex import FullTextIndex, IndexConfiguration
from fulltextindex.SearchMethods import SearchMethods, ResultSet, removeDupsAndSort
SearchParams = Tuple[str, str, str, bool] # Search query, folders, extensions, case sensitive
def searchContent(parent: QObject, params: SearchParams, indexConf: IndexConfiguration.IndexConfiguration, commonKeywordMap: FullTextIndex.CommonKeywordMap=None) -> ResultSet:
"""This executes an indexed or a direct search in the file content. This depends on the IndexConfiguration
setting "indexUpdateMode" and "indexType"."""
commonKeywordMap = commonKeywordMap or {}
strSearch, strFolderFilter, strExtensionFilter, bCaseSensitive = params
if not strSearch:
return ResultSet()
searchData = FullTextIndex.ContentQuery(strSearch, strFolderFilter, strExtensionFilter, bCaseSensitive)
result: ResultSet
ftiSearch = SearchMethods()
result = AsynchronousTask.execute(parent, ftiSearch.searchContent, searchData, indexConf, commonKeywordMap, bEnableCancel=True, cancelAction=ftiSearch.cancel)
result.label = strSearch
return result
def searchFileName(parent: QObject, params: SearchParams, indexConf: IndexConfiguration.IndexConfiguration) -> ResultSet:
"""This executes an indexed or a direct search for the file name. This depends on the IndexConfiguration
setting "indexUpdateMode" and "indexType"."""
strSearch, strFolderFilter, strExtensionFilter, bCaseSensitive = params
if not strSearch:
return ResultSet()
searchData = FullTextIndex.FileQuery(strSearch, strFolderFilter, strExtensionFilter, bCaseSensitive)
result: ResultSet
ftiSearch = SearchMethods()
result = AsynchronousTask.execute(parent, ftiSearch.searchFileName, searchData, indexConf, bEnableCancel=True, cancelAction=ftiSearch.cancel)
result.label = strSearch
return result
def customSearch(parent: QObject, script: str, params: SearchParams, indexConf: IndexConfiguration.IndexConfiguration,
commonKeywordMap: FullTextIndex.CommonKeywordMap=None) -> ResultSet:
"""
Executes a custom search script from disk. The script receives a locals dictionary with all neccessary
search parameters and returns its result in the variable "result". The variable "highlight" must be set
to a regular expression which is used to highlight the matches in the result.
"""
commonKeywordMap = commonKeywordMap or {}
result: ResultSet = AsynchronousTask.execute(parent, __customSearchAsync, os.path.join("scripts", script), params, commonKeywordMap, indexConf)
return result
class ScriptSearchData:
def __init__(self, reExpr: Pattern) -> None:
self.reExpr = reExpr
def matches(self, data: str) -> Iterator[Tuple[int,int]]:
"""Yields all matches in str. Each match is returned as the touple (position,length)."""
if not self.reExpr:
return
cur = 0
while True:
result = self.reExpr.search(data, cur)
if result:
startPos, endPos = result.span()
yield (startPos, endPos-startPos)
cur = endPos
else:
return
def __customSearchAsync(script: str, params: SearchParams, commonKeywordMap: FullTextIndex.CommonKeywordMap,
indexConf: IndexConfiguration.IndexConfiguration) -> ResultSet:
query, folders, extensions, caseSensitive = params
def performSearch(strSearch: str, strFolderFilter: str="", strExtensionFilter:str="", bCaseSensitive: bool=False) -> FullTextIndex.SearchResult:
if not strSearch:
return []
searchData = FullTextIndex.ContentQuery(strSearch, strFolderFilter, strExtensionFilter, bCaseSensitive)
ftiSearch = SearchMethods()
return ftiSearch.searchContent(searchData, indexConf, commonKeywordMap).matches
def regexFromText(strQuery: str, bCaseSensitive: bool) -> Pattern:
query = FullTextIndex.ContentQuery(strQuery, "", "", bCaseSensitive)
return query.regExForMatches()
class Result:
def __init__(self) -> None:
self.matches: FullTextIndex.SearchResult = []
self.highlight = None
self.label = "Custom script"
localsDict = {"re": re,
"performSearch" : performSearch,
"regexFromText" : regexFromText,
"query" : query,
"folders" : folders,
"extensions" : extensions,
"caseSensitive" : caseSensitive,
"result" : Result()}
# The actual script is wrapped in the function "customSearch". It is needed to
# establish a proper scope which enables access to local variables from sub functions
# analog to globals. Example what caused problems:
# import time
# def foo(files):
# time.sleep(5)
# foo(files)
# This failed in previous versions with "global 'time' not found".
scriptCode = ""
with fopen(script) as file:
scriptCode = "def customSearch(re,performSearch,regexFromText,query,folders,extensions,caseSensitive,result):\n"
for line in file:
scriptCode += "\t"
scriptCode += line
scriptCode += "\ncustomSearch(re,performSearch,regexFromText,query,folders,extensions,caseSensitive,result)\n"
code = compile(scriptCode, script, 'exec')
exec(code, globals(), localsDict)
result = cast(Result,localsDict["result"])
matches = result.matches
highlight = result.highlight
label = result.label
matches = removeDupsAndSort(matches)
if highlight:
searchData = ScriptSearchData(highlight)
else:
# Highlight by default the query
searchData = FullTextIndex.ContentQuery(query, "", "", caseSensitive)
return ResultSet(matches, searchData, label=label)
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.