o
    z!h                      @   s   d dl Z d dlZddlmZ g Ze dZdZd ZdZ	ddd	Z
d
d Zdd Zdd Zdd Zdd Zed ddddZdddZdS )    N   )profilez
\W|^(?=\d)))/z per )%percent)*z times )r   i  i  i   i  i <  i (  i  i  i l  i x  i  i P  i  i  i D  Fc                 C   s.   |rt D ]\}}| |t|} qtd| S )z/Create a valid Python name out of *method_name*_)UNIT_NAME_TO_FUNC_REPLACEMENTSreplacestrMETHOD_NAME_SCRUBBERsub)method_nameconvert_unitsreplace_from
replace_to r   E/var/www/html/gps/gps/lib/python3.10/site-packages/fitdecode/utils.pyscrub_method_name   s   r   c                 C   sR   zt j|  W S  ty   Y nw t j D ]}| |jkr |  S qtd|  d)z
    Get a :class:`fitdecode.MessageType` from ``profile``, by its name (`str`)
    or its global number (`int`).

    Raise `ValueError` if type was not found.
    message type "" not found)r   MESSAGE_TYPESKeyErrorvaluesname
ValueError)mesg_name_or_num	mesg_typer   r   r   get_mesg_type!   s   
r   c                 C   s4   t j D ]}| |jkr|j  S qtd|  d)z
    Get the global number of a message as defined in ``profile``, by its name

    Raise `ValueError` if type was not found.
    r   r   )r   r   r   r   mesg_numr   )	mesg_namer   r   r   r   get_mesg_num5   s
   

r!   c                 C   sB   t | }|jD ]}||j|jfv r|  S qtd| d|  d)z
    Get the :class:`fitdecode.types.Field` object of a particular field from a
    particular message.

    Raise `ValueError` if message or field was not found.
    field "" not found in message "")r   fieldsdef_numr   r   )r   field_name_or_numr   fieldr   r   r   get_mesg_fieldB   s   
r)   c                 C   s>   t | }|jD ]}|j|kr|j  S qtd| d|  d)z
    Get the definition number of a particular field from a particular message.

    Raise `ValueError` if message or field was not found.
    r"   r#   r$   )r   r%   r   r&   r   )r   
field_namer   r(   r   r   r   get_mesg_field_numS   s   


r+   c                 C   s,   zt j|  W S  ty   td|  dw )zs
    Get :class:`fitdecode.FieldType` by name from ``profile``.

    Raise `ValueError` if type was not found.
    zfield type "r   )r   
FIELD_TYPEr   r   )r*   r   r   r   get_field_typeb   s
   r-   )crcstartendc                C   s   |st | }||krJ t| || D ]0}t|d@  }|d? d@ }||A t|d@  A }t|d@  }|d? d@ }||A t|d? d@  A }q|S )z~
    Compute the CRC as per FIT definition, of *byteslike* object, from offset
    *start* (included) to *end* (excluded)
    r         i  )len
memoryview	CRC_TABLE)	bytesliker.   r/   r0   bytetmpr   r   r   compute_crcn   s   r9   Q?c                    s   |du st |trt |trJ |sdS g  d} fdd}	 z:| |dk r(dn|| }|dk r4|W S |s? s;|W S | W S |dksEJ  | |t|7 }||krX| W S W n tyg   t| Y nw q)a  
    Read from *istream* and do not return until *size* `bytes` have been read
    unless EOF has been reached.

    Return all the data read so far. The length of the returned data may still
    be less than *size* in case EOF has been reached.

    *nonblocking_reads_delay* specifies the number of seconds (float) to wait
    before trying to read from *istream* again in case `BlockingIOError` has
    been raised during previous call.
    Nr   c                     s    t  d tr	dnd} |  S )Nr        )
isinstancer   join)glueoutputr   r   _join   s   
zblocking_read.<locals>._joinTr:   )	r>   intboolreadappendr3   BlockingIOErrortimesleep)istreamsizenonblocking_reads_delaylen_readrC   chunkr   rA   r   blocking_read   s2    
rP   )F)r:   r;   )rerI   r<   r   __all__compiler   r	   	CRC_STARTr5   r   r   r!   r)   r+   r-   r9   rP   r   r   r   r   <module>   s    

	#