NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

dataSearch.py (3744B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\fsd\simpleSearch\dataSearch.py
      2 from __future__ import absolute_import
      3 from . import dataWalker
      4 import re
      5 
      6 class SearchResults:
      7 
      8     def __init__(self, dataQuery, schemaQuery, ignoreCase, exactMatch, regularExpression):
      9         self.searchResultObjects = None
     10         self._dataQuery = dataQuery
     11         self._schemaQuery = schemaQuery
     12         self._ignoreCase = ignoreCase
     13         self._exactMatch = exactMatch
     14         self._regularExpression = regularExpression
     15 
     16     def _CreateOrderIndependentQuery(self, queryAsString):
     17         queryStrings = queryAsString.split(' ')
     18         orderIndependentQuery = ''
     19         for query in queryStrings:
     20             orderIndependentQuery += '(?=.*%s)' % query
     21 
     22         return orderIndependentQuery
     23 
     24     def DoesDataQueryMatchData(self, queryAsString, dataAsString):
     25         if not self._exactMatch and not self._regularExpression:
     26             queryAsString = self._CreateOrderIndependentQuery(queryAsString)
     27         regularExpressionFlag = re.IGNORECASE if self._ignoreCase else 0
     28         return re.match(queryAsString, dataAsString, regularExpressionFlag) is not None
     29 
     30     def Callback(self, rootObj, obj, schemaNode, path):
     31         continueWalking = True
     32         if self._dataQuery:
     33             for queryPath, queryValue in self._dataQuery.iteritems():
     34                 queryRegularExpression = re.compile(queryPath)
     35                 if queryRegularExpression.match(path) and self.DoesDataQueryMatchData(str(queryValue), str(obj)):
     36                     self.AppendMatch(rootObj, path)
     37                     return False
     38 
     39         if self._schemaQuery:
     40             for queryPath, query in self._schemaQuery.iteritems():
     41                 starSearch = queryPath.endswith('*')
     42                 if starSearch:
     43                     queryPath = queryPath.replace('*', '')
     44                 if path.startswith(queryPath) and starSearch or path == queryPath:
     45                     for queryField, queryValue in query.iteritems():
     46                         if queryField in schemaNode and schemaNode[queryField] == queryValue:
     47                             self.AppendMatch(rootObj, path)
     48                             return False
     49 
     50         return continueWalking
     51 
     52     def AppendMatch(self, rootObj, path):
     53         if type(self.searchResultObjects) == type({}):
     54             if path == 'root':
     55                 self.searchResultObjects = rootObj
     56             else:
     57                 keySearchRes = re.search('root\\[\\S*\\]|root\\<\\S*\\>', path)
     58                 if keySearchRes:
     59                     key = keySearchRes.group()
     60                     key = key.replace('root[', '').replace(']', '')
     61                     key = key.replace('root<', '').replace('>', '')
     62                     keyType = type(rootObj.keys()[0])
     63                     self.searchResultObjects[keyType(key)] = rootObj[keyType(key)]
     64                 else:
     65                     self.searchResultObjects = rootObj
     66         elif type(self.searchResultObjects) == type([]):
     67             if path == 'root':
     68                 self.searchResultObjects = rootObj
     69             else:
     70                 key = re.search('root\\[\\S*\\]', path).group().replace('root[', '').replace(']', '')
     71                 self.searchResultObjects.append(rootObj[int(key)])
     72         else:
     73             self.searchResultObjects = rootObj
     74 
     75 
     76 def Search(data, schema, dataQuery = None, schemaQuery = None, ignoreCase = False, exactMatch = False, regularExpression = False):
     77     results = SearchResults(dataQuery, schemaQuery, ignoreCase, exactMatch, regularExpression)
     78     results.searchResultObjects = type(data)()
     79     dataWalker.Walk(data, schema, continueWalkingFunction=results.Callback)
     80     return results.searchResultObjects