
    cV                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Zd dl
Zej        j        j                                        Zej        j        j                                        Zej        j        j                                        Zej        j        j                                        Zej        j        j                                        Z G d dej        ej        j        j        ej        j        j                  Z G d deej        j        j                  Z G d deej        j        j        ej        j        j                  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ej        j        j                  Z$ G d de$          Z% G d de%          Z& G d de          Z'd Z(dS )    Nc                       e Zd ZdZdZed             Zd' fd	Zed             Z	ed             Z
d Zd	 Zd
 Zd Zd Z fdZd Zed             Zed             Zej        d             Zed             Zed             Zd Zd Zd Zd(dZd Zd Zd Zd Zd Z d Z!ed             Z"d  Z# fd!Z$ fd"Z%d# Z&d$ Z'd% Z(d& Z) xZ*S ))	StyleRulea  StyleRule Object.

    A dictionary containing nested Styles and property:value pairs.

    Example structure::

        <ClassRule name="QCheckBox" dict={
            "color": <PropRule name="color" value="red" />,
            "background-color": <PropRule name="background-color" value="black" />,
            "indicator": <SubControlRule name="indicator" dict={
                "border": <PropRule name="border" value="1px solid green" />,
                "hover": <PseudoStateRule name="hover" dict={
                    "background-color": <PropRule name="background-color" value="green" />,
                    "border": <PropRule name="border value="0px transparent black" />
                } />
            } />
        } />

    Output format::

        <selector> {
            <property>: <value>;
            <property>: <value>;
            ...
        }

    Stylesheet output::

        QCheckBox {
            color: red;
            background-color: black;
        }
        QCheckBox::indicator {
            border: 1px solid green;
        }
        QCheckBox::indicator:hover {
            background-color: green;
            border: 0px transparent black;
        }

    z\*|\[[A-Za-z0-9='\"]+\]|\W*\w*c                 r    |                     dd          }t          j        | j        |          dd         S )a  Split the selector based on the _split_regex.

        Return a list of each component.
        Example::

            name = "QObject::subcontrol:pseudostate"
            return value = ["QObject", "::subcontrol", ":pseudostate"]

        :param name: String name

        -_N)replacerefindall_split_regex)clsselectors     /lib/python3.11/site-packages/qstylizer/style.pysplit_selectorzStyleRule.split_selectorF   s7     ##C--z#*H55crc::    Nc                 h   t          t          |                                            |r|                     |          nd| _        || _        |                                 | _        |                                 | _	        | 
                    |          | _        t          j                    | _        dS )a  Initialize the StyleRule dictionary.

        .. note:: All public variables will be put into ordered dictionary.

        :param name: The name of the StyleRule
        :param value: The property value
        :param parent:  The parent StyleRule

        N)superr   __init___sanitize_key_name_parentget_attributes_attributesget_attr_options_attr_options_sanitize_value_valuecollectionsOrderedDict_child_rules)selfnamevalueparent	__class__s       r   r   zStyleRule.__init__V   s     	i'')))15?T''---4
..00!2244**511'355r   c                 6   t          |           } | rK| d         dvrA| t          j        |           k    r)|                     d          st          j        |           } |                     dd                              dd                              dd	          S )
zmStrip the key of colons and replace underscores with dashes.

        :param key: A string variable

        r   )Q#[ z
