a
    û#¢jl  ã                   @   s  d dl Z d dlZd dlmZmZmZmZmZmZ g d¢Z	e 
e¡Ze jd  dkrXdd„ ZG dd„ deƒZd	d
„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é    N)ÚQualifiedNameÚLocalizedTextÚNodeIdÚStringÚValueÚvalueIsInternalType)Ú	ReferenceÚ
RefOrAliasÚNodeÚReferenceTypeNodeÚ
ObjectNodeÚVariableNodeÚVariableTypeNodeÚ
MethodNodeÚObjectTypeNodeÚDataTypeNodeÚViewNodeé   c                 C   s   | S ©N© ©Úsr   r   ú_/mnt/data/dev_pub/qqyu/v2.12.x/application/opensource/open62541/tools/nodeset_compiler/nodes.pyÚunicode   s    r   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )r   c                 C   s   || _ || _|| _|| _d S r   )ÚsourceÚreferenceTypeÚtargetÚ	isForward)Úselfr   r   r   r   r   r   r   Ú__init__   s    zReference.__init__c                 C   sJ   t | jƒ}| js|d }|d t | jƒ d }| jr<|d }|t | jƒ S )Nú<z--[z]--ú>)Ústrr   r   r   r   )r   Zretvalr   r   r   Ú__str__$   s    
zReference.__str__c                 C   s   t | ƒS r   ©r"   ©r   r   r   r   Ú__repr__-   s    zReference.__repr__c                 C   s   t | ƒt |ƒkS r   r$   ©r   Úotherr   r   r   Ú__eq__0   s    zReference.__eq__c                 C   s   |   |¡ S r   )r)   r'   r   r   r   Ú__ne__3   s    zReference.__ne__c                 C   s   t t| ƒƒS r   )Úhashr"   r%   r   r   r   Ú__hash__6   s    zReference.__hash__N)	Ú__name__Ú
__module__Ú__qualname__r   r#   r&   r)   r*   r,   r   r   r   r   r      s   	r   c                 C   s&   z
t | ƒW S  ty    |  Y S 0 d S r   )r   Ú	Exceptionr   r   r   r   r	   9   s    
r	   c                   @   s\   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )r
   c                 C   sT   d | _ d | _d | _d | _d | _d | _d | _tƒ | _d| _	d | _
d | _d | _i | _d S ©NF)ÚidÚ
browseNameÚdisplayNameÚdescriptionÚsymbolicNameÚ	writeMaskÚuserWriteMaskÚdictÚ
referencesÚhiddenÚmodelUriÚparentÚparentReferenceÚnamespaceMappingr%   r   r   r   r   @   s    zNode.__init__c                 C   s   | j jd t| jƒ d S )Nú(ú))Ú	__class__r-   r"   r2   r%   r   r   r   r#   O   s    zNode.__str__c                 C   s   t | ƒS r   r$   r%   r   r   r   r&   R   s    zNode.__repr__c                 C   s   d S r   r   r%   r   r   r   ÚsanitizeU   s    zNode.sanitizec                 C   s‚  dD ]}|  |¡rt| |¡ƒ| _q|j ¡ D ]’\}}|dkrJt|ƒ| _q.|dkr^t|ƒ| _	q.|dkrrt|ƒ| _
q.|dkr†t|ƒ| _q.|dkršt|ƒ| _q.|dkr®t|ƒ| _q.|dkr.t|ƒ| _q.|jD ]´}|j|jkrÚqÈ|jrÈ|jdkrút|jjƒ| _nn|jdk�rt|ƒ| _	nV|jdk�r*t|ƒ| _
n>|jdk�rJtt|jjƒƒ| _n|jdk�rhtt|jjƒƒ| _|jd	krÈ|  |¡ qÈd S )
N)r   ZNodeIDÚnodeidÚ
BrowseNameZDisplayNameZDescriptionZ	WriteMaskZUserWriteMaskÚEventNotifierÚSymbolicNameZ
References)ÚhasAttributer	   ÚgetAttributer2   Ú
attributesÚitemsr   r3   r   r4   r5   Úintr7   r8   ÚeventNotifierr   r6   Ú
childNodesÚnodeTypeÚELEMENT_NODEÚ
firstChildÚ	localNameÚdatar   ÚparseXMLReferences)r   Ú
xmlelementZidnameÚatÚavÚxr   r   r   ÚparseXMLX   sD    



