
    f                        d Z ddlmZmZmZ ddlZ ej                  d      ZddlZddl	m
Z
 ddlZddlZddlZddlZddlmZ ddlmZ ddlmZmZmZmZmZmZmZmZ ddlmZ dd	lmZ  e       Z G d
 d      Z  G d de!      Z" G d de      Z# G d de#      Z$ G d de#      Z% G d de#      Z& G d de#      Z' G d de#      Z( G d de(      Z) G d de#      Z*y)z#
Module for theme related classes.
    )divisionprint_functionunicode_literalsN
Appearance)minidom)log)
Exceptions)hexstring_to_floatbrightentoprettyxmlTreeItemVersionunicode_str	open_utf8XDGDirs)Configc                      e Zd ZdZ edd      Z edd      Z edd      Z edd      ZeZ	g dg dg dg d	g d
g dg dg dg dg dddi gg dg dgZ
d Zed        Zed        Zd Zd Zd,dZd Zd Zd Zd Zd Zed        Zed         Zed!        Zed"        Zed#        Zed$        Zed%        Zed-d&       Zed'        Z ed-d(       Z!d) Z"d* Z#y+).ThemezK
    Theme controls the visual appearance of Onboards keyboard window.
       r         )color_scheme_basenames )background_gradientd        )	key_styler   flat)roundrect_radiusr   r   )key_sizer         Y@)key_stroke_widthr   r"   )key_fill_gradientr   r   )key_stroke_gradientr   r   )key_gradient_directionr   r   )key_label_fontr   r   key_label_overridesza{s[ss]})key_shadow_strengthr   r   )key_shadow_sizer   r   c                     d| _         d| _        d| _        d| _        d| _        | j
                  D ]  \  }}}t        | ||        y )NFr   )	_modified	_filename
_is_system_system_exists_name
attributessetattr)selfname_typedefaults       4/usr/lib/python3/dist-packages/Onboard/Appearance.py__init__zTheme.__init__Y   sN    #
 %)OO 	) D%D$(	)    c                     t         j                  j                  t         j                  j                  | j                              d   S )z* Returns the file base name of the theme. r   ospathsplitextbasenamer-   r3   s    r7   r?   zTheme.basenamef   1     ww 0 0 @A!DDr9   c                     | j                   S )z$ Returns the filename of the theme. r-   r@   s    r7   filenamezTheme.filenamek        ~~r9   c                 j    |sy| j                   D ]!  \  }}}t        | |      t        ||      k7  s! y y)NFT)r1   getattr)r3   otherr4   r5   _defaults        r7   __eq__zTheme.__eq__p   s@    %)__ 	!D%tT"geT&::	 r9   c                 d    d| j                   | j                  | j                  | j                  fz  S )Nz&name=%s, colors=%s, font=%s, radius=%d)r0   r   r'   r    r@   s    r7   __str__zTheme.__str__x   s8    74::040J0J040C0C040E0E;G G 	Gr9   c                 P   | j                         }|s+t        j                  t        d| j                               yt
        j                  j                  ||       | j                  D ]:  \  }}}|dk7  s t        t
        j                  d|z         t        | |      |       < y)z3 Applies the theme to config properties/gsettings. z-Color scheme for theme '{filename}' not found)rD   Fr   set_T)
get_color_scheme_filename_loggererror_formatr-   configtheme_settingsset_color_scheme_filenamer1   rG   )r3   saverD   r4   r5   rI   s         r7   applyzTheme.apply~   s    113MM'"Q+/>>; <77$G%)__ 	=!D%..=--v}=")$"5t=	=
 r9   c                    t         j                  j                  t        j	                         | j
                        dz   t        j                         z   }t         j                  j                  |      sTt         j                  j                  t        j                         | j
                        dz   t        j                         z   }t         j                  j                  |      sy|S )z1 Returns the filename of the themes color scheme..N)
r<   r=   joinr   	user_pathr   ColorScheme	extensionisfilesystem_pathr3   rD   s     r7   rO   zTheme.get_color_scheme_filename   s    77<< 1 $ : :< #$&1&;&;&=> ww~~h'ww||E$5$5$7$($>$>@$'(*5*?*?*ABH ww~~h'r9   c                     t         j                  j                  t         j                  j                  |            d   | _        y)z' Set the filename of the color_scheme. r   N)r<   r=   r>   r?   r   r`   s     r7   rU   zTheme.set_color_scheme_filename   s2      WW--bgg.>.>x.IJ1M 	"r9   c                 H    | j                   j                  d      }|r|d   S y)z? Returns the (potentially overridden) label of the super keys. LWINr   Nr(   getr3   overrides     r7   get_superkey_labelzTheme.get_superkey_label   s(    ++//7A;r9   c                 H    | j                   j                  d      }|r|d   S y)zT
        Returns the (potentially overridden) size group of the super keys.
        rc   r   Nrd   rf   s     r7   get_superkey_size_groupzTheme.get_superkey_size_group   s*     ++//7A;r9   c                 v    | j                   }|d|v r|d= d|v r|d= || _         y||f|d<   ||f|d<   || _         y)zB Sets or clears the override for left and right super key labels. Nrc   RWIN)r(   )r3   label