qproperty-not_!: r   r   )str
inflectioncamelize
startswith
underscorer	   keys    r   r   zStyleRule._sanitize_keyi   s     #hh 	-3q6!55 	-#ATUXAYAY:Y 	-bebpbpq}b~b~ 	-',,CWVS!!WS"WS#		
r   c                     	 t          |           t          t          fv r|                     dd          S nE# t          $ r8 t          |           t          t
          fv r|                     dd          cY S Y nw xY w| S )zXStrip the value of any semi-colons.

        :param value: A value of any type

        ;r.   )typer/   unicoder	   	NameErrorbytes)r#   s    r   r   zStyleRule._sanitize_valuez   s    	.E{{sGn, .}}S"---. 	. 	. 	.E{{sEl* .}}S"-----. .	. s   26 =A87A8c                     |                      |          }||S d|v r|                     |          }|S |                     |          S )a_  Find or create a child rule from a string key.

        If the key rule already exists, return the rule.
        If there is a comma in requested key, return a StyleRuleList object.
        Otherwise create rules from the style rule names in the key and return
        the top level rule or property.

        :param name: The dictionary key

        N,)find_child_rulecreate_child_rule_listcreate_child_rules)r!   r"   r#   	rule_lists       r   find_or_create_child_rulez#StyleRule.find_or_create_child_rule   s]     $$T** 	L$; 	33D99I&&t,,,r   c                 V    |                      |          }|                     |          S )zaFind rule from key.

        Return the sanitized key's hash value in the ordered dict.

        )r   getr!   r5   s     r   r>   zStyleRule.find_child_rule   s'       %%xx}}r   c                 T    t          ||           }|                     ||           |S )zeCreate a StyleRuleList object and add it to ordered dict.

        :param name: String name

        r"   r$   )StyleRuleListset_child_rule)r!   r"   rA   s      r   r?   z StyleRule.create_child_rule_list   s2     "tD999	D),,,r   c                 .   |                      |          d         }|                    |d          d                             dd          }|                     |          }||                     |          }|r||k    r|                    |          S |S )a  Create child rules from selector string.

        Split the selector into individual components based on the _split_regex
        and recursively build the StyleRule hierarchy looping through
        the components.

        If selector is "QClass::subcontrol:pseudostate",
        curr_name is "QClass" and remaining is "::subcontrol:pseudostate"

        :param name: String to split

        r      r   r   r   )r   splitr	   r>   create_child_rulerB   )r!   r   	curr_name	remainingrules        r   r@   zStyleRule.create_child_rules   s     ''11!4	NN9a004<<S#FF	##I.. 	5)))44D 	=i/ 	=11)<<<r   c                 j    t          |          } |||           }|                     ||           |S )zCreate child rule from name.

        Determine subclass from name, create an instance of the subclass,
        then add it to ordered dict.

        :param name: String name

        rG   )
rule_classrI   )r!   r"   class_rP   s       r   rM   zStyleRule.create_child_rule   s@     D!!v4---D$'''r   c                    |                      |          }t          |t                    s'|                     |          }t	          |||           }|                     |            t          t          |           j        ||fi |S )zSet rule in ordered dictionary.)r"   r#   r$   )r   
isinstancer   r   PropRule_add_child_ruler   __setitem__)r!   r5   r#   kwargsr%   s       r   rI   zStyleRule.set_child_rule   s      %%%++ 	A((//E#U4@@@EU###1uY%%1#uGGGGGr   c                     |j         | j        vr|| j        |j         <   | j        | j                            |           dS dS )z^Add a rule to the _child_rules dictionary.

        :param rule: A StyleRule object.

        N)r   r    r   rW   r!   rP   s     r   rW   zStyleRule._add_child_rule   sU     = 11 	4/3Ddm,< 	/L((.....	/ 	/r   c                 h    | j         | j        r| j        ndS | j         j        | j        z   | j        z   S )zZGet the selector.

        Example::

            Object::subcontrol:pseudostate

        Nr.   )r   r"   r   scope_operatorr!   s    r   r   zStyleRule.selector   s;     < 	2 $	1499r1|$t'::TYFFr   c                     | j         rL| j        rE| j                             | j                  r&| j                             | j        d          d         S | j         S )zyReturn the name of the StyleRule (eg. "QCheckBox").

        Strip off the scope operator if it exists in name.

        rK   r   )r   r]   r2   rL   r^   s    r   r"   zStyleRule.name   s^     J 	@4. 	@:  !455	@:##D$7;;B??zr   c                 :    |                      |          | _        d S N)r   r   )r!   r"   s     r   r"   zStyleRule.name  s    ''--