zNode.parseXMLc           	      C   sŽ   |j D ]‚}|j|jkrqtt| jƒƒ}t|jjƒ}d }d}|j 	¡ D ].\}}|dkr^t|ƒ}qD|dkrDd| 
¡ v}qDd | jt||||ƒ< qd S )NTÚReferenceTypeZ	IsForwardÚfalse)rN   rO   rP   r	   r"   r2   rQ   rS   rJ   rK   Úlowerr:   r   )	r   rU   Úrefr   r   ZreftypeZforwardrV   rW   r   r   r   rT   ~   s    

zNode.parseXMLReferencesc                 C   sP   | j D ] }|jtdƒkr|js|  S q| j D ]}|j|v r.|js.|  S q.d S )Nz	ns=0;i=45)r:   r   r   r   )r   Úparentreftypesr]   r   r   r   ÚgetParentReferenceŽ   s    



zNode.getParentReferencec                 C   sD   | j D ](}|jtdƒkr|jr| j |= |  S qttƒ tƒ tƒ dƒS )Nz	ns=0;i=40F)r:   r   r   r   r   )r   r]   r   r   r   Ú
popTypeDef˜   s
    

zNode.popTypeDefc                 C   sÎ   t | jƒ|v rt|| j ƒ| _t| tƒs2t| tƒrPt | jƒ|v rPt|| j ƒ| _tƒ }| jD ]f}t |j	ƒ|v r~t||j	 ƒ|_	t |j
ƒ|v rœt||j
 ƒ|_
t |jƒ|v rºt||j ƒ|_d ||< q\|| _d S r   )r"   r2   r   Ú
isinstancer   r   ÚdataTyper9   r:   r   r   r   )r   ÚaliasesÚnew_refsr]   r   r   r   ÚreplaceAliasesŸ   s    

zNode.replaceAliasesc                 C   s    || j j | j _|| jj | j_t| dƒrFt| jtƒrF|| jj | j_tƒ }| jD ]<}||j	j |j	_||j
j |j
_||jj |j_d ||< qR|| _|| _d S )Nrb   )r2   Únsr3   Úhasattrra   rb   r   r9   r:   r   r   r   r?   )r   Z	nsMappingrd   r]   r   r   r   ÚreplaceNamespaces°   s    

zNode.replaceNamespacesN)r-   r.   r/   r   r#   r&   rC   rY   rT   r_   r`   re   rh   r   r   r   r   r
   ?   s   &
r
   c                   @   s   e Zd Zddd„Zdd„ ZdS )r   Nc                 C   s0   t  | ¡ d| _d| _d| _|r,t | |¡ d S )NFÚ )r
   r   Ú
isAbstractÚ	symmetricÚinverseNamer   rY   ©r   rU   r   r   r   r   ¿   s    
zReferenceTypeNode.__init__c                 C   s~   t  | |¡ |j ¡ D ]6\}}|dkr6d| ¡ v| _q|dkrd| ¡ v| _q|jD ]$}|j|j	krT|j
dkrTt|ƒ| _qTd S )NZ	Symmetricr[   Ú
IsAbstractZInverseName)r
   rY   rJ   rK   r\   rk   rj   rN   rO   rP   rR   r   rl   ©r   rU   rV   rW   rX   r   r   r   rY   Ç   s    

zReferenceTypeNode.parseXML)N©r-   r.   r/   r   rY   r   r   r   r   r   ¾   s   
r   c                   @   s   e Zd Zddd„Zdd„ ZdS )r   Nc                 C   s$   t  | ¡ d| _|r t | |¡ d S )Nr   )r
   r   rM   r   rY   rm   r   r   r   r   Õ   s    
zObjectNode.__init__c                 C   s6   t  | |¡ |j ¡ D ]\}}|dkrt|ƒ| _qd S )NrF   )r
   rY   rJ   rK   rL   rM   ©r   rU   rV   rW   r   r   r   rY   Û   s    zObjectNode.parseXML)Nrp   r   r   r   r   r   Ô   s   
r   c                   @   s&   e Zd Zddd„Zdd„ Zdd„ ZdS )	r   Nc                 C   sT   t  | ¡ d | _d | _g | _d| _d| _d| _d| _d | _	d | _
|rPt | |¡ d S )Né   g        F)r
   r   rb   Ú	valueRankÚarrayDimensionsÚaccessLevelÚuserAccessLevelÚminimumSamplingIntervalÚhistorizingÚvalueÚxmlValueDefr   rY   rm   r   r   r   r   â   s    
