
    wi                         d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlZdade	de
eef         fdZdefdZd	eddfd
Zdedeeeeef         f         fdZdee         dee         fdZdS )    N)partial)CallableDictListSequenceTupleUnion   
port_rangereturnc                 z   dt           dt          fd}t                      }| D ]$} ||          r|                    |            n%t	          |           D ]$} ||          r|                    |            n%t          |          dk    rt          d|            t          t          |                    S )a  
    Get two available ports, one from the start and one from the end of the range

    :param port_range: Port range to try. Reverses the list and will then pick the first one available
    :raises OSError: If there are not enough ports available
    :return: Two available ports
    port_nrr   c                     	 t          j         t           j        t           j                  }|                    d| f           |                                 dS # t
          $ r Y dS w xY w)z
        Checks if a port is available

        :param port_nr: Port number to check
        :return: True if available, False otherwise
         TF)socketAF_INETSOCK_STREAMbindcloseOSError)r   ss     m/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/mpire/dashboard/utils.py_port_availablez0get_two_available_ports.<locals>._port_available   sg    	fnf.@AAAFFB=!!!GGIII4 	 	 	55	s   AA 
A&%A&   z@Dashboard Manager Server: there are not enough ports available: )	intboolsetaddreversedlenr   tuplesorted)r   r   available_portsr   s       r   get_two_available_portsr$      s          eeO  ?7## 	(((E	 J''  ?7## 	(((E	 ?q  eYceefff(()))    c                      t           S )zx
    Gets the stack level to use when obtaining function details (used for the dashboard)

    :return: Stack level
    DASHBOARD_FUNCTION_STACKLEVEL r%   r   get_stacklevelr*   3   s
     )(r%   
stacklevelc                 
    | a dS )z
    Sets the stack level to use when obtaining function details (used for the dashboard)

    :param stacklevel: Stack level
    Nr'   )r+   s    r   set_stacklevelr-   <   s     %/!!!r%   funcc                    d}d}t          j                    D ]K}|j        j        d                             d          d         dk    s|dk    r|}|dz  }|t
          k    r nLt          j        |j        d          j        }|t|d|                    |j        d                   dz            }t          |          }|j
        t          |          dz
  z
  }d	                    d
 |D                       }nd}t          | t                    r| j        } nCt!          | d          r3t          | t"          t$          j        t$          j        f          s| j        } 	 t          j        |           }| j        j        }| j        }n#  d}d}d}Y nxY w	 t5          j                    }	n# t8          $ r d}	Y nw xY w|	 dt;          j                     ||||j        ||d}
|
S )aq  
    Obtain function details, including:

    - function filename
    - function line number
    - function name
    - invoked from filename
    - invoked from line number
    - invoked code context

    :param func: Function to call each time new task arguments become available. When passing on the worker ID the
        function should receive the worker ID as its first argument. If shared objects are provided the function should
        receive those as the next argument. If the worker state has been enabled it should receive a state variable as
        the next argument
    :return: Function details dictionary
    Nr   __name__.mpirer
   
   )context c              3   >   K   | ]}|                                 V  d S )N)strip).0lines     r   	<genexpr>z'get_function_details.<locals>.<genexpr>m   s*      FF

FFFFFFr%   zN/A__call__zn/a@)userfunction_filenamefunction_line_nofunction_nameinvoked_filenameinvoked_line_noinvoked_code_context) inspectstackframe	f_globalssplitr(   getframeinfocode_contextindexfind_calling_lineslinenor    join
isinstancer   r.   hasattrtypetypesFunctionType
MethodTyper;   
getabsfile__code__co_firstlinenor0   getpassgetuserKeyErrorr   gethostnamefilename)r.   invoked_framer+   
frame_inforJ   rB   r>   r?   r@   r=   func_detailss              r   get_function_detailsr`   F   s+   ( MJmoo  
%j177<<Q?7JJj[\nn&M!OJ::: '(;RHHHUL#$Z\%7%78RST8U%V%VYZ%Z$Z[),77'.#l2C2Ca2GHxxFFFFFFF$   y	z	"	" :dT5CUW\Wg<h+i+i }#.t44=7!        $<<f&8&:&:<<):(8%2(5(>'6,8: :L s   'F FF' 'F65F6rJ   c                    d}d}d}d}d}t          t          |           d          D ]N\  }}t          |          D ]/}|r	||k    rd}|dv rd}|}|dk    r|dz  }d}$|d	k    r|dz  }0|r|dk    r nO| | d         S )
z
    Tries to find the lines corresponding to the calling function

    :param code_context: List of code lines
    :return: List of code lines
    r   FNr
   )start>   "'T)()	enumerater   )rJ   n_parentheses_groupsfound_parentheses_groupinside_stringinside_string_chline_nrr9   chs           r   rL   rL      s     #MG"8L#9#9CCC  4.. 	* 	*B  *)))$)M z!! $#%   s$)$*.'' s$)$ # 	';q'@'@E		""r%   )rX   rD   r   	functoolsr   typingr   r   r   r   r   r	   rR   r(   r   r$   r*   r-   strr`   rL   r)   r%   r   <module>rq      sH            ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?  ! %* %*U38_ %* %* %* %*P) ) ) ) )/s /t / / / /Mx MDeCHo1E,F M M M M`)#T#Y )#49 )# )# )# )# )# )#r%   