size_grouptupless       r7   set_superkey_labelzTheme.set_superkey_label   s`    ))=6N6N $*  $Z0F6N#Z0F6N#) r9   c                    t         j                  j                  t         j                  j                  |             d   }t         j                  j	                  t
        j                         |      dz   t
        j                         z   S )z: Returns the user filename for the given system filename. r   rY   )r<   r=   r>   r?   rZ   r   r[   r]   )rD   r?   s     r7   system_to_user_filenamezTheme.system_to_user_filename   sf     77##BGG$4$4X$?@Cww||EOO- (*,/027//2CD 	Dr9   c                     t         j                  j                  t        j	                         |       dz   t        j                         z   S )zU
        Returns a fully qualified filename pointing into the user directory
        rY   )r<   r=   rZ   r   r[   r]   r?   s    r7   build_user_filenamezTheme.build_user_filename   s>    
 ww||EOO- (*,/027//2CD 	Dr9   c                     t         j                  j                  t        j	                         |       dz   t        j                         z   S )zW
        Returns a fully qualified filename pointing into the system directory
        rY   )r<   r=   rZ   r   r_   r]   rt   s    r7   build_system_filenamezTheme.build_system_filename   s@    
 ww||E--/ (*,/027//2CD 	Dr9   c                  ^    t         j                  j                  t        j                  d      S )z4 Returns the path of the user directory for themes. themesr<   r=   rZ   rS   user_dir r9   r7   r[   zTheme.user_path        ww||FOOX66r9   c                  ^    t         j                  j                  t        j                  d      S )z6 Returns the path of the system directory for themes. ry   r<   r=   rZ   rS   install_dirr|   r9   r7   r_   zTheme.system_path        ww||F..99r9   c                       y)z+ Returns the file extension of theme files themer|   r|   r9   r7   r]   zTheme.extension   s     r9   c                     t         j                  d      } t         j                  d      }t        d | D              }|D ]C  }|j                  |v r"|||j                     d   f||j                  <   3|df||j                  <   E |S )zk
        Merge system and user themes.
        User themes take precedence and hide system themes.
        TFc              3   <   K   | ]  }|j                   |d ff  y wNrt   .0ts     r7   	<genexpr>z+Theme.load_merged_themes.<locals>.<genexpr>   s     E!qzzAt9-E   r   N)r   load_themesdictr?   )system_themesuser_themesry   r   s       r7   load_merged_themeszTheme.load_merged_themes   s     ))$/''.E}EE  	7E~~'*/1G1J)Ku~~&*/u~~&	7 r9   c                     g }| rt         j                         }nt         j                         }t         j                  |      }|D ],  }t         j	                  ||       }|s|j                  |       . |S )z? Load all themes from either the user or the system directory. )r   r_   r[   find_themesloadappend)	is_systemry   r=   	filenamesrD   r   s         r7   r   zTheme.load_themes   sk     $$&D??$D%%d+	! 	%HJJx3Ee$	% r9   c                    g }	 t        j                  |       }|D ]U  }|j                  t        j                               s'|j                  t         j                  j                  | |             W |S # t        $ r g }Y iw xY w)zT
        Returns the full path names of all themes found in the given path.
        )	r<   listdirOSErrorendswithr   r]   r   r=   rZ   )r=   ry   filesrD   s       r7   r   zTheme.find_themes  sz    
 	JJt$E  	<H  !23bggll4:;	<   	E	   A5 5BBc                 (   d}t        |       }	 t        j                  |      j                  }	 t	               }|j
                  j                  d      }|rt        j                  |j                        nt        j                  }|j
                  d   j                  |_        t        j                  |d      }|||_        |j                  d      }	|	r|	d   }
i }|
j                  d      D ]x  }|j
                  d   j                  }|j
                  j                  d	      }|r|j                  nd
}|j
                  j                  d      }|r|j                  nd
}||f||<   z ||_        t        j
                  D ]  \  }}}|dvst        j                  ||      }|%|dk(  rt#        |      }|dk(  rt%        |      }|dk(  r'|j'                  d      D cg c]  }t%        |       }}|t        j(                  k  r|dk(  r|dz   }|dk  r|dz  }t+        |||        | |_        ||_        ||_        |}|j3                          	 |jM                          |S c c}w # |j3                          w xY w# t4        j6                  t8        j:                  j<                  j>                  f$ rJ}t@        jC                  tE        d| tG        |      jH                  tK        |                   d}Y d}~d}~ww xY w# |jM                          w xY w)z- Load a theme and return a new theme object. Nformatr4   color_schemer(   r   keyidrm   r   group)r   r(   ir   ad,r&   ih  iLz6Error loading theme '{filename}'. {exception}: {cause}rD   	exceptioncause)'r   r   parsedocumentElementr   r1   re   r   from_stringvalueTHEME_FORMAT_INITIALr4   utilsxml_get_textr   getElementsByTagNamer(   intfloatsplitTHEME_FORMAT_1_1r2   r-   r   system_existsunlinkr	   ThemeFileErrorxmlparsersexpat
ExpatErrorrP   rQ   rR   type__name__r   close)rD   r   result_filedomdocr   noder   textnodes	overridesro   rg   key_idrm   r   r4   r5   rI   r   r   exs                         r7   r   z
Theme.load  s    (#G	]]5)99F: ((,,X6  !,,TZZ8&+&@&@  $..v6<<
 ))&.A|26E/ 334IJ %aIF$-$B$B5$I 8!)!4!4T!:!@!@'2266w?.2

'2266w?.2

*/v8 17E- .3-=-= 8)D% $; ; % 2 264 @$}$|(+E
$|(-e$};@;;s;K(Laq(L(L  &(>(>> $(+C#C.3ckNE',}(-#E47-80 #+"+&/# KKM? )M$ ))!!,,. 	MM' #9-5.22h.?.?*5b/	; <
 F	 KKMsg   I; EI& I& $8I& I!.AI& >I; K? !I& &I88I; ;7K<2A K72K? 7K<<K? ?Lc                 ^    | j                  |      | _        || _        | j                          y)z# Save this theme under a new name. N)ru   r-   r0   rV   )r3   r?   r4   s      r7   save_aszTheme.save_asi  s$    11(;
		r9   c                    t        j                         }	 |j                  d      }|j                  d| j                         |j                  dt        | j                               |j                  |       | j                  D ]  \  }}}|dk(  rO|j                  d      }|j                  | j                        }|j                  |       |j                  |       \|dk(  r|j                  d      }|j                  |       | j                  }	t        |	j                               D ]c  \  }
}|j                  d      }|j                  d|
       |j                  d	|d
          |j                  d|d          |j                  |       e t        | |      }|dk(  rnU|dk(  rt        |      }nD|dk(  rt        t        t!        |      d            }n |dk(  rdj#                  d |D              }nJ |j                  |      }|j                  |      }|j                  |       |j                  |        t%        |      }t'        j(                  | j+                                t-        | j.                  d      5 }t0        j2                  j4                  dk\  r|j7                  |       n |j7                  |j9                  d             ddd       |jC                          y# 1 sw Y   xY w# t:        $ r4}|}t=        j>                  tA        d      | j.                  z   |      d}~ww xY w# |jC                          w xY w)z Save this theme. r   r4   r   r   r   r(   r   r   rm   r   r   r   r   r   r   r   r   z, c              3   2   K   | ]  }t        |        y wr   )str)r   r   s     r7   r   zTheme.save.<locals>.<genexpr>  s     )@Q#a&)@s   wr   zUTF-8NzError saving )chained_exception)"r   DocumentcreateElementsetAttributer0   r   THEME_FORMATappendChildr1   createTextNoder   r(   listitemsrG   roundr   rZ   r   r   assure_user_dir_existsr[   r   r-   sysversion_infomajorwriteencode	Exceptionr	   r   _r   )r3   r   theme_elementr4   r5   rI   elementr   overrides_elementro   r   valuesr   
pretty_xmlr   xxx_todo_changeme2r   s                    r7   rV   z
Theme.saveo  s    !!#8	"009M&&vtzz:&&xT5F5F1GH}-)- !7%eX22$22>BG!001K1KLD''-!--g622"001FG &!--.?@!55F*.v||~*> ?"("6"6u"=,,T6:,,WfQi@,,WfQi@)55g>? $D$/E|# #E
# #E%,$: ;$ $		)@%)@ @%u$2248G!007D''-!--g6C!7F %V,J**4>>+;<4>>3/ <5##))Q.KK
+KK
 1 1' :;	< MMO< <  	:%R++Ao,>..-!68: :	:
 MMOs=   I1K< AK0K< 0K95K< <	L9/L44L99L< <MN)TF)$r   
__module____qualname____doc__r   r   r   THEME_FORMAT_1_2THEME_FORMAT_1_3r   r1   r8   propertyr?   rD   rJ   rL   rW   rO   rU   rh   rj   rp   staticmethodrr   ru   rw   r[   r_   r]   r   r   r   r   r   rV   r|   r9   r7   r   r   4   s    #1a= q!} q!} q!}#L
 /-&*$,+-0'"J3-)J ) E E  G N
* D D D D D D 7 7 : :    "       O Ob<r9   r   c                      e Zd ZdZ edd      Z edd      Z edd      ZeZd Z	e
d        Ze
d        Zd Zd!d
Zd Zd Zd Zd Zd Zed        Zed        Zed        Zed        Zed"d       Zed        Zed"d       Zed        Zed        Zed        Zed        Zed        Z  e!jD                  de!jF                        Z$ed        Z%ed        Z&ed        Z'ed         Z(y	)#r\   a  
    ColorScheme defines the colors of onboards keyboard.
    Each key or groups of keys may have their own individual colors.
    Any color definition may be omitted. Undefined colors fall back
    to color scheme defaults first, then to hard coded default colors.
    r   r   r   c                 .    d| _         d| _        d | _        y )Nr   F)r-   r.   _rootr@   s    r7   r8   zColorScheme.__init__  s    
r9   c                     t         j                  j                  t         j                  j                  | j                              d   S )z1 Returns the file base name of the color scheme. r   r;   r@   s    r7   r?   zColorScheme.basename  rA   r9   c                     | j                   S )z+ Returns the filename of the color scheme. rC   r@   s    r7   rD   zColorScheme.filename  rE   r9   c                 x    |j                   |j                  fD ]  }| j                  j                  |      s y y)NTF)theme_idr   r   find_key_id)r3   r   r   s      r7   is_key_in_schemezColorScheme.is_key_in_scheme  s8    <<( 	Bzz%%b)	 r9   Nc                    |#|j                         }|j                   |d<   |d= d}d}d}d}d}|j                  |j                  g}	|j                  dk(  r|	j	                  d       n|j                  dk(  r|	j	                  d       n|j                         r|	j	                  d       nc|j                         r|	j	                  d       nA|j                         r1|	j	                  |j                  d	             |	j	                  d	       |	D ]6  }
| j                  j                  |
      }|s!|j                  ||      \  }} n | j                  j                         }|r|j                  ||      \  }}|d
k(  r|j                         s|dk(  r|j                         r|||}|	d}n||}|}|| j                  |||      dd }|| j                  |||      d   }||gz   }|S )z
        Get the color for the given key element and optionally key state.
        If <state> is None the key state is retrieved from <key>.
        Ninsensitive	sensitivecorrectionsbgwordlistpredictionsbg
prediction
correctionlayerfillrm         ?r   )	get_stater   r   r   r   is_prediction_keyis_correction_keyis_layer_buttonget_similar_theme_idr   r   find_element_colorget_default_key_groupget_key_default_rgba)r3   r   r   statergbopacityroot_rgbroot_opacity	key_groupidsr   root_key_grouprgbas                r7   get_key_rgbazColorScheme.get_key_rgba  s    =MMOE'*}}#4E- k"	 ||SVV$
 66_$JJz"VV&JJz"""$JJ|$""$JJ|$  "JJs//89JJw  	B

..r2I(;;GUKW		 99;"55guE #Hl f!4!4!6g#"7"7"9 ;&?!GC"G;++C%@!DC?//WeDQGGgYr9   c                    g dg dg dg dg dg dg dg dg dg dg dd	}g d}|d
k(  r|j                         r<t        |j                               s#|j                         }| j	                  |      }|S |j                  d      rt        t        |j                                     }d|d<   | j                  |||      }t        j                  |d d  \  }}	}
t        |	dz   d|	dz
  z
  z         dz  dz   }|	dk  rt        |g| }|S t        | g| }|S |j                  d      r|d   }|j                  d      rt        t        |j                                     }d|d<   d|d<   | j                  |||      }t        t        |j                                     }d|d<   | j                  |||      }t        d      D ]  }||   ||   z   dz  ||<    |S |j                  d      r|d   }|S |j                  d      r|d   }|S |j                  d      r|d   }|S |d
   }|S |dk(  r
||d   k(   |S |dk(  rA|j                         r|d   }n|d   }|j                  d      r| j!                  |||      }|S |dk(  r+|d   }|j                  d      r| j!                  |||      }|S |dk(  r|d   }|S J )N)g?g333333?ffffff?r  r   r   r   r  333333?r  r  r        ?r  r  r  r  r   r   r  ?r  r  r  g=
ףp=?gRQ?g      ?r  )r  r  r  r  )r   prelightpressedactivelockedscannedstrokerm   secondary-labeldwell-progresscorrection-labelr   r   Fr   gMbP?r   g?g{Gz?r  r#  r!  r"            @r  r$  rm   r'  r   r%  r&  )r  anyr   get_layer_indexget_layer_fill_rgbare   r   r   r   r  colorsys
rgb_to_hlsr   r   ranger  _get_insensitive_color)r3   r   r   r
  colorsr  layer_index	new_statehlr   amountr#  r   r   s                  r7   r	  z ColorScheme.get_key_default_rgba!  s[   0F0F0F0F0F0F0F0F0F0G0F $f""$u||~& "113//<Z W 9% ekkm!45	',	)$((gyA #--tBQx81a $AdF456t;dBr6#VG3d3Dz w $VG3d3Dv s 9%i( 99X& $T%++-%8 9I*/Ih'*/Ih'"//WiHG !%T%++-%8 9I+0Ii(,,S'9ED #1X ?#*1:Q#73">Q?P K :&j)H G 8$h'D C 8$h'@ = f~< 9  F8$$6 3 $$&01g yy'223wG   ))+,D yy'223wG  ((*+D
  5r9   c                    |j                         }d|d<   | j                  |d|      }| j                  |||      }t        j                  |d d  \  }}}	t        j                  |d d  \  }}
}	|
|z
  dz  dz  }t	        | g| S )NFr   r   r   r)  g      @)copyr  r-  r.  r   )r3   r   r
  r   r3  r   r  r4  lfr   llr6  s               r7   r0  z"ColorScheme._get_insensitive_color  s    JJL	#(	-   fi8  gy9&&Ra12q&&Ra12q r'S3&'$''r9   c                 4   d}d}| j                   j                         }d}|D ]  }|j                  |k(  s|} n |rK|j                  D ]<  }|j	                         s|j
                  |k(  s$|j                  }|j                  } n |g d}|d}||gz   }|S )z~
        Returns window colors.
        window_type may be "keyboard" or "key-popup".
        element may be "border"
        N)r  r  r  r  )r   get_windowsr   r   is_colorr   r  r  )	r3   window_typer   r  r  windowswindowitemr  s	            r7   get_window_rgbazColorScheme.get_window_rgba  s     **((* 	DyyK'	
  ==?<<7*((C"llG ;!C?GgYr9   c                 T   d}d}| j                   j                         }t        |t        |      dz
        }|dk\  r\|t        |      k  rN||   j                  D ]<  }|j                         s|j                  dk(  s$|j                  }|j                  } n |g d}|d}||gz   }|S )zV
        Returns the background fill color of the layer with the given index.
        Nr   r   
backgroundr  r  r  r  )	r   
get_layersminlenr   r=  r   r  r  )r3   r2  r  r  layersrA  r  s          r7   r,  zColorScheme.get_layer_fill_rgba  s    
 &&( +s6{Q7!c&k 9{+11 ==?<<</((C"llG ;!C?GgYr9   c                 :   d}d}| j                   j                         }|D ]M  }|j                  D ]<  }|j                         s|j                  |k(  s$|j
                  }|j                  } M O |dk(  rg d}nJ ||dd }||d   }|g d}|d}||gz   }|S )zF
        Returns the color for the given element of the icon.
        NrD  ))\(?rK  rK  r  r   rE  r  )r   	get_iconsr   r=  r   r  r  )	r3   r   r  r  iconsiconrA  rgba_defaultr  s	            r7   get_icon_rgbazColorScheme.get_icon_rgba  s     

$$& 	D

 ==?<<7*((C"llG	 l"2L5;r"C?"1oG;!C?GgYr9   c                  ^    t         j                  j                  t        j                  d      S )z; Returns the path of the user directory for color schemes. ry   rz   r|   r9   r7   r[   zColorScheme.user_path  r}   r9   c                  ^    t         j                  j                  t        j                  d      S )z= Returns the path of the system directory for color schemes. ry   r   r|   r9   r7   r_   zColorScheme.system_path  r   r9   c                       y)z2 Returns the file extension of color scheme files r1  r|   r|   r9   r7   r]   zColorScheme.extension   s     r9   c                      t         j                  d      } t         j                  d      }t        d | D              }|D ]  }|||j                  <    |S )z
        Merge system and user color schemes.
        User color schemes take precedence and hide system color schemes.
        TFc              3   8   K   | ]  }|j                   |f  y wr   rt   r   s     r7   r   z7ColorScheme.get_merged_color_schemes.<locals>.<genexpr>  s     Kajj!_Ks   )r\   load_color_schemesr   r?   )system_color_schemesuser_color_schemescolor_schemesschemes       r7   get_merged_color_schemesz$ColorScheme.get_merged_color_schemes  sZ      +==dC(;;EBK6JKK( 	4F-3M&//*	4r9   c                     g }| rt         j                         }nt         j                         }t         j                  |      }|D ],  }t         j	                  ||       }|s|j                  |       . |S )zV
        Load all color schemes from either the user or the system directory.
        )r\   r_   r[   find_color_schemesr   r   )r   rY  r=   r   rD   r   s         r7   rV  zColorScheme.load_color_schemes  sr    
 **,D((*D2248	! 	3H&++Hi@L$$\2	3 r9   c                    g }	 t        j                  |       }|D ]U  }|j                  t        j                               s'|j                  t         j                  j                  | |             W |S # t        $ r g }Y iw xY w)z[
        Returns the full path names of all color schemes found in the given path.
        )	r<   r   r   r   r\   r]   r   r=   rZ   )r=   rY  r   rD   s       r7   r]  zColorScheme.find_color_schemes%  s    
 	JJt$E  	CH  !6!6!89$$RWW\\$%AB	C   	E	r   c                    d}t        |       }	 t        j                  |      j                  }|j                  d   j
                  }t        j                  }|j                  d      r,t        j                  |j                  d   j
                        }|t        j                  k\  rt        j                  |di       }nEt        j                  t        d|t        j                   |              t        j#                  |      }|At%               }|j'                  |       t               }||_        | |_        ||_        ||_        |jA                          |S # t0        j2                  j4                  j6                  $ rH}	t        j9                  t        d| t;        |	      j<                  t?        |	                   Y d}	~	|d}	~	ww xY w# |jA                          w xY w)z6 Load a color scheme and return it as a new instance. Nr4   r   z{Loading legacy color scheme format '{old_format}', please consider upgrading to current format '{new_format}': '{filename}')
old_format
new_formatrD   z=Error loading color scheme '{filename}'. {exception}: {cause}r   )!r   r   r   r   r1   r   r\   COLOR_SCHEME_FORMAT_LEGACYhasAttributer   r   COLOR_SCHEME_FORMAT_TREE_parse_dom_noderP   warningrR   COLOR_SCHEME_FORMAT_parse_legacy_color_schemeRoot	set_itemsr4   r-   r   r   r   r   r   r   rQ   r   r   r   r   )
rD   r   r   fdomr4   r   r   rootr   s
             r7   r   zColorScheme.load6  s    h'	--"22C>>&)//D !;;F)++CNN8,D,J,JKv===#33CrB3 "(!,!@!@'!) * $>>sCMvu%*}$(!)1&)2&%)" GGI {{  ++ 	<MM' #9-5.22h.?.?*5b/	; < <	< GGIs*   D8E 'G >G>G GG Gc                 f   g }| j                   D ]  }|j                  t        j                  j                  k(  s,|j
                  dk(  rt        j                  |      }n|j
                  dk(  rt        j                  |      }nr|j
                  dk(  rt        j                  |      }nM|j
                  dk(  rt        j                  ||      }n'|j
                  dk(  rt        j                  |      }nd}|s||_        t        j                  |||      |_        |j                  |       ! |S )z> Recursive function to parse all dom nodes of the layout tree r@  r   rN  r  colorN)
childNodesnodeTyper   NodeELEMENT_NODEtagNamer\   _parse_window_parse_layer_parse_icon_parse_key_group_parse_colorparentre  r   r   )dom_nodeparent_item	used_keysr   childrA  s         r7   re  zColorScheme._parse_dom_nodeh  s     (( 	'E~~!:!::==H,&44U;D]]g-&33E:D]]f,&2259D]]k1&77yID]]g-&33E:DD"-DK!,!<!<UD)!TDJLL&%	'( r9   c                 d    | j                  d      r| j                  d   j                  |_        yy)z' Parses common properties of all items r   N)rc  r1   r   r   r   rA  s     r7   _parse_dom_node_itemz ColorScheme._parse_dom_node_item  s-     T"ood+11DG #r9   c                     t               }| j                  d      r| j                  d   j                  |_        t
        j                  | |       |S )Nr   )Windowrc  r1   r   r   r\   r  r  s     r7   ru  zColorScheme._parse_window  sB    xV$/55DI((t4r9   c                 F    t               }t        j                  | |       |S r   )Layerr\   r  r  s     r7   rv  zColorScheme._parse_layer  s    w((t4r9   c                 F    t               }t        j                  | |       |S r   )Iconr\   r  r  s     r7   rw  zColorScheme._parse_icon  s    v((t4r9   z[\w-]+(?:[.][\w-]+)?c                    t               }t        j                  | |       dj                  | j                  D cg c]'  }|j
                  |j                  k(  r|j                  ) c}      }t        j                  j                  |      D cg c]  }|s|	 }}|D ]  }||v st        t        d|             |j                  t        t        ||                   ||_        |S c c}w c c}w )Nr   ODuplicate key_id '{}' found in color scheme file. Key_ids must occur only once.)KeyGroupr\   r  rZ   rp  rq  	TEXT_NODEdata_key_ids_patternfindall
ValueErrorrR   updater   zipkey_ids)r   r}  rA  nr   r   r  r   s           r7   rx  zColorScheme._parse_key_group  s    z((t4 ww 61::4  6 7'88@@FMb"rMM  	2F"  *I *0"1 2 2	2 	c#sm,-!6Ms   ,C)C.C.c                    t               }t        j                  | |       | j                  d      r| j                  d   j
                  |_        | j                  d      rP| j                  d   j
                  }t        |dd       dz  t        |dd       dz  t        |dd       dz  g|_        | j                  d      r't        | j                  d   j
                        |_
        i }t        j                  | d	|       t        j                  | d
|       t        j                  | d|       t        j                  | d|       t        j                  | d|       t        j                  | d|       ||_        |S )Nr   r  r   r            r  r  r   r!  r"  r   r#  )KeyColorr\   r  rc  r1   r   r   r
   r  r   r  _parse_state_attibuter
  )r   rA  r   r
  s       r7   ry  zColorScheme._parse_color  sI   z((t4Y'??95;;DLU#OOE*00E*51:6s:*51:6s:*51:6s:<DH Y' !;!A!ABDL))$
EB))$	5A))$%@))$%@))$uE))$	5A
r9   c                     | j                  |      r0| j                  |   j                  dk(  }|||<   |dk(  r	|rd|d<   y y y y )Ntruer"  Tr!  )rc  r1   r   )r   r4   r
  r   s       r7   r  z!ColorScheme._parse_state_attibute  sR    T"OOD)//69EE$KxE"&h %*	 #r9   c           
         i dg ddg ddg ddg ddg ddg d	d
g ddg ddg ddg ddg ddg ddg ddg ddg ddg ddg dg dg dg dg dg dg dg dg dd}g }| j                  d      }|s| j                  d      }t        |      D ]  \  }}d}d}d}t               }	|j                  |      rP|j                  |   j
                  }
t        |
dd       dz  t        |
dd       dz  t        |
dd        dz  g|	_        |d!z   }|j                  |      r't        |j                  |   j
                        |	_	        d"|	_
        t               }|j                  |	g       |j                  |        i }d}g }| j                  d#      D ]h  }d$}|j                  d%      r"t        |j                  d%   j
                        }d&j                  |j                   D cg c]  }|j"                   c}      }t%        j&                  d'|      D cg c]  }|s|	 }}|D ]  }||v st)        t+        d(|             |j-                  t/        t1        ||                   g }t/        |j3                               D ];  }d}d}|j                  |      rK|j                  |   j
                  }
t        |
dd       dz  t        |
dd       dz  t        |
dd        dz  g}|d!z   }|j                  |      r"t        |j                  |   j
                        }||g d)}|D ]  }|j5                  |      s n d}|j5                  |      r%|t7        |      d }|j5                  d*      r|dd }n|}t               }	||	_        ||	_	        ||	_
        |r
|d+i|	_        ni |	_        |j                  |	       > t;               }|j                  |       ||_        |r|}X|j                  |       k |r"|j?                  |       |j                  |       |S c c}w c c}w ),z4 Load a color scheme and return it as a new object. r   r  hoveredr   r  zpressed-latchedzpressed-lockedlatchedr  r"  r  r#  r  r$  zstroke-hoveredzstroke-pressedzstroke-pressed-latchedzstroke-pressed-lockedzstroke-latchedzstroke-lockedzstroke-scannedrm   r  )zlabel-hoveredzlabel-pressedzlabel-pressed-latchedzlabel-pressed-lockedzlabel-latchedzlabel-lockedzlabel-scannedr&  r   paneNr   r   r  r  r  z-opacityrD  r  Fr6   r   z\w+(?:[.][\w-]+)?r  )r   r$  rm   r&  -T) r   	enumerater  rc  r1   r   r
   r  r   r  r   r  rj  r   boolrZ   rp  r  rer  r  rR   r  r   r  keys
startswithrH  r
  r  r  append_items)r{  color_defaultsr   rI  r   r   attribr  r  ro  r   oattribr}  r  
key_groupsr   default_groupr  r   xr  r   r1  elementsr   state_attribr  s                              r7   rh  z&ColorScheme._parse_legacy_color_scheme  s   .D.D .D &.D	
 %.D .D .D .D .D %.D %.D -.D ,.D %.D  $.D!" %.D#& .D'( /E.D.D.D.D.D.D.E9>  ..w7226:F!&) 	 HAuFCGJE!!&)((066/a
;C?"51:.s2"51:.s24	
 z)G!!'* %e&6&6w&?&E&E F(EMGEOOUG$LL)	 . 	
22;? I	-E
 "M!!), $U%5%5i%@%F%F G 77E,<,<=qAFF=>D"$**-A4"HNQAqNGN " 6Y&$W .M .4&5 6 66 T#gw"789F~2245 )) %%f-!,,V4::E-eAaj9#=!3E!AJ!?!C!3E!AJ!?!CEC
 !:-%%g.#E$4$4W$=$C$CDG{'/LH#+ )!,,W5!) #)((1'-c'lm'<'2237+7+;L'-$JE #EI$+EM$+EM#'3d&;&(MM%(S))V !
I' 'I!*!!),SI	-V ''
3LL(K >Ns   7P7
)P<1P<r   r   ))r   r   r   r   r   rb  rd  COLOR_SCHEME_WINDOW_COLORSrg  r8   r   r?   rD   r   r  r	  r0  rB  r,  rP  r   r[   r_   r]   r[  rV  r]  r   re  r  ru  rv  rw  r  compileUNICODEr  rx  ry  r  rh  r|   r9   r7   r\   r\     s    ")A ")A $+1a=4
 E E  IVdL(@:"H 7 7 : :   
 
  $    / /b  2 2 2
    
  
 "rzz"92::F .  2 ' ' R Rr9   r\   c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
ColorSchemeItemz" Base class of color scheme items c                     dt               vrdat        dz  adt        z  t        |       z   dz   dj                  d | j                  D              z   }t        dz  a|S )zl
        Recursively dumps the (sub-) tree starting from self.
        Returns a multi-line string.
        _levelr   z   
r   c              3   <   K   | ]  }|j                           y wr   )dumps)r   rA  s     r7   r   z(ColorSchemeItem.dumps.<locals>.<genexpr>~  s     ;tzz|;r   )globalsr  reprrZ   r   )r3   r   s     r7   r  zColorSchemeItem.dumpst  s\     79$F!&L4:%,ww;

;;<!r9   c                      yNFr|   r@   s    r7   	is_windowzColorSchemeItem.is_window      r9   c                      yr  r|   r@   s    r7   is_layerzColorSchemeItem.is_layer  r  r9   c                      yr  r|   r@   s    r7   is_iconzColorSchemeItem.is_icon  r  r9   c                      yr  r|   r@   s    r7   is_key_groupzColorSchemeItem.is_key_group  r  r9   c                      yr  r|   r@   s    r7   r=  zColorSchemeItem.is_color  r  r9   c                     | j                         r|| j                  v r| S | j                  D ]  }|j                  |      }|s|c S  y)z$ Find the key group that has key_id N)r  r  r   r   )r3   r   r~  rA  s       r7   r   zColorSchemeItem.find_key_id  sO    ${ZZ 	E$$V,D	
 r9   N)r   r   r   r   r  r  r  r  r  r=  r   r|   r9   r7   r  r  q  s(    ,r9   r  c                   (    e Zd ZdZd Zd Zd Zd Zy)ri  z Container for a layers colors c                 p    g }| j                   D ]$  }|j                         s|j                  |       & |S )z]
        Get list of window in order of appearance
        in the color scheme file.
        )r   r  r   )r3   r?  rA  s      r7   r<  zRoot.get_windows  s9    
 JJ 	%D~~t$	% r9   c                 p    g }| j                   D ]$  }|j                         s|j                  |       & |S )zb
        Get list of layer items in order of appearance
        in the color scheme file.
        )r   r  r   )r3   rI  rA  s      r7   rF  zRoot.get_layers  s8    
 JJ 	$D}}d#	$ r9   c                 p    g }| j                   D ]$  }|j                         s|j                  |       & |S )ze
        Get list of the icon items in order of appearance
        in the color scheme file.
        )r   r  r   )r3   rM  rA  s      r7   rL  zRoot.get_icons  s8    
 JJ 	#D||~T"	# r9   c                 N    | j                   D ]  }|j                         s|c S  y)z> Default key group for keys that aren't part of any key group N)r   r  )r3   r~  s     r7   r  zRoot.get_default_key_group  s+    ZZ 	E!!#	 r9   N)r   r   r   r   r<  rF  rL  r  r|   r9   r7   ri  ri    s    )			r9   ri  c                       e Zd ZdZdZd Zy)r  z! Container for a window's colors r   c                      yNTr|   r@   s    r7   r  zWindow.is_window      r9   N)r   r   r   r   r   r  r|   r9   r7   r  r    s    +Dr9   r  c                       e Zd ZdZd Zy)r  z  Container for a layer's colors c                      yr  r|   r@   s    r7   r  zLayer.is_layer  r  r9   N)r   r   r   r   r  r|   r9   r7   r  r    
    *r9   r  c                       e Zd ZdZd Zy)r  z  Container for a Icon's' colors c                      yr  r|   r@   s    r7   r  zIcon.is_icon  r  r9   N)r   r   r   r   r  r|   r9   r7   r  r    r  r9   r  c                   .    e Zd ZdZdZdZdZd Zd Zd Z	y)Colorz A single color, rgb + opacity Nc                     dj                  t        j                  |       t        | j                        t        | j
                        t        | j                              S )Nz{} element={} rgb={} opacity={})r   r  __repr__r  r   r  r  r@   s    r7   r  zColor.__repr__  sF    077$3$<$<T$B$($6$(N$($6	8 	8r9   c                      yr  r|   r@   s    r7   r=  zColor.is_color  r  r9   c                      | j                   |k(  S )zD
        Returns true if self matches the given parameters.
        )r   )r3   r   argss      r7   matcheszColor.matches  s     ||w&&r9   )
r   r   r   r   r   r  r  r  r=  r  r|   r9   r7   r  r    s#    )G
CG8'r9   r  c                        e Zd ZdZdZd Zd Zy)r  z(
    A single key (or layer) color.
    Nc           	          dj                  t        j                  |       t        | j                        t        | j
                        t        | j                        t        | j                              S )Nz({} element={} rgb={} opacity={} state={})r   r  r  r  r   r  r  r
  r@   s    r7   r  zKeyColor.__repr__  sR    9@@$3$<$<T$B$($6$(N$($6$($46 	6r9   c                 "   d}| j                   |k(  syt        |j                               D ]`  \  }}|}|dk(  r|dv r|| j                  vrd}|dk(  s|dk(  r|dv r|| j                  vrd}| j                  j	                  ||      |k7  s_d}b |S )z
        Returns true if self matches the given parameters.
        state attributes match if they are equal or None, i.e. an
        empty state dict always matches.
        TFr   )r!  r"  r   r#  rm   r%  )r   )r   r   r   r
  re   )r3   r   r
  r   attrr   r6   s          r7   r  zKeyColor.matches  s     ||w&. 	KD% G& AA4::%7"g1B&B&4::%