zVariableNode.__init__c                 C   sð  t  | |¡ |j ¡ D ]˜\}}|dkr2t|ƒ| _q|dkrFt|ƒ| _q|dkrZt|ƒ| _q|dkrnt|ƒ| _	q|dkr‚t
|ƒ| _q|dkr˜| d¡| _q|dkrd	| ¡ v| _q|jD �]2}|j|jkrÊq¶|jd
krÜ|| _q¶|jdkròt
|ƒ| _q¶|jdk�rtt|jjƒƒ| _q¶|jdk�rrt| jƒdk�rr| d¡}t|ƒ�rêt|d  d¡ƒD ]\}}| j |jj¡ �qTq¶|jdk�r’tt|jjƒƒ| _q¶|jdk�r²tt|jjƒƒ| _q¶|jdk�rÒtt|jjƒƒ| _	q¶|jdkr¶d	| ¡ v| _q¶d S )NÚ	ValueRankZAccessLevelZUserAccessLevelZMinimumSamplingIntervalÚDataTypeÚArrayDimensionsú,ZHistorizingr[   r   r   ZListOfUInt32ÚUInt32)r
   rY   rJ   rK   rL   rs   ru   rv   Úfloatrw   r	   rb   Úsplitrt   r\   rx   rN   rO   rP   rR   rz   r   rQ   rS   ÚlenÚgetElementsByTagNameÚ	enumerateÚappend)r   rU   rV   rW   rX   ÚelementsÚidxÚvr   r   r   rY   ñ   sL    




zVariableNode.parseXMLc                 C   sH   |  | j¡}|d u rdS | jd u r&dS tƒ | _| j | j|| |j¡ dS )NFT)ÚgetDataTypeNoderb   rz   r   ry   ÚparseXMLEncodingÚparser)r   ÚnodesetÚdataTypeNoder   r   r   ÚallocateValue  s    
zVariableNode.allocateValue)N)r-   r.   r/   r   rY   rŽ   r   r   r   r   r   á   s   
)r   c                   @   s   e Zd Zddd„Zdd„ ZdS )r   Nc                 C   s$   t  | ¡ d| _|r t | |¡ d S r1   )r   r   rj   r   rY   rm   r   r   r   r   )  s    
zVariableTypeNode.__init__c                 C   sl   t  | |¡ |j ¡ D ]\}}|dkrd| ¡ v| _q|jD ]*}|j|jkrNq<|j	dkr<d| ¡ v| _q<d S ©Nrn   r[   )
r   rY   rJ   rK   r\   rj   rN   rO   rP   rR   ro   r   r   r   rY   /  s    

zVariableTypeNode.parseXML)Nrp   r   r   r   r   r   (  s   
r   c                   @   s   e Zd Zddd„Zdd„ ZdS )r   Nc                 C   s0   t  | ¡ d| _d| _d | _|r,t | |¡ d S )NT)r
   r   Ú
executableÚuserExecutableZmethodDecalarationr   rY   rm   r   r   r   r   <  s    
zMethodNode.__init__c                 C   sb   t  | |¡ |j ¡ D ]F\}}|dkr4d| ¡ v| _|dkrJd| ¡ v| _|dkrt|ƒ| _qd S )NZ
Executabler[   ZUserExecutableZMethodDeclarationId)	r
   rY   rJ   rK   r\   r�   r‘   r"   ZmethodDeclarationrq   r   r   r   rY   D  s    zMethodNode.parseXML)Nrp   r   r   r   r   r   ;  s   
r   c                   @   s   e Zd Zddd„Zdd„ ZdS )r   Nc                 C   s$   t  | ¡ d| _|r t | |¡ d S r1   )r
   r   rj   r   rY   rm   r   r   r   r   O  s    
zObjectTypeNode.__init__c                 C   s:   t  | |¡ |j ¡ D ]\}}|dkrd| ¡ v| _qd S r�   )r
   rY   rJ   rK   r\   rj   rq   r   r   r   rY   U  s    zObjectTypeNode.parseXML)Nrp   r   r   r   r   r   N  s   