r   c                     | j         S ra   )r   r^   s    r   r$   zStyleRule.parent  s
    |r   c                 ^    |                                  rdS |                                 rdS dS )a-  Get the scope operator.

        The scope operator is the "::" or ":" that is printed in front
        of the name of the StyleRule in the selector.

        Subclasses are expected to define the scope operator or else it will
        try to guess it based on its position in the hierarchy.

        r.   r-   ::)is_top_levelis_leafr^   s    r   r]   zStyleRule.scope_operator	  s7      	2\\^^ 	3tr   c                 n    | j                                         D ]}t          |t                    s dS dS )z|Determine if StyleRule is a leaf.

        StyleRule is a leaf its child rules dictionary contains only PropRules.

        FT)r    valuesrU   rV   r[   s     r   rf   zStyleRule.is_leaf  sE     %,,.. 	 	DdH-- uutr   c                 6    t          | j        t                    S )zxDetermine if StyleRule is top level.

        StyleRule is top level if its parent is of the StyleSheet class.

        )rU   r   
StyleSheetr^   s    r   re   zStyleRule.is_top_level%  s     $,
333r   c                     |                      d          }| j                                        D ]}||                     d          z  }|S )Convert all child rules into a single stirng in css format.

        Loop through all of the rules and generate a stylesheet string.

        F	recursive)toStringr    rh   )r!   
stylesheetrP   s      r   _to_string_recursivezStyleRule._to_string_recursive-  sU     ]]U]33
%,,.. 	9 	9D$--%-888JJr   Fc                    |r|                                  S d}d}d}d}| j        }|                                 D ]*\  }}|j        ||                    ||j                  z  }+|r |j        di t                      }|S )zqConvert to a single string in css format.

        :param recursive: Output all of the sub-style rules.

        {selector} {{
{properties}}}
    {}: {};
r.   N )rq   r   itemsr#   formatlocals)	r!   rn   rule_templateprop_template
propertiessheetr   r5   rP   s	            r   
_to_stringzStyleRule._to_string8  s      	/,,...9'
= 	D 	DICz Dm223
CCC
 	5(M(4468844Er   c                      | j         |i |S )zrConvert to a single string in css format.

        Use camelcase for function name to match PyQt/PySide.

        )r}   r!   argsrY   s      r   ro   zStyleRule.toStringL  s     t////r   c                     |                                 D ]Z\  }}d|v r)|                     |                              |           2|                     |                              |           [dS )z&Set property values in the style rule.r   N)rv   __getitem__setValue__getattribute__)r!   r   rY   r5   r#   s        r   _set_valueszStyleRule._set_valuesT  s}     ,,.. 	; 	;JCcz ;  %%..u5555%%c**33E::::		; 	;r   c                 R   t          |d         t                    r|d         j                                        D ]k\  }}|                     |          }|                                D ]:\  }}t          |t                    r t          j        |          }||_        |||<   ;jd S d S )Nr   )rU   r   r    rv   rB   copydeepcopyr   )r!   r   rY   r5   
child_rulerP   kvs           r   updatezStyleRule.update\  s    d1gy)) 		$#'7#7#=#=#?#? $ $Z55c::&,,.. $ $DAq!!Y// $ M!,,$(	"#Q$			$ 		$$ $r   c                       | j         |i | dS )zoSet property values in the style rule.

        Use camelcase for function name to match PyQt/PySide.

        N)r   r   s      r   	setValueszStyleRule.setValuesh  s"     	$)&)))))r   c                 :    |                      |          | _        dS )zSet property value.N)r   r   r!   r#   s     r   
