U
    >qaE                     @   s   d Z ddlZddlZddlmZ G dd deZG dd de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G dd dZG dd deZdS )a  
*******************************************************************
  Copyright (c) 2017, 2019 IBM Corp.

  All rights reserved. This program and the accompanying materials
  are made available under the terms of the Eclipse Public License v2.0
  and Eclipse Distribution License v1.0 which accompany this distribution.

  The Eclipse Public License is available at
     http://www.eclipse.org/legal/epl-v10.html
  and the Eclipse Distribution License is available at
    http://www.eclipse.org/org/documents/edl-v10.php.

  Contributors:
     Ian Craggs - initial implementation and/or documentation
*******************************************************************
    N   )PacketTypesc                   @   s   e Zd ZdS )MQTTExceptionN__name__
__module____qualname__ r	   r	   W/var/www/html/flasktest/Project_env/lib/python3.8/site-packages/paho/mqtt/properties.pyr      s   r   c                   @   s   e Zd ZdS )MalformedPacketNr   r	   r	   r	   r
   r      s   r   c                 C   s   t td| S )N!H	bytearraystructpacklengthr	   r	   r
   
writeInt16!   s    r   c                 C   s   t d| d d d S )Nr      r   r   unpackbufr	   r	   r
   	readInt16&   s    r   c                 C   s   t td| S )N!Lr   r   r	   r	   r
   
writeInt32+   s    r   c                 C   s   t d| d d d S )Nr      r   r   r   r	   r	   r
   	readInt320   s    r   c                 C   sH   t jd dk rt| d} nt| tdkr.| nt| d} tt| |  S )Nr      utf-8    )sysversion_infor   typebytesr   len)datar	   r	   r
   writeUTF5   s    r'   c                 C   s   |dkrt | }ntd|d8 }||kr2td| dd|  d}|D ]D}t|}|dkrp|dkrptd|dkrtd	|d
krLtdqL||d fS )Nr   z%Not enough data to read string lengthz Length delimited string too longr   i   i  z,[MQTT-1.5.4-1] D800-DFFF found in UTF-8 datar   z'[MQTT-1.5.4-2] Null found in UTF-8 datai  z#[MQTT-1.5.4-3] U+FEFF in UTF-8 data)r   r   decodeord)buffermaxlenr   r   cZord_cr	   r	   r
   readUTF>   s     

r-   c                 C   s   t t| |  S N)r   r%   )r*   r	   r	   r
   
writeBytesS   s    r/   c                 C   s    t | }| dd|  |d fS )Nr   )r   )r*   r   r	   r	   r
   	readBytesW   s    r0   c                   @   s(   e Zd ZdZedd Zedd ZdS )VariableByteIntegerszh
    MQTT variable byte integer helper class.  Used
    in several places in MQTT v5.0 properties.

    c                 C   s|   d|   krdksn t d}| d }| d } | dkr>|dO }tjd dkr\|t|g7 }n|tt|7 }| dkrqxq|S )z
          Convert an integer 0 <= x <= 268435455 into multi-byte format.
          Returns the buffer convered from the integer.
        r   r       r   )AssertionErrorr!   r"   r$   chr)xr*   digitr	   r	   r
   encodec   s    zVariableByteIntegers.encodec                 C   sX   d}d}d}|d7 }| d }| dd } ||d@ | 7 }|d@ dkrFqP|d9 }q||fS )z
          Get the value of a multi-byte integer from a buffer
          Return the value, and the number of bytes used.

          [MQTT-1.5.5-1] the encoded value MUST use the minimum number of bytes necessary to represent the value
        r   r   N   r3   r	   )r*   Z
multipliervaluer$   r7   r	   r	   r
   r(   x   s    
zVariableByteIntegers.decodeN)r   r   r   __doc__staticmethodr8   r(   r	   r	   r	   r
   r1   \   s
   
r1   c                   @   sx   e Zd 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d Zdd Zdd ZdS )
Propertiesab  MQTT v5.0 properties class.

    See Properties.names for a list of accepted property names along with their numeric values.

    See Properties.properties for the data type of each property.

    Example of use:

        publish_properties = Properties(PacketTypes.PUBLISH)
        publish_properties.UserProperty = ("a", "2")
        publish_properties.UserProperty = ("c", "3")

    First the object is created with packet type as argument, no properties will be present at
    this point.  Then properties are added as attributes, the name of which is the string property
    name without the spaces.

    c              %   C   s  || _ dddddddg| _dd	d