tW-6+	. r9   )r   r   r   r   r
  r  r  r|   r9   r7   r  r    s     E6"r9   r  c                   &    e Zd ZdZdZd Zd Zd Zy)r  z% A group of key ids and their colors r|   c                 r    dj                  t        j                  |       t        | j                              S )Nz{} key_ids={})r   r  r  r  r  r@   s    r7   r  zKeyGroup.__repr__+  s.    %%o&>&>t&D$($68 	8r9   c                      yr  r|   r@   s    r7   r  zKeyGroup.is_key_group/  r  r9   c                 H   d }d }| j                         D ]  }|j                         s|j                  D ]f  }|j                         s|j	                         D ]@  }|j                  ||      s||j                  }||j                  }|5|8||fc c c S  h  ||fS r   )iter_to_rootr  r   r=  iter_depth_firstr  r  r  )r3   r   r
  r  r  r  r~  ro  s           r7   r  zKeyGroup.find_element_color2  s     **, 	8I%%' '__ 8E~~'%*%;%;%= 	8E  %}}We<#&;*/))C#*?.3mmG'*{7?+.<$7	88		8" G|r9   N)r   r   r   r   r  r  r  r  r|   r9   r7   r  r  '  s    /G8r9   r  )+r   
__future__r   r   r   logging	getLoggerrP   r   xml.domr   r   r<   r  r-  mathr   Onboardr	   Onboard.utilsr
   r   r   r   r   r   r   r   r   Onboard.Configr   rS   r   objectr\   r  ri  r  r  r  r  r  r  r|   r9   r7   <module>r     s   ( B A 
'

L
)   
 	 	   *( ( (  "	w wt@& @F'h 'T)? )X_ O ? 'O ',0u 0f! !r9   