_set_valuezStyleRule._set_valuep  s    **511r   c                 0    |                      |           dS )z\Set property value.

        Use camelcase for function name to match PyQt/PySide.

        N)r   r   s     r   r   zStyleRule.setValuet  s     	r   c                     | j         S ra   )r   r^   s    r   r#   zStyleRule.value|  s
    {r   c                 ,    |                      |          S )zOverride the retrieving of a value from dictionary.

        Find or create rule in the key's hash location.

        :param key: The dictionary key

        )rB   rE   s     r   r   zStyleRule.__getitem__  s     --c222r   c                     || j         v r(t          t          |                               |          S |                     |          S )zOverride the deletion of an attribute.

        If attribute starts with an underscore, delete the attribute from
        the object's __dict__ otherwise delete it from the ordered dict.

        :param name: String name of attribute to delete

        )__dict__r   r   __delattr____delitem__)r!   r"   r%   s     r   r   zStyleRule.__delattr__  sF     4=  	<D))55d;;;%%%r   c                     |                     d          r)t          t          |                               ||          S || j        v r!| j        |                             | |          S |                     ||          S )a  Override the setting of an attribute.

        If name is in the pre-defined attributes, call the attribute's
        descriptor's __set__ function. Otherwise, add it to ordered dict as-is.

        :param name: The attribute name
        :param val: The value to set

        r   )r2   r   r   __setattr__r   __set__rI   r!   r"   valr%   s      r   r   zStyleRule.__setattr__  s}     ??3 	=D))55dC@@@T%% 	=#D)11$<<<""4---r   c                 <   || j         v rd|v rO|                    dd          }t          j        |          }|d                                         |dd         z   }	 | j        |                             | |          S # t          $ r Y nw xY w | j        ||fi |S )a=  Override the setting of an attribute in ordered dict.

        If key is in pre-defined attributes, call attribute's descriptor's
        __set__ function. Otherwise add the value to ordered dict as-is.

        :param key: The hash key of the ordered dict
        :param value: The value to map to hash key

        r   r   r   rK   N)	r   r	   r0   r1   lowerr   r   KeyErrorrI   r!   r5   r#   rY   s       r   rX   zStyleRule.__setitem__  s     $$$ 	cz /kk#s++ )#..!fllnns122w.',44T5AAA   "t"388888s    A? ?
BBc           	      b   | j         }|                    |          }| j        |_        | j        |_        d|_        t          j                    |_        ||t          |           <   | j	        
                                D ].\  }}|dv r
t          ||t          j        ||                     /|                                 | 
                                D ]L\  }}t          |t                     r2t          j        ||          }||_        |                    ||           M| j        |_        |S )zOverride deepcopy.

        Make a deepcopy of all member attributes as well as all rule rules
        in ordered dictionary.

        N)r    )r%   __new__r   r   r   r   r   r    idr   rv   setattrr   r   clearrU   r   rI   )r!   memor   resultr   r   s         r   __deepcopy__zStyleRule.__deepcopy__  s    nS!!z)577RXXM'')) 	7 	7DAq%% FAt}Q556666JJLL 	, 	,DAq!Y'' ,M!T**"	%%a+++r   c                     d}d}| j         d                    | j                   }| j        d                    | j                  }d                    | j        j        ||          S )z/Set the representation to look like xml syntax.r.   Nzvalue={0!r} zname={0!r} z<{0} {1}{2}/>)r#   rw   r"   r%   __name__)r!   r   rY   
value_attr	name_attrs        r   __repr__zStyleRule.__repr__  sn    
	: 	;'..tz::J9 	8%,,TY77I%%N#Y

 
 	
r   c                 *    |                                  S )z-Call toString if StyleRule is cast to string.)ro   r^   s    r   __str__zStyleRule.__str__  s    }}r   )NNN)F)+r   
__module____qualname____doc__r   classmethodr   r   staticmethodr   r   rB   r>   r?   r@   rM   rI   rW   propertyr   r"   setterr$   r]   rf   re   rq   r}   ro   r   r   r   r   r   r#   r   r   r   rX   r   r   r   __classcell__r%   s   @r   r   r      s       ( (R 5L; ; [;6 6 6 6 6 6& 
 
 \
    \- - -&      ,  H H H H H	/ 	/ 	/ 
G 
G X
G 	 	 X	 
[. . [.   X   X 	 	 	4 4 4	 	 	   (0 0 0; ; ;
$ 
$ 
$* * *2 2 2     X3 3 3& & & & &. . . . . 9 9 9*  :

 

 

      r   r   c                   <    e Zd ZdZd Zd ZddZed             ZdS )	rj   z^The StyleSheet definition.

    Contains descriptors for all class and property options.

    c                 *    |                                  S )zDetermine if stylesheet is global scope.

        A StyleSheet is global scope if it has no rules.
        Resulting string should contain no brackets.
        ::

            background-color: red;
            border: none;

        )rf   r^   s    r   is_global_scopezStyleSheet.is_global_scope  s     ||~~r   c                     |                      d          }| j                                        D ]%\  }}|dk    r||                     d          z  }&|S )rl   Frm   *)ro   r    rv   )r!   rp   r5   rP   s       r   rq   zStyleSheet._to_string_recursive  sh     ]]U]33
