<s> __author__ = '<STR_LIT>' <EOL> from . . interfaces . i_element import IElement <EOL> from . . utils . mac_utils import MacUtils <EOL> from . . import TooSaltyUISoupException <EOL> from . mouse import MacMouse <EOL> class MacElement ( IElement ) : <EOL> _acc_role_name_map = { <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' <EOL> } <EOL> _mouse = MacMouse ( ) <EOL> def __init__ ( self , obj_selector , layer_num , process_name , process_id , <EOL> class_id = None ) : <EOL> """<STR_LIT>""" <EOL> self . _object_selector = obj_selector <EOL> self . _layer_num = layer_num <EOL> self . _proc_id = process_id <EOL> self . _proc_name = process_name <EOL> self . _class_id = class_id <EOL> self . _cached_children = set ( ) <EOL> self . _cached_properties = None <EOL> @ property <EOL> def _properties ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _cached_properties : <EOL> self . _cached_properties = MacUtils . ApplescriptExecutor . get_element_properties ( <EOL> self . _object_selector , self . _proc_name ) <EOL> return self . _cached_properties <EOL> @ property <EOL> def _role ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _properties . get ( '<STR_LIT>' , None ) <EOL> def _find_windows_by_same_proc ( self ) : <EOL> """<STR_LIT>""" <EOL> axunknown_windows = MacUtils . ApplescriptExecutor . get_axunknown_windows ( self . _proc_name ) <EOL> axdialog_windows = MacUtils . ApplescriptExecutor . get_axdialog_windows ( self . _proc_name ) <EOL> mac_elements = [ MacElement ( element . applescript_specifier , <NUM_LIT:1> , self . _proc_name , <EOL> self . _proc_id , element . class_id ) for element in <EOL> axunknown_windows + axdialog_windows ] <EOL> return filter ( lambda x : x . acc_child_count , mac_elements ) <EOL> def click ( self , x_offset = <NUM_LIT:0> , y_offset = <NUM_LIT:0> ) : <EOL> x , y , w , h = self . acc_location <EOL> x += x_offset if x_offset is not None else w / <NUM_LIT:2> <EOL> y += y_offset if y_offset is not None else h / <NUM_LIT:2> <EOL> self . _mouse . click ( x , y ) <EOL> self . _cached_properties = None <EOL> def right_click ( self , x_offset = <NUM_LIT:0> , y_offset = <NUM_LIT:0> ) : <EOL> x , y , w , h = self . acc_location <EOL> x += x_offset if x_offset is not None else w / <NUM_LIT:2> <EOL> y += y_offset if y_offset is not None else h / <NUM_LIT:2> <EOL> self . _mouse . click ( x , y , self . _mouse . RIGHT_BUTTON ) <EOL> self . _cached_properties = None <EOL> def double_click ( self , x_offset = <NUM_LIT:0> , y_offset = <NUM_LIT:0> ) : <EOL> x , y , w , h = self . acc_location <EOL> x += x_offset if x_offset is not None else w / <NUM_LIT:2> <EOL> y += y_offset if y_offset is not None else h / <NUM_LIT:2> <EOL> self . _mouse . double_click ( x , y ) <EOL> self . _cached_properties = None <EOL> def drag_to ( self , x , y , x_offset = None , y_offset = None , smooth = True ) : <EOL> el_x , el_y , el_w , el_h = self . acc_location <EOL> el_x += x_offset if x_offset is not None else el_w / <NUM_LIT:2> <EOL> el_y += y_offset if y_offset is not None else el_h / <NUM_LIT:2> <EOL> self . _mouse . drag ( el_x , el_y , x , y , smooth ) <EOL> self . _cached_properties = None <EOL> @ property <EOL> def proc_id ( self ) : <EOL> return self . _proc_id <EOL> @ property <EOL> def is_top_level_window ( self ) : <EOL> return self . acc_parent_count == <NUM_LIT:0> <EOL> @ property <EOL> def is_selected ( self ) : <EOL> result = False <EOL> if self . acc_role_name == self . _acc_role_name_map [ '<STR_LIT>' ] and self . _properties . get ( '<STR_LIT>' , '<STR_LIT:false>' ) == '<STR_LIT:true>' : <EOL> result = True <EOL> return result <EOL> @ property <EOL> def is_checked ( self ) : <EOL> result = False <EOL> if self . acc_role_name == self . _acc_role_name_map [ '<STR_LIT>' ] and self . _properties . get ( '<STR_LIT>' , '<STR_LIT:false>' ) == '<STR_LIT:true>' : <EOL> result = True <EOL> return result <EOL> @ property <EOL> def is_visible ( self ) : <EOL> return True <EOL> @ property <EOL> def is_enabled ( self ) : <EOL> return bool ( self . _properties . get ( '<STR_LIT>' , False ) ) <EOL> @ property <EOL> def acc_parent_count ( self ) : <EOL> return self . _layer_num <EOL> @ property <EOL> def acc_child_count ( self ) : <EOL> children_elements = MacUtils . ApplescriptExecutor . get_children_elements ( <EOL> self . _object_selector , self . acc_parent_count , self . _proc_name ) <EOL> return len ( children_elements [ <NUM_LIT:0> ] ) <EOL> @ property <EOL> def acc_name ( self ) : <EOL> result = self . _properties . get ( '<STR_LIT>' ) or self . _properties . get ( '<STR_LIT>' ) <EOL> if not result : <EOL> if self . acc_role_name == self . _acc_role_name_map [ '<STR_LIT>' ] : <EOL> result = self . _class_id <EOL> else : <EOL> result = self . _properties . get ( '<STR_LIT>' ) or self . _class_id <EOL> return MacUtils . replace_inappropriate_symbols ( result or '<STR_LIT>' ) <EOL> def set_focus ( self ) : <EOL> MacUtils . ApplescriptExecutor . set_element_attribute_value ( <EOL> self . _object_selector , '<STR_LIT>' , '<STR_LIT:true>' , self . _proc_name , False ) <EOL> @ property <EOL> def acc_c_name ( self ) : <EOL> return self . acc_role_name + self . acc_name if self . acc_name else '<STR_LIT>' <EOL> @ property <EOL> def acc_location ( self ) : <EOL> x , y = self . _properties . get ( '<STR_LIT>' , [ <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> w , h = self . _properties . get ( '<STR_LIT>' , [ <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> return map ( int , [ x , y , w , h ] ) <EOL> @ property <EOL> def acc_value ( self ) : <EOL> return self . _properties . get ( '<STR_LIT>' , None ) <EOL> def set_value ( self , value ) : <EOL> MacUtils . ApplescriptExecutor . set_element_attribute_value ( <EOL> self . _object_selector , '<STR_LIT>' , value , self . _proc_name ) <EOL> self . _cached_properties = None <EOL> @ property <EOL> def acc_description ( self ) : <EOL> return self . _properties . get ( '<STR_LIT>' , None ) <EOL> @ property <EOL> def acc_parent ( self ) : <EOL> result = None <EOL> event_descriptor = MacUtils . ApplescriptExecutor . get_apple_event_descriptor ( <EOL> self . _object_selector , self . _proc_name ) <EOL> if self . acc_parent_count > <NUM_LIT:0> and event_descriptor . from_ : <EOL> result = MacElement ( event_descriptor . from_ . applescript_specifier , <EOL> self . acc_parent_count - <NUM_LIT:1> , <EOL> self . _proc_name , <EOL> self . proc_id , <EOL> event_descriptor . class_id ) <EOL> return result <EOL> @ property <EOL> def acc_selection ( self ) : <EOL> return self . _properties . get ( '<STR_LIT>' , None ) <EOL> @ property <EOL> def acc_focused_element ( self ) : <EOL> childs = self . findall ( ) <EOL> result = None <EOL> for element in childs : <EOL> if self . _properties . get ( '<STR_LIT>' , '<STR_LIT:false>' ) == '<STR_LIT:true>' : <EOL> result = element <EOL> break <EOL> return result <EOL> @ property <EOL> def acc_role_name ( self ) : <EOL> return self . _acc_role_name_map . get ( self . _role , '<STR_LIT>' ) <EOL> def __iter__ ( self ) : <EOL> children_elements = MacUtils . ApplescriptExecutor . get_children_elements ( <EOL> self . _object_selector , self . _layer_num , self . _proc_name ) <EOL> children = children_elements [ <NUM_LIT:0> ] <EOL> layer_number = children_elements [ <NUM_LIT:1> ] <EOL> if not len ( children ) : <EOL> raise StopIteration ( ) <EOL> for element in children : <EOL> yield MacElement ( element [ '<STR_LIT>' ] , layer_number , <EOL> self . _proc_name , self . proc_id , <EOL> element [ '<STR_LIT>' ] ) <EOL> def __findcacheiter ( self , only_visible , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for obj_element in self . _cached_children : <EOL> if obj_element . _match ( only_visible , ** kwargs ) : <EOL> yield obj_element <EOL> def _finditer ( self , only_visible , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> lst_queue = list ( self ) <EOL> if self . is_top_level_window : <EOL> lst_queue . extend ( self . _find_windows_by_same_proc ( ) ) <EOL> while lst_queue : <EOL> obj_element = lst_queue . pop ( <NUM_LIT:0> ) <EOL> self . _cached_children . add ( obj_element ) <EOL> if obj_element . _match ( only_visible , ** kwargs ) : <EOL> yield obj_element <EOL> if obj_element . acc_child_count : <EOL> childs = [ el for el in list ( obj_element ) ] <EOL> lst_queue [ : <NUM_LIT:0> ] = childs <EOL> def find ( self , only_visible = True , ** kwargs ) : <EOL> try : <EOL> return self . __findcacheiter ( only_visible , <EOL> ** kwargs ) . next ( ) <EOL> except StopIteration : <EOL> try : <EOL> return self . _finditer ( only_visible , <EOL> ** kwargs ) . next ( ) <EOL> except StopIteration : <EOL> attrs = [ '<STR_LIT>' % ( k , v ) for k , v in kwargs . iteritems ( ) ] <EOL> raise TooSaltyUISoupException ( <EOL> '<STR_LIT>' % <EOL> '<STR_LIT>' . join ( attrs ) ) <EOL> def findall ( self , only_visible = True , ** kwargs ) : <EOL> result = self . _finditer ( only_visible , ** kwargs ) <EOL> if result : <EOL> result = list ( result ) <EOL> return result <EOL> def is_object_exists ( self , ** kwargs ) : <EOL> try : <EOL> self . find ( ** kwargs ) <EOL> return True <EOL> except TooSaltyUISoupException : <EOL> return False </s>
