CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FindAlgorithm.py
Go to the documentation of this file.
1 import logging
2 
3 from Vispa.Share.ObjectHolder import ObjectHolder
4 from Vispa.Share.BasicDataAccessor import BasicDataAccessor,BasicDataAccessorInterface
5 from Vispa.Main.Exceptions import exception_traceback
6 
8  """ Searches for label and properties in a list of objects using a BasicDataAccessor.
9 
10  One can search using findUsingFindDialog where a FindDialog is used as input for the search label and properties.
11  Navigation through the results in supported by next(), previous(),numberOfResult(),currentNumber()
12  """
13  def __init__(self):
14  ObjectHolder.__init__(self)
15  self._results=[]
16  self._index=0
17  self._message=None
18 
19  def setDataAccessor(self, accessor):
20  logging.debug(__name__ + ": setDataAccessor")
21  if not isinstance(accessor, BasicDataAccessor):
22  raise TypeError(__name__ + " requires data accessor of type BasicDataAccessor.")
23  ObjectHolder.setDataAccessor(self, accessor)
24 
25  def clear(self):
26  self._message=None
27  self._results=[]
28 
29  def findUsingFindDialog(self, dialog):
30  logging.debug(__name__ +': findUsingFindDialog')
31  self.clear()
32  if self.dataAccessor():
33  for object in self.dataObjects():
34  self._results+=self._findIn(object,dialog)
35  self._index=0
36  if len(self._results)>0:
37  return self._results[0]
38  else:
39  return []
40 
41  def _findIn(self, object,dialog):
42  # find Label
43  foundLabel=True
44  findLabel=dialog.label()
45  if findLabel!="":
46  label=self.dataAccessor().label(object)
47  #logging.debug(__name__ +': _findIn: ' + label)
48  if not dialog.caseSensitive():
49  label=label.lower()
50  findLabel=findLabel.lower()
51  if dialog.exactMatch():
52  foundLabel=findLabel=="" or findLabel==label
53  else:
54  foundLabel=findLabel in label
55 
56  # find property
57  foundProperties=True
58  findProperties=dialog.properties()
59  if len(findProperties)>0 and (findProperties[0][0]!="" or findProperties[0][1]!=""):
60  properties=[(p[1],p[2]) for p in self.dataAccessor().properties(object)]
61  if not dialog.caseSensitive():
62  properties=[(str(property[0]).lower(),str(property[1]).lower()) for property in properties]
63  findProperties=[(str(property[0]).lower(),str(property[1]).lower()) for property in findProperties]
64  if dialog.exactMatch():
65  for findProperty in findProperties:
66  foundProperties=(foundProperties and\
67  True in [(findProperty[0]=="" or findProperty[0]==p[0]) and\
68  (findProperty[1]=="" or findProperty[1]==p[1]) for p in properties])
69  else:
70  for findProperty in findProperties:
71  foundProperties=(foundProperties and\
72  True in [findProperty[0] in p[0] and\
73  findProperty[1] in p[1] for p in properties])
74 
75  # find property
76  findScripts=dialog.scripts()
77  foundScripts=True
78  if len(findScripts)>0 and findScripts[0]!="":
79  dataAccessorObject=BasicDataAccessorInterface(object,self.dataAccessor())
80  for findScript in findScripts:
81  try:
82  foundScripts=(foundScripts and\
83  (findScript=="" or dataAccessorObject.runScript(findScript)))
84  except Exception as e:
85  foundScripts=False
86  logging.info("Error in script: "+ exception_traceback())
87  self._message="Error in script: "+ str(e)
88 
89  # combine the searches
90  found=foundLabel and foundProperties and foundScripts
91  if found:
92  results=[object]
93  else:
94  results=[]
95  for daughter in self.applyFilter(self.dataAccessor().children(object)):
96  for object in self._findIn(daughter,dialog):
97  if not object in results:
98  results+=[object]
99  return results
100 
101  def results(self):
102  return self._results
103 
104  def numberOfResults(self):
105  return len(self._results)
106 
107  def currentNumber(self):
108  return self._index+1
109 
110  def next(self):
111  if len(self._results)==0:
112  return None
113  self._index+=1
114  if self._index>len(self._results)-1:
115  self._index=0
116  return self._results[self._index]
117 
118  def previous(self):
119  if len(self._results)==0:
120  return None
121  self._index-=1
122  if self._index<0:
123  self._index=len(self._results)-1
124  return self._results[self._index]
125 
126  def message(self):
127  return self._message