*0022 	9 	9ICcz $--%-888JJr   Tc                    |r|                                  S d}d}| j        }|                                 rd}d}nd}d}d}|                                 D ]\  }}|dk    r|                     d                                          D ]Y\  }	}
t          |
t                    s||                    |	|
          z  }4|
j        ||                    |	|
j                  z  }Zt          |t                    s||                    ||          z  }|j        ||                    ||j                  z  }|r |j        di t                      }|S )	zReturn the selector and properties as a single string.

        :param recursive: Loop through all rules to generate a stylesheet.

        rs   rt   z{properties}z{}: {};
r   r.   Nru   )
rq   r   r   rv   r   rU   r   rw   r#   rx   )r!   rn   ry   rz   r   r{   r|   r5   r#   
global_keyglobal_values              r   r}   zStyleSheet._to_string
  s     	/,,...9'=!! 	*M'MMH
**,, 	E 	EJC cz 	040@0@0E0E0K0K0M0M  ,J%lI>> "m&:&:&' ' 

 &+ "m&:&:&(:' ' 
 eY// Em223>>>

 Em223DDD
 	5(M(4468844Er   c                     | j         S )z"Return the name of the StyleSheet.)r   r^   s    r   r"   zStyleSheet.name6  s     zr   N)T)	r   r   r   r   r   rq   r}   r   r"   ru   r   r   rj   rj     sl         
    * * * *X   X  r   rj   c                       e Zd ZdZdS )	ClassRulezThe ClassRule definition.

    Example class rule name: "QCheckBox".
    Contains descriptors for all subcontrols and pseudostates.

    Nr   r   r   r   ru   r   r   r   r   <  s           r   r   c                   (    e Zd ZdZed             ZdS )