r   c                   @   s<   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zddd„ZdS )r   a&   DataTypeNode is a subtype of Node describing DataType nodes.

        DataType contain definitions and structure information usable for Variables.
        The format of this structure is determined by buildEncoding()
        Two definition styles are distinguished in XML:
        1) A DataType can be a structure of fields, each field having a name and a type.
           The type must be either an encodable builtin node (ex. UInt32) or point to
           another DataType node that inherits its encoding from a builtin type using
           an inverse "hasSubtype" (hasSuperType) reference.
        2) A DataType may be an enumeration, in which each field has a name and a numeric
           value.
        The definition is stored as an ordered list of tuples. Depending on which
        definition style was used, the __definition__ will hold
        1) A list of ("Fieldname", Node) tuples.
        2) A list of ("Fieldname", int) tuples.

        A DataType (and in consequence all Variables using it) shall be deemed not
        encodable if any of its fields cannot be traced to an encodable builtin type.

        A DataType shall be further deemed not encodable if it contains mixed structure/
        enumaration definitions.

        If encodable, the encoding can be retrieved using getEncoding().
    Nc                 C   sH   t  | ¡ d| _d | _g | _d | _g | _d| _d| _|rDt	 
| |¡ d S r1   )r
   r   rj   Ú__xmlDefinition__Ú__baseTypeEncoding__Ú__encodable__Ú__definition__Ú
__isEnum__Ú__isOptionSet__r   rY   rm   r   r   r   r   u  s    
zDataTypeNode.__init__c                 C   sb   t  | |¡ |j ¡ D ]\}}|dkrd| ¡ v| _q|jD ] }|j|jkr<|j	dkr<|| _
q<d S )Nrn   r[   Z
Definition)r
   rY   rJ   rK   r\   rj   rN   rO   rP   rR   r’   ro   r   r   r   rY   �  s    

zDataTypeNode.parseXMLc                 C   s   | j du rtdƒ‚| j S )a   Will return True if buildEncoding() was able to determine which builtin
            type corresponds to all fields of this DataType.

            If no encoding has been build yet an exception will be thrown.
            Make sure to call buildEncoding() first.
        Nú6Encoding needs to be built first using buildEncoding())r”   r0   r%   r   r   r   ÚisEncodableŒ  s    
zDataTypeNode.isEncodablec                 C   s&   | j du rtdƒ‚| j sg S | jS dS )ad   If the dataType is encodable, getEncoding() returns a nested list
            containing the encoding the structure definition for this type.

            If no encoding has been build yet an exception will be thrown.
            Make sure to call buildEncoding() first.

            If buildEncoding() has failed, an empty list will be returned.
        Nr˜   )r”   r0   r“   r%   r   r   r   ÚgetEncoding—  s
    	
zDataTypeNode.getEncodingr   Fc                 C   sÐ  dd|  d }|dkrd| _ | j durn| j rn|  ¡ rPt |t| jƒ d ¡ nt |t| jƒ d ¡ | jS d| _ |dkr¢t d	t| jƒ d
 t| jƒ d ¡ t| jj	ƒrü| jj	g| _d| _ t |t| jƒ d ¡ t dt| jƒ ¡ t d¡ | jS d}| j