dddddddddddddddddddddd d!d"d#| _| jdtjtjgf| jdtjtjgf| jdtjtjgf| jdtjtjgf| jdtjtjgf| jdtjtjgf| jdtjtj	tj
gf| jdtj	gf| jdtj	gf| jdtjtj	tjgf| jdtjtj	tjgf| jdtjgf| jdtjgf| jdtjgf| jdtj	gf| jdtj	tj
gf| jdtj	tjtjtjtjtjtjtj
tjg	f| jdtjtj	gf| jdtjtj	gf| jdtjgf| jdtj	gf| jdtj	gf| jdtjtj	tjtjtjtjtjtjtjtjtjtj
tjtjgf| jdtjtj	gf| jdtj	gf| jdtj	gf| jdtj	gfd$| _d S )%NByteTwo Byte IntegerFour Byte IntegerVariable Byte IntegerBinary DataUTF-8 Encoded StringUTF-8 String Pairr   r   r      	                                       !   "   #   $   %   &   '   (   )   *   )zPayload Format IndicatorzMessage Expiry IntervalzContent TypezResponse TopiczCorrelation DatazSubscription IdentifierzSession Expiry IntervalzAssigned Client IdentifierzServer Keep AlivezAuthentication MethodzAuthentication DatazRequest Problem InformationzWill Delay IntervalzRequest Response InformationzResponse InformationzServer ReferencezReason StringzReceive MaximumzTopic Alias MaximumzTopic AliaszMaximum QoSzRetain AvailablezUser PropertyzMaximum Packet SizezWildcard Subscription Availablez!Subscription Identifier AvailablezShared Subscription Available)r   r   r   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   )
packetTypetypesnamesindexr   ZPUBLISHZWILLMESSAGEZ	SUBSCRIBEZCONNECTZCONNACKZ
DISCONNECTZAUTHZPUBACKZPUBRECZPUBRELZPUBCOMPZSUBACKZUNSUBACKZUNSUBSCRIBE
properties)selfr]   r	   r	   r
   __init__   s      !








    




       

zProperties.__init__c                 C   s   |  |dkS )N)rG   rX   )getIdentFromName)rb   compressedNamer	   r	   r
   allowsMultiple   s    zProperties.allowsMultiplec                 C   s6   d}| j  D ]"}||ddkr| j | } q2q|S )N  )r_   keysreplace)rb   re   resultnamer	   r	   r
   rd      s    
zProperties.getIdentFromNamec                 C   s  | dd}ddddg}||kr2t| || nb|dd | j D kr`td	t| j  | j| j| 	| d
 krtd|t
j| j f t|tg krF|dkr|d
k s|dkrtd| n||dkr|dk s|dkrtd| nV|dkr|d
k s|dkrtd| n*|dkrF|dkrF|d
krFtd| | |rt|tg krj|g}t| |rt| || }t| || d S )Nrh   ri   r]   r^   r_   ra   c                 S   s   g | ]}| d dqS )rh   ri   )rk   ).0anamer	   r	   r
   
<listcomp>	  s     z*Properties.__setattr__.<locals>.<listcomp>zProperty name must be one of r   z,Property %s does not apply to packet type %s)ZReceiveMaximumZ
TopicAliasi  z.%s property value must be in the range 1-65535)ZTopicAliasMaximumr   z.%s property value must be in the range 0-65535)ZMaximumPacketSizeZSubscriptionIdentifierr2   z2%s property value must be in the range 1-268435455)ZRequestResponseInformationZRequestProblemInformationZPayloadFormatIndicatorz %s property value must be 0 or 1)rk   object__setattr__r_   rj   r   strr]   ra   rd   r   ZNamesr#   rf   hasattr__getattribute__)rb   rm   r:   ZprivateVarsr	   r	   r
   rr     sf    

zProperties.__setattr__c                 C   sd   d}d}| j  D ]D}|dd}t| |r|s8|d7 }||d tt| | 7 }d}q|d7 }|S )	N[Trh   ri   z, z : F])r_   rj   rk   rt   rs   getattr)rb   r*   firstrm   re   r	   r	   r
   __str__/  s    
zProperties.__str__c                 C   s`   i }| j  D ]L}|dd}t| |rt| |}|dkrRt|trR| ||< q|||< q|S )Nrh   ri   ZCorrelationData)r_   rj   rk   rt   rx   
isinstancer$   hex)rb   r&   rm   re   valr	   r	   r
   json=  s    


