<s> import re <EOL> from enaml . styling import StyleCache <EOL> _grad_re = re . compile ( ur '<STR_LIT>' ) <EOL> _alert_re = re . compile ( ur '<STR_LIT>' ) <EOL> def _translate_gradient ( v ) : <EOL> return _grad_re . sub ( ur '<STR_LIT>' , v ) <EOL> _KNOWN_FIELDS = set ( [ <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT:left>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT:right>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT:width>' , <EOL> ] ) <EOL> _MAY_HAVE_GRADIENT = set ( [ <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> ] ) <EOL> def _translate_setter ( setter ) : <EOL> field = setter . field <EOL> if field in _KNOWN_FIELDS : <EOL> value = setter . value <EOL> if value : <EOL> if field in _MAY_HAVE_GRADIENT : <EOL> value = _translate_gradient ( value ) <EOL> value = u'<STR_LIT>' % ( field , value ) <EOL> return value <EOL> def _translate_style_body ( style ) : <EOL> translated = [ ] <EOL> for setter in style . setters ( ) : <EOL> tks = StyleCache . toolkit_setter ( setter , _translate_setter ) <EOL> if tks is not None : <EOL> translated . append ( tks ) <EOL> return u'<STR_LIT:\n>' . join ( translated ) <EOL> def translate_style ( name , style ) : <EOL> parts = [ u'<STR_LIT>' % name ] <EOL> if style . pseudo_element : <EOL> root = parts . pop ( ) <EOL> for pe in style . pseudo_element . split ( u'<STR_LIT:U+002C>' ) : <EOL> parts . append ( root + u'<STR_LIT>' % pe . strip ( ) ) <EOL> if style . pseudo_class : <EOL> these = parts [ : ] <EOL> parts = [ ] <EOL> for pc in style . pseudo_class . split ( u'<STR_LIT:U+002C>' ) : <EOL> pc = pc . strip ( ) <EOL> for this in these : <EOL> parts . append ( this + u'<STR_LIT>' % pc ) <EOL> selector = u'<STR_LIT:U+002C>' . join ( parts ) <EOL> body = u'<STR_LIT>' % _translate_style_body ( style ) <EOL> return u'<STR_LIT>' % ( selector , body ) <EOL> def _basic_pc ( root , pc ) : <EOL> if pc : <EOL> root += u'<STR_LIT>' % pc <EOL> return root <EOL> def _base_dock_area ( name , pc ) : <EOL> rest = [ ] <EOL> floating = False <EOL> for part in pc . split ( u'<STR_LIT::>' ) : <EOL> if part == '<STR_LIT>' : <EOL> floating = True <EOL> else : <EOL> rest . append ( part ) <EOL> if floating : <EOL> root = u'<STR_LIT>' <EOL> else : <EOL> root = u'<STR_LIT>' <EOL> return _basic_pc ( root , u'<STR_LIT::>' . join ( rest ) ) <EOL> def _dock_bar ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _dock_bar_button ( name , pc ) : <EOL> return _maybe_alert ( u'<STR_LIT>' , name , pc ) <EOL> _position_map = { '<STR_LIT>' : '<STR_LIT:0>' , '<STR_LIT:right>' : '<STR_LIT:1>' , '<STR_LIT>' : '<STR_LIT:2>' , '<STR_LIT:left>' : '<STR_LIT:3>' } <EOL> def _dock_bar_handle ( name , pc ) : <EOL> rest = [ ] <EOL> position = None <EOL> for part in pc . split ( u'<STR_LIT::>' ) : <EOL> if part in _position_map : <EOL> position = _position_map [ part ] <EOL> else : <EOL> rest . append ( part ) <EOL> if position is not None : <EOL> root = u'<STR_LIT>' % position <EOL> else : <EOL> root = u'<STR_LIT>' <EOL> return _basic_pc ( root , u'<STR_LIT::>' . join ( rest ) ) <EOL> def _dock_container ( name , pc ) : <EOL> rest = [ ] <EOL> tabbed = False <EOL> floating = False <EOL> for part in pc . split ( u'<STR_LIT::>' ) : <EOL> if part == '<STR_LIT>' : <EOL> tabbed = True <EOL> elif part == '<STR_LIT>' : <EOL> floating = True <EOL> else : <EOL> rest . append ( part ) <EOL> if tabbed : <EOL> root = u'<STR_LIT>' <EOL> elif floating : <EOL> root = u'<STR_LIT>' <EOL> else : <EOL> root = u'<STR_LIT>' <EOL> return _maybe_alert ( root , name , u'<STR_LIT::>' . join ( rest ) ) <EOL> def _dock_window ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _dock_window_button ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _dock_window_close_button ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _dock_window_link_button ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _dock_window_maximize_button ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _dock_window_restore_button ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _rubber_band ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _splitter_handle ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _tab_bar ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _tab_bar_tab ( name , pc ) : <EOL> rest = [ ] <EOL> alert = u'<STR_LIT>' <EOL> for part in pc . split ( u'<STR_LIT::>' ) : <EOL> match = _alert_re . match ( part ) <EOL> if match is not None : <EOL> alert = match . group ( <NUM_LIT:1> ) <EOL> else : <EOL> rest . append ( part ) <EOL> if alert : <EOL> root = u'<STR_LIT>' % alert <EOL> else : <EOL> root = u'<STR_LIT>' <EOL> return _basic_pc ( root , u'<STR_LIT::>' . join ( rest ) ) <EOL> def _tab_bar_close_button ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _maybe_alert ( root , name , pc ) : <EOL> rest = [ ] <EOL> alert = u'<STR_LIT>' <EOL> for part in pc . split ( u'<STR_LIT::>' ) : <EOL> match = _alert_re . match ( part ) <EOL> if match is not None : <EOL> alert = match . group ( <NUM_LIT:1> ) <EOL> else : <EOL> rest . append ( part ) <EOL> if alert : <EOL> root = u'<STR_LIT>' % ( root , alert ) <EOL> return _basic_pc ( root , u'<STR_LIT::>' . join ( rest ) ) <EOL> def _base_dock_item ( name , pc ) : <EOL> rest = [ ] <EOL> tabbed = False <EOL> floating = False <EOL> for part in pc . split ( u'<STR_LIT::>' ) : <EOL> if part == '<STR_LIT>' : <EOL> tabbed = True <EOL> elif part == '<STR_LIT>' : <EOL> floating = True <EOL> else : <EOL> rest . append ( part ) <EOL> if tabbed : <EOL> root = u'<STR_LIT>' <EOL> elif floating : <EOL> root = u'<STR_LIT>' <EOL> else : <EOL> root = u'<STR_LIT>' <EOL> return _maybe_alert ( root , name , u'<STR_LIT::>' . join ( rest ) ) <EOL> def _title_bar ( name , pc ) : <EOL> return _maybe_alert ( u'<STR_LIT>' , name , pc ) <EOL> def _title_bar_label ( name , pc ) : <EOL> return _maybe_alert ( u'<STR_LIT>' , name , pc ) <EOL> def _title_bar_button ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _title_bar_close_button ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _title_bar_link_button ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _title_bar_maximize_button ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _title_bar_pin_button ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> def _title_bar_restore_button ( name , pc ) : <EOL> return _basic_pc ( u'<STR_LIT>' , pc ) <EOL> _DOCK_AREA_PSEUDO_ELEMENTS = { <EOL> u'<STR_LIT>' : _base_dock_area , <EOL> u'<STR_LIT>' : _dock_bar , <EOL> u'<STR_LIT>' : _dock_bar_button , <EOL> u'<STR_LIT>' : _dock_bar_handle , <EOL> u'<STR_LIT>' : _dock_container , <EOL> u'<STR_LIT>' : _dock_window , <EOL> u'<STR_LIT>' : _dock_window_button , <EOL> u'<STR_LIT>' : _dock_window_close_button , <EOL> u'<STR_LIT>' : _dock_window_link_button , <EOL> u'<STR_LIT>' : _dock_window_maximize_button , <EOL> u'<STR_LIT>' : _dock_window_restore_button , <EOL> u'<STR_LIT>' : _rubber_band , <EOL> u'<STR_LIT>' : _splitter_handle , <EOL> u'<STR_LIT>' : _tab_bar , <EOL> u'<STR_LIT>' : _tab_bar_tab , <EOL> u'<STR_LIT>' : _tab_bar_close_button , <EOL> } <EOL> _DOCK_ITEM_PSEUDO_ELEMENTS = { <EOL> u'<STR_LIT>' : _base_dock_item , <EOL> u'<STR_LIT>' : _title_bar , <EOL> u'<STR_LIT>' : _title_bar_label , <EOL> u'<STR_LIT>' : _title_bar_button , <EOL> u'<STR_LIT>' : _title_bar_close_button , <EOL> u'<STR_LIT>' : _title_bar_link_button , <EOL> u'<STR_LIT>' : _title_bar_maximize_button , <EOL> u'<STR_LIT>' : _title_bar_pin_button , <EOL> u'<STR_LIT>' : _title_bar_restore_button , <EOL> } <EOL> def _dock_style_selector ( name , style , elements ) : <EOL> handlers = [ ] <EOL> for pe in style . pseudo_element . split ( u'<STR_LIT:U+002C>' ) : <EOL> handler = elements . get ( pe . strip ( ) ) <EOL> if handler : <EOL> handlers . append ( handler ) <EOL> if not handlers : <EOL> return <EOL> parts = [ ] <EOL> for pc in style . pseudo_class . split ( u'<STR_LIT:U+002C>' ) : <EOL> pc = pc . strip ( ) <EOL> for handler in handlers : <EOL> parts . append ( handler ( name , pc ) ) <EOL> return u'<STR_LIT:U+002CU+0020>' . join ( parts ) <EOL> def translate_dock_area_style ( name , style ) : <EOL> selector = _dock_style_selector ( name , style , _DOCK_AREA_PSEUDO_ELEMENTS ) <EOL> if not selector : <EOL> return <EOL> body = u'<STR_LIT>' % _translate_style_body ( style ) <EOL> return u'<STR_LIT>' % ( selector , body ) <EOL> def translate_dock_item_style ( name , style ) : <EOL> selector = _dock_style_selector ( name , style , _DOCK_ITEM_PSEUDO_ELEMENTS ) <EOL> if not selector : <EOL> return <EOL> body = u'<STR_LIT>' % _translate_style_body ( style ) <EOL> return u'<STR_LIT>' % ( selector , body ) </s>
