PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/AndroidAdapter/object_keyword.py

https://github.com/jaaskel9/tema-android-adapter
Python | 146 lines | 85 code | 25 blank | 36 comment | 18 complexity | cbb09d07f255251d6b23bb1a902014cc MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2006-2010 Tampere University of Technology
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining
  5. # a copy of this software and associated documentation files (the
  6. # "Software"), to deal in the Software without restriction, including
  7. # without limitation the rights to use, copy, modify, merge, publish,
  8. # distribute, sublicense, and/or sell copies of the Software, and to
  9. # permit persons to whom the Software is furnished to do so, subject to
  10. # the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be
  13. # included in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. import re
  23. from adapterlib.ui_keyword import UIKeyword
  24. class ObjectKeyword(UIKeyword):
  25. def __init__(self):
  26. super(ObjectKeyword,self).__init__()
  27. def __setSearchRoot(self,value):
  28. self.__searchroot = value
  29. def __getSearchRoot(self):
  30. if self.__searchroot == None:
  31. return self._target.getGUIReader().getRoot()
  32. else:
  33. return self.__searchroot
  34. searchroot = property(__getSearchRoot,__setSearchRoot)
  35. def __getSearchRootReference(self):
  36. if self.__searchroot == None:
  37. return "root"
  38. else:
  39. return self.__searchroot_reference
  40. def __setSearchRootReference(self,value):
  41. self.__searchroot_reference = value
  42. searchrootReference = property(__getSearchRootReference,__setSearchRootReference )
  43. def findComponentReference(self, reference, searchAll = False):
  44. self._target.getGUIReader().readGUI()
  45. return super(ObjectKeyword,self).findComponentReference(reference, searchAll )
  46. def searchChildren(self, searchRoot, component, roleName = None, searchOnlyFirst = False):
  47. """
  48. @type searchRoot: ViewItem
  49. @rtype: None or ViewItem or [ViewItem]
  50. """
  51. #If the searched component is root, just return it
  52. if component == "root":
  53. if searchRoot == self.searchroot:
  54. temp = []
  55. temp.append(searchRoot)
  56. return temp
  57. return None
  58. matcher = re.compile("(?P<partial>partial:)?'(?P<text>.*)'").match(component)
  59. #Search children by their text content when enclosed with single quote
  60. # chars
  61. if( matcher != None ):
  62. return self._target.getGUIReader().findComponentWithText(matcher.group("text"),roleName,searchRoot, not searchOnlyFirst, matcher.group("partial") != None)
  63. #Search by id
  64. else:
  65. return self._target.getGUIReader().findComponentWithId(component,roleName,searchRoot,not searchOnlyFirst)
  66. def findHierarchically(self, searchNode, component):
  67. """
  68. @type searchNode: ViewItem
  69. @rtype: list or None
  70. @return: List of ViewItems
  71. """
  72. names = component.split("::")
  73. print ":: separated: ",
  74. print names
  75. if names == None or len(names) < 2:
  76. return self.searchChildren(searchNode, component)
  77. compName = names[0]
  78. components = self.searchChildren(searchNode, compName)
  79. if components == None:
  80. return None
  81. #Check the parent names
  82. for name in names[0:-1]:
  83. tempNodes = []
  84. #check if text content or name is refereced
  85. matcher = re.compile("'(.*)'").match(name)
  86. if matcher:
  87. text = matcher.group(1)
  88. for comp in components:
  89. compOK = False
  90. if matcher:
  91. if(text == comp.getProperties()['mText']):
  92. compOK = True
  93. elif name == None or name == "" or comp.getId() == name or (name == 'root' and comp == self._target.getGUIReader().getRoot()):
  94. compOK = True
  95. if compOK and len(comp.getChildren()) > 0:
  96. tempNodes.extend(comp.getChildren())
  97. components = tempNodes
  98. #Check the final components names and add accepted components to a list
  99. #if name is "" all children are acceptable
  100. if(names[-1] != ""):
  101. temp = components
  102. components = []
  103. #Check if final component is name or text reference
  104. matcher = re.compile("'(.*)'").match(names[-1])
  105. for t in temp:
  106. if matcher:
  107. if t.getProperties().has_key('mText') and matcher.group(1) == t.getProperties()['mText']:
  108. components.append(t)
  109. elif names[-1] == t.getId():
  110. components.append(t)
  111. return components
  112. def checkNodeImplementsRole(self,node,rolename):
  113. return node.getClassName().find(rolename) != -1