zProperties.jsonc                 C   s6   d}| j  D ]"}|dd}t| |rd} q2q|S )NTrh   ri   F)r_   rj   rk   rt   )rb   rcrm   re   r	   r	   r
   isEmptyI  s    
zProperties.isEmptyc                 C   s4   | j  D ]$}|dd}t| |r
t| | q
d S )Nrh   ri   )r_   rj   rk   rt   delattr)rb   rm   re   r	   r	   r
   clearR  s    
zProperties.clearc                 C   s  d}|t |7 }|| jdkrNtjd dk r>|t|7 }n|t|g7 }n|| jdkrl|t|7 }n|| jdkr|t	|7 }n|| jdkr|t |7 }nj|| jdkr|t
|7 }nL|| jd	kr|t|7 }n.|| jd
kr|t|d t|d  7 }|S )Nr    r>   r   r   r?   r@   rA   rB   rC   rD   r   )r1   r8   r^   r`   r!   r"   r5   r$   r   r   r/   r'   )rb   
identifierr#   r:   r*   r	   r	   r
   writePropertyX  s&    zProperties.writePropertyc              	   C   s   d}| j  D ]x}|dd}t| |r| |}| j| d }| |rnt| |D ]}|| |||7 }qTq|| ||t| |7 }qt	
t|| S )Nr    rh   ri   r   )r_   rj   rk   rt   rd   ra   rf   rx   r   r1   r8   r%   )rb   r*   rm   re   r   	attr_typepropr	   r	   r
   r   n  s     


 

zProperties.packc                 C   s  || j dkr|d }d}n|| j dkr<t|}d}n|| j dkrZt|}d}n|| j dkrzt|\}}n|| j d	krt|\}}nn|| j d
krt||\}}nN|| j dkrt||\}}||d  }t||| \}}||f}||7 }||fS )Nr>   r   r   r?   r   r@   r   rA   rB   rC   rD   )r^   r`   r   r   r1   r(   r0   r-   )rb   r*   r#   propslenr:   valuelenZvalue1Z	valuelen1r	   r	   r
   readProperty  s,    zProperties.readPropertyc                 C   s&   d }| j D ]}| j | |kr
|}q
|S r.   )r_   )rb   r   r   rm   r	   r	   r
   getNameFromIdent  s
    
zProperties.getNameFromIdentc                 C   s   t jd dk rt|}|   t|\}}||d  }|}|dkrt|\}}||d  }||8 }| j| d }| |||\}}	||	d  }||	8 }| |}
|
	dd}| 
|st| |rtdt t| |
| q<| || fS )Nr   r   rh   ri   z+Property '%s' must not exist more than once)r!   r"   r   r   r1   r(   ra   r   r   rk   rf   rt   r   propertysetattr)rb   r*   r   ZVBIlenZpropslenleftr   ZVBIlen2r   r:   r   Zpropnamere   r	   r	   r
   r     s8      
zProperties.unpackN)r   r   r   r;   rc   rf   rd   rr   rz   r~   r   r   r   r   r   r   r   r	   r	   r	   r
   r=      s   U	-	r=   )r;   r   r!   Zpackettypesr   	Exceptionr   r   r   r   r   r   r'   r-   r/   r0   r1   rq   r=   r	   r	   r	   r
   <module>   s   	2