ObjectRulezThe ObjectRule definition.

    Example object rule name: "#objectName".
    Inherits from ClassRule. Only difference is "#" is the scope operator.

    c                     dS )Nr(   ru   r^   s    r   r]   zObjectRule.scope_operatorO      sr   Nr   r   r   r   r   r]   ru   r   r   r   r   H  s9            X  r   r   c                   (    e Zd ZdZed             ZdS )ChildClassRulezThe ChildClassRule definition.

    Example object rule name: " QFrame".
    Inherits from ClassRule.
    ::

        QWidget QFrame {
            property: value
        }

    c                     dS )Nr*   ru   r^   s    r   r]   zChildClassRule.scope_operator`  r   r   Nr   ru   r   r   r   r   T  s9        
 
   X  r   r   c                   (    e Zd ZdZed             ZdS )ObjectPropRulez^The ObjectPropRule definition.

    Example object property rule name: "[echoMode="2"]".

    c                     dS Nr.   ru   r^   s    r   r]   zObjectPropRule.scope_operatork      rr   Nr   ru   r   r   r   r   e  s9         
   X  r   r   c                   p     e Zd ZdZed             Zd Zed             Z fdZ	d Z
ed             Z xZS )rH   zXThe StyleRuleList definition.

    Example rule list name: "QCheckBox, QComboBox".

    c                 H    t          |                               dd          S )zStrip the key of newlines only.
r.   )r/   r	   r4   s    r   r   zStyleRuleList._sanitize_keyw  s      3xxb)))r   c                     | j                             d          }|D ]0}| j                            |                              ||           1dS )a  Find or create value in parent StyleRule

        Will loop through all components in name separated by a comma and set the
        property in each of the rules in the parent StyleRule.

        :param name: The attribute name
        :param val: The value

        r=   N)r"   rL   r   rB   r   )r!   r"   r   
rule_names	rule_names        r   _create_child_rules_in_parentz+StyleRuleList._create_child_rules_in_parent|  s_     Y__S))
# 	 	IL229==IIc    tr   c                     dS r   ru   r^   s    r   r]   zStyleRuleList.scope_operator  r   r   c                     |                     d          r)t          t          |                               ||          S |                     ||          S )z}Override the setting of an attribute.

        :param name: The attribute name
        :param val: The value to set

        r   )r2   r   r   r   r   r   s      r   r   zStyleRuleList.__setattr__  sP     ??3 	AD))55dC@@@11$<<<r   c                 .    |                      ||          S )zOverride the setting of a value in ordered dict.

        :param key: The hash key of the ordered dict
        :param value: The value to map to hash key

        )r   r   s       r   rX   zStyleRuleList.__setitem__  s     11#u===r   c                 8    | j                             dd          S )zReturn the name with no spaces.r*   r.   )r   r	   r^   s    r   r"   zStyleRuleList.name  s     z!!#r***r   )r   r   r   r   r   r   r   r   r]   r   rX   r"   r   r   s   @r   rH   rH   p  s          * * \*  "   X	= 	= 	= 	= 	=> > > + + X+ + + + +r   rH   c                   (    e Zd ZdZed             ZdS )SubControlRulezQThe SubControlRule definition.

    Example subcontrol name: "::indicator".

    c                     dS )Nrd   ru   r^   s    r   r]   zSubControlRule.scope_operator  s    tr   Nr   ru   r   r   r   r     s9         
   X  r   r   c                   (    e Zd ZdZed             ZdS )PseudoStateRulezNThe PseudoStateRule definition.

    Example pseudostate name: ":hover".

    c                     dS )Nr-   ru   r^   s    r   r]   zPseudoStateRule.scope_operator  r   r   Nr   ru   r   r   r   r     s9         
   X  r   r   c                       e Zd ZdZdS )PseudoPropRulea4  The PseudoPropRule definition.

    The PseudoPropRule covers PseudoStates and properties that have the same
    name like "top", "bottom", "left", and "right".

    It is basically a PseudoStateRule that also stores a property value.
    In the following example, *top* is the PseudoPropRule.

    .. code-block:: python

        >>> css.QWidget.tab.top = "0"
        >>> css.QWidget.tab.top.color = "green"
        >>> print(css.toString())
        QWidget::tab {
            top: 0;
        }
        QWidget::tab:top {
            color: green;
        }

    Nr   ru   r   r   r   r     s           r   r   c                       e Zd ZdZdS )rV   zOThe PropRule definition.

    Example prop rule name: "background-color".

    Nr   ru   r   r   rV   rV     s           r   rV   c                     d| v r;|                      dd          } | d                                         | dd         z   } t          }|                     d          s	| t          v rt
          }n|                     d          s	| t          v rt          }nk|                     d          rt          }nN|                     d	          rt          }n1| t          v s|                     d
          rt          }nd| v rt          }|S )zZDetermine StyleRule subclass from string name.

    :param name: name of type string

    r,   r.   r   rK   Nrd   r-   r(   r*   r'   =)r	   r   r   r2   QSUBCONTROLSr   QPSEUDOSTATESr   r   r   QCLASSESr   r   )r"   rS   s     r   rR   rR     s    d{ *||C$$Aw}}abb)Ft   4  			 	 !6 	  			  			  		  T__S11  	  Mr   ))r
   r   r   r0   qstylizer.descriptor.prop	qstylizerqstylizer.descriptor.subcontrol qstylizer.descriptor.pseudostateqstylizer.descriptor.pseudopropqstylizer.descriptor.qclassqstylizer.descriptor.stylerule
descriptorprop
PropParentr   QPROPERTIES
subcontrolSubControlParentr   pseudostatePseudoStateParentr   
pseudopropPseudoPropParentQPSEUDOPROPSqclassClassStyleParentr   r   r   rj   r   r   r   r   rH   r   r   r   rV   rR   ru   r   r   <module>r      s@   
			                  & & & & ' ' ' ' & & & & " " " " % % % % "'2CCEE#.?PPRR$0BSSUU#.?PPRR&7HHJJP P P P PY16A#4P P PfO O O O OI07H O O Od	 	 	 	 	y#.?$6	 	 		 	 	 	 	 	 	 	    Y   "    Y   8+ 8+ 8+ 8+ 8+I 8+ 8+ 8+v    Y	 4 @ R       n       _   0    y       r   