D ]L}|j�r�q|jjdk�r|j|j }|du�rt|tƒ�r|} �qT�q| jdu �r^|du�rÒt |d t|jƒ d d t| jƒ ¡ |j||d |d�}	| ¡ �sºd| _ n| j| jj	|	ddg | _t| jƒdk�rt |d t| jƒ d t| jƒ d ¡ d| _ |dk�rX| j �s:t dt| jƒ ¡ nt dt| jƒ ¡ t d¡ | jS d}
|du�o„|jjdk�o„|jjdk}g }g }| jjD �]}|j|jk�r–d}d}d}d}d}d}|j ¡ D ]Î\}}|dk�rþt|ƒ}||jv �r˜|j| }nš|dk�rt|ƒ}n†|dk�r"�qÌnv|dk�r:t|ƒ}d}
n^|dk�rNt|ƒ}nJ|dk�rbt|ƒ}n6|d k�rvt|ƒ}n"|d!k�r†�qÌnt d"t|ƒ ¡ �qÌ|
�r¶| ||f¡ �q–nì|dk�rÄd#}t|ƒ}|dk�râ||j |_||jv�rüt d$ !|¡ƒ‚|j| }| ||g¡ t|jj	ƒ}t || d% | d& t|jƒ ¡ |j||d |d�}	|�rv| j||	|d'gg | _n| j||	|dgg | _| ¡ �s–d| _  �q¦�q–t| jƒdk�rØt| jd t"ƒ�rØ| jd | _�q¦|dk�r*d| _#|j||d(�}	| ¡ �sd| _ n| j| jj	|	dg | _|| _$| jS |
dk�rx| jd)g | _|| _$d| _%t |d* t|ƒ d+ t| j ƒ ¡ | jS |dk�rÄ| j �s t dt| jƒ ¡ nt dt| jƒ ¡ d| _%t d¡ || _$| jS ),a    buildEncoding() determines the structure and aliases used for variables
            of this DataType.

            The function will parse the XML <Definition> of the dataType and extract
            "Name"-"Type" tuples. If successful, buildEncoding will return a nested
            list of the following format:

            [['Alias1', ['Alias2', ['BuiltinType']]], [Alias2, ['BuiltinType']], ...]

            Aliases are fieldnames defined by this DataType or DataTypes referenced. A
            list such as ['DataPoint', ['Int32']] indicates that a value will encode
            an Int32 with the alias 'DataPoint' such as <DataPoint>12827</DataPoint>.
            Only the first Alias of a nested list is considered valid for the BuiltinType.

            Single-Elemented lists are always BuiltinTypes. Every nested list must
            converge in a builtin type to be encodable. buildEncoding will follow
            the first type inheritance reference (hasSupertype) of the dataType if
            necessary;

            If instead to "DataType" a numeric "Value" attribute is encountered,
            the DataType will be considered an enumeration and all Variables using
            it will be encoded as Int32.

            DataTypes can be either structures or enumeration - mixed definitions will
            be unencodable.

            Calls to getEncoding() will be iterative. buildEncoding() can be called
            only once per dataType, with all following calls returning the predetermined
            value. Use of the 'force=True' parameter will force the Definition to be
            reparsed.

            After parsing, __definition__ holds the field definition as a list. Note
            that this might deviate from the encoding, especially if inheritance was
            used.
        ú ú|ú+TNz (already analyzed)z"(already analyzed, not encodable!)r   zParsing DataType z (rA   Ú*zEncodable as: ri   é-   z&Attempting definition using supertype z for DataType rr   )rŒ   Úindentr?   r[   zNo viable definition for z found.zNot encodable (partial): FiÓ1  r|   ÚNamerG   r   r{   Z
IsOptionalr}   ZAllowSubTypeszUnknown Field Attribute zi=24zNode {} not found in nodesetz : z -> Útrue)rŒ   r?   ÚInt32z&Int32* -> enumeration with dictionary z encodable )&r”   r™   ÚloggerÚdebugr"   r“   r3   r2   r   Únamer:   r   r   ÚiÚnodesr   ra   r   r’   ÚbuildEncodingr‚   rf   rN   rO   rP   rJ   rK   rc   rL   Úwarnr…   r   r0   ÚformatÚlistr—   r•   r–   )r   rŒ   r    Úforcer?   ÚprefixZ
parentTyper]   Z
targetNodeZsubencZisEnumÚisOptionSetZenumDictZtypeDictrX   ZfnameZfdtypeZenumValrs   rt   Z
isOptionalrV   rW   ZfdTypeNodeIdZdtnoder   r   r   r©   ¨  sú    %&



*ÿ
*

$














$ÿ

"


$

zDataTypeNode.buildEncoding)N)r   FN)	r-   r.   r/   Ú__doc__r   rY   r™   rš   r©   r   r   r   r   r   [  s   
r   c                   @   s   e Zd Zddd„Zdd„ ZdS )r   Nc                 C   s*   t  | ¡ d| _d| _|r&t | |¡ d S r1   )r
   r   ÚcontainsNoLoopsrM   r   rY   rm   r   r   r   r   |  s
    
zViewNode.__init__c                 C   sP   t  | |¡ |j ¡ D ]4\}}|dkr4d| ¡ v| _|dkrd| ¡ v| _qd S )NZContainsNoLoopsr[   rF   )r
   rY   rJ   rK   r\   r±   rM   rq   r   r   r   rY   ƒ  s    zViewNode.parseXML)Nrp   r   r   r   r   r   {  s   
r   )ÚsysÚloggingÚ	datatypesr   r   r   r   r   r   Ú__all__Ú	getLoggerr-   r¤   Úversion_infor   Úobjectr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   Ú<module>   s&    
G  "