<s> import wx <EOL> import re <EOL> from shortcut import Shortcut <EOL> from robotide . widgets import ImageProvider <EOL> def ActionInfoCollection ( data , event_handler , container = None ) : <EOL> """<STR_LIT>""" <EOL> menu = None <EOL> actions = [ ] <EOL> for row in data . splitlines ( ) : <EOL> row = row . strip ( ) <EOL> if not row : <EOL> continue <EOL> elif row . startswith ( '<STR_LIT:[>' ) and row . endswith ( '<STR_LIT:]>' ) : <EOL> menu = row [ <NUM_LIT:1> : - <NUM_LIT:1> ] . strip ( ) <EOL> else : <EOL> actions . append ( _create_action_info ( event_handler , menu , container , <EOL> row ) ) <EOL> return actions <EOL> def _create_action_info ( eventhandler , menu , container , row ) : <EOL> if row . startswith ( '<STR_LIT>' ) : <EOL> return SeparatorInfo ( menu ) <EOL> tokens = [ t . strip ( ) for t in row . split ( '<STR_LIT:|>' ) ] <EOL> tokens += [ '<STR_LIT>' ] * ( <NUM_LIT:5> - len ( tokens ) ) <EOL> name , doc , shortcut , icon , position = tokens <EOL> if name . startswith ( '<STR_LIT:!>' ) : <EOL> name = name [ <NUM_LIT:1> : ] <EOL> container = None <EOL> eventhandler_name , name = _get_eventhandler_name_and_parsed_name ( name ) <EOL> action = getattr ( eventhandler , eventhandler_name ) <EOL> return ActionInfo ( menu , name , action , container , shortcut , icon , doc , position ) <EOL> def _get_eventhandler_name_and_parsed_name ( name ) : <EOL> eventhandler_name , name = _parse_shortcuts_from_name ( name ) <EOL> return ( '<STR_LIT>' % eventhandler_name . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) . replace ( '<STR_LIT:&>' , '<STR_LIT>' ) , <EOL> name ) <EOL> def _parse_shortcuts_from_name ( name ) : <EOL> if '<STR_LIT:(>' in name : <EOL> eventhandler_name , shortcuts = name . split ( '<STR_LIT:(>' , <NUM_LIT:1> ) <EOL> shortcuts = shortcuts . split ( '<STR_LIT:)>' ) [ <NUM_LIT:0> ] <EOL> elements = shortcuts . split ( '<STR_LIT>' ) <EOL> name = '<STR_LIT>' % ( eventhandler_name , <EOL> '<STR_LIT>' . join ( Shortcut ( e ) . printable for e in elements ) ) <EOL> return eventhandler_name , name <EOL> return name , name <EOL> class MenuInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . insertion_point = _InsertionPoint ( ) <EOL> def is_separator ( self ) : <EOL> return False <EOL> def set_menu_position ( self , before = None , after = None ) : <EOL> """<STR_LIT>""" <EOL> self . insertion_point = _InsertionPoint ( before , after ) <EOL> class ActionInfo ( MenuInfo ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , menu_name , name , action = None , container = None , <EOL> shortcut = None , icon = None , doc = '<STR_LIT>' , position = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> MenuInfo . __init__ ( self ) <EOL> self . menu_name = menu_name <EOL> self . name = name <EOL> self . action = action <EOL> self . container = container <EOL> self . shortcut = Shortcut ( shortcut ) <EOL> self . _icon = None <EOL> self . _icon_source = icon <EOL> self . doc = doc <EOL> self . _position = position <EOL> @ property <EOL> def icon ( self ) : <EOL> if not self . _icon : <EOL> self . _icon = self . _get_icon ( ) <EOL> return self . _icon <EOL> def _get_icon ( self ) : <EOL> if not self . _icon_source : <EOL> return None <EOL> if isinstance ( self . _icon_source , basestring ) : <EOL> if self . _icon_source . startswith ( "<STR_LIT>" ) : <EOL> return ImageProvider ( ) . get_image_by_name ( self . _icon_source [ len ( "<STR_LIT>" ) : ] ) <EOL> return wx . ArtProvider . GetBitmap ( getattr ( wx , self . _icon_source ) , <EOL> wx . ART_TOOLBAR , ( <NUM_LIT:16> , <NUM_LIT:16> ) ) <EOL> return self . _icon_source <EOL> @ property <EOL> def position ( self ) : <EOL> if isinstance ( self . _position , int ) : <EOL> return self . _position <EOL> elif isinstance ( self . _position , str ) : <EOL> if len ( self . _position ) > <NUM_LIT:0> : <EOL> return int ( self . _position . split ( "<STR_LIT>" ) [ - <NUM_LIT:1> ] ) <EOL> return - <NUM_LIT:1> <EOL> class SeparatorInfo ( MenuInfo ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , menu_name ) : <EOL> """<STR_LIT>""" <EOL> MenuInfo . __init__ ( self ) <EOL> self . menu_name = menu_name <EOL> def is_separator ( self ) : <EOL> return True <EOL> class _InsertionPoint ( object ) : <EOL> _shortcut_remover = re . compile ( '<STR_LIT>' ) <EOL> def __init__ ( self , before = None , after = None ) : <EOL> self . _item = before or after <EOL> self . _insert_before = before is not None <EOL> def get_index ( self , menu ) : <EOL> if not self . _item : <EOL> return menu . GetMenuItemCount ( ) <EOL> index = self . _find_position_in_menu ( menu ) <EOL> if not index : <EOL> return menu . GetMenuItemCount ( ) <EOL> if not self . _insert_before : <EOL> index += <NUM_LIT:1> <EOL> return index <EOL> def _find_position_in_menu ( self , menu ) : <EOL> for index in range ( <NUM_LIT:0> , menu . GetMenuItemCount ( ) ) : <EOL> item = menu . FindItemByPosition ( index ) <EOL> if self . _get_menu_item_name ( item ) . lower ( ) == self . _item . lower ( ) : <EOL> return index <EOL> return None <EOL> def _get_menu_item_name ( self , item ) : <EOL> return self . _shortcut_remover . split ( item . GetLabel ( ) ) [ <NUM_LIT:0> ] </s>
