o
    Df#                     @   s  d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlZddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ zddlmZ ddlmZ ddlmZ W n e yw   dd ZdZ!dZ"Y nw e Z!e Z"G dd dZ#dd Z$G dd dej%Z&G dd dej'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j0Z1e, Z2e- Z3e. Z4e/ Z5e1 Z6e+ Z7dS )+z Click customizations for Celery.    N)OrderedDictupdate_wrapper)pformat)Any)Context	ParamType)cached_property)get_current_app)user_preload_options)text)mlevel)maybe_iso8601)	highlight)Terminal256Formatter)PythonLexerc                 O   s   | S )z2Place holder function in case pygments is missing. )sargskwargsr   r   H/home/ubuntu/webapp/venv/lib/python3.10/site-packages/celery/bin/base.pyr      s   r   c                   @   s|   e Zd ZdZdddZedd Zedd Zdd
dZdddZ	dddZ
dddZdd Zdd Zdd ZdddZd	S )
CLIContextzContext Object for the CLI.Fc                 C   s"   |pt  | _|| _|| _|| _dS )zInitialize the CLI context.N)r
   appno_colorquietworkdir)selfr   r   r   r   r   r   r   __init__%   s   
zCLIContext.__init__c                 C      | j ddddS )NOKgreenTfgboldstyler   r   r   r   r   ,      zCLIContext.OKc                 C   r   )NERRORredTr!   r$   r&   r   r   r   r(   0   r'   zCLIContext.ERRORNc                 K   s   | j r|S tj|fi |S N)r   clickr%   r   messager   r   r   r   r%   4   s   zCLIContext.stylec                 K   s:   | j rd|d< tj|fi | d S tj|fi | d S NFcolorr   r+   echosechor,   r   r   r   r2   :      zCLIContext.sechoc                 K   s:   | j rd|d< tj|fi | d S tj|fi | d S r.   )r   r+   r1   r,   r   r   r   r1   A   r3   zCLIContext.echoc                 K   sB   d|d< | j rd|d< tj|fi | d S tj|fi | d S )NTerrFr/   r0   r,   r   r   r   errorH   s
   zCLIContext.errorc                 C   s   t |tr| j| |fS t |tr5d|v sd|v r| |S tj|ddd}| js0t	|t
t}| j|fS t |tr?| j|fS | jt|fS )Nokr5   T   )	sort_keysindent)
isinstancelistr   pretty_listdictpretty_dict_ok_errorjsondumpsr   r   LEXER	FORMATTERstrr   )r   nr   r   r   r   prettyP   s   





zCLIContext.prettyc                    s    |sdS d  fdd|D S )Nz	- empty -
c                 3   s(    | ]} j d dd d| V  qdS )*white)r"    Nr$   ).0itemr&   r   r   	<genexpr>b   s    
z)CLIContext.pretty_list.<locals>.<genexpr>)joinr   rD   r   r&   r   r<   _   s
   zCLIContext.pretty_listc                 C   sV   z| j t| |d d dfW S  ty   Y nw | jt| |d d dfS )Nr6      r7   r5   )r   r   r9   rE   KeyErrorr(   rN   r   r   r   r>   f   s   zCLIContext.pretty_dict_ok_error c                 C   sf   |dkr	| j r	d S | j  r| j|ddd dpd}| | d|  |r/|r1| | d S d S d S )Nz<-rH   Tr!   rI   rQ   )r   r%   r1   )r   	directiontitlebody	show_bodydirstrr   r   r   say_chato   s   "zCLIContext.say_chat)Fr*   )rQ   F)__name__
__module____qualname____doc__r   r	   r   r(   r%   r2   r1   r5   rE   r<   r>   rW   r   r   r   r   r   "   s    






	r   c                    s    fdd}t | S )z6Extract preload options and return a wrapped callable.c                    s\   | j j}dd |jdg D }|r# fdd|D }tj||d | g|R i  S )Nc                 S   s   g | ]}|j qS r   )name)rJ   or   r   r   
<listcomp>}   s    z:handle_preload_options.<locals>.caller.<locals>.<listcomp>preloadc                    s   i | ]}| | qS r   r   )rJ   preload_optionr   r   r   
<dictcomp>   s    z:handle_preload_options.<locals>.caller.<locals>.<dictcomp>)senderr   options)objr   user_optionsgetr   send)ctxr   r   r   preload_optionsrf   fra   r   callerz   s   
z&handle_preload_options.<locals>.callerr   )rl   rm   r   rk   r   handle_preload_optionsx   s   
rn   c                       ,   e Zd ZdZ fddZ fddZ  ZS )CeleryOptionzCustomized option for Celery.c                    s.   | j r
|j| j  | _t j|g|R i |S r*   )default_value_from_contextre   defaultsuperget_default)r   ri   r   r   	__class__r   r   rt      s   zCeleryOption.get_defaultc                    s2   | dd| _| dd| _t j|i | dS )zInitialize a Celery option.
help_groupNrq   )poprw   rq   rs   r   r   r   r   ru   r   r   r      s   zCeleryOption.__init__)rX   rY   rZ   r[   rt   r   __classcell__r   r   ru   r   rp      s    rp   c                   @   s   e Zd ZdZdd ZdS )CeleryCommandzCustomized command for Celery.c              	   C   s   t  }| |D ])}||}|dur1t|dr(|jr(|t|jg | q|dg | q| D ]\}}|	| |
| W d   n1 sOw   Y  q6dS )z7Write all the options into the formatter if they exist.Nrw   Options)r   
get_paramsget_help_recordhasattrrw   
setdefaultrC   appenditemssectionwrite_dl)r   ri   	formatteroptsparamrvr\   
opts_groupr   r   r   format_options   s   
zCeleryCommand.format_optionsN)rX   rY   rZ   r[   r   r   r   r   r   r{      s    r{   c                       s:   e Zd ZdZ fddZdedededefdd	Z  Z	S )
DaemonOptionzCommon daemonization optionc                    s0   t  j|f|dd|d| jd| d S )Nrw   zDaemonization Optionscallback)rw   r   )rs   r   rx   daemon_settingry   ru   r   r   r      s   


zDaemonOption.__init__ri   optvaluereturnc                 C   s&   |pt |jjj|jj d| j dS )z
        Try to fetch deamonization option from applications settings.
        Use the daemon command name as prefix (eg. `worker` -> `worker_pidfile`)
        _N)getattrre   r   confcommandr\   )r   ri   r   r   r   r   r   r      s   &zDaemonOption.daemon_setting)
rX   rY   rZ   r[   r   r   rp   r   r   rz   r   r   ru   r   r      s    "r   c                       s    e Zd ZdZ fddZ  ZS )CeleryDaemonCommandzDaemon commands.c                    s`   t  j|i | | jtddddtdddtdddtd	d
dtdddtdddf dS )z7Initialize a Celery command with common daemon options.z	--logfilez-fz#Log destination; defaults to stderr)helpz	--pidfilez&PID file path; defaults to no PID filez--uidz Drops privileges to this user IDz--gidz!Drops privileges to this group IDz--umaskz,Create files and directories with this umaskz--executablez&Override path to the Python executableN)rs   r   paramsextendr   ry   ru   r   r   r      s   





zCeleryDaemonCommand.__init__)rX   rY   rZ   r[   r   rz   r   r   ru   r   r      s    r   c                   @      e Zd ZdZdZdd ZdS )CommaSeparatedListzComma separated list argument.zcomma separated listc                 C   s
   t |S r*   )r   str_to_listr   r   r   ri   r   r   r   convert   s   
zCommaSeparatedList.convertNrX   rY   rZ   r[   r\   r   r   r   r   r   r          r   c                   @   r   )	JsonArrayzJSON formatted array argument.z
json arrayc              
   C   n   t |tr|S zt|}W n ty' } z| t| W Y d }~nd }~ww t |ts5| | d |S )Nz was not an array)r:   r;   r?   loads
ValueErrorfailrC   r   r   r   ri   ver   r   r   r         

zJsonArray.convertNr   r   r   r   r   r      r   r   c                   @   r   )
JsonObjectzJSON formatted object argument.zjson objectc              
   C   r   )Nz was not an object)r:   r=   r?   r   r   r   rC   r   r   r   r   r      r   zJsonObject.convertNr   r   r   r   r   r      r   r   c                   @   r   )ISO8601DateTimezISO 8601 Date Time argument.z	iso-86091c              
   C   s@   zt |W S  ttfy } z| | W Y d }~d S d }~ww r*   )r   	TypeErrorr   r   r   r   r   ri   r   r   r   r   r     s   
zISO8601DateTime.convertNr   r   r   r   r   r      r   r   c                   @   r   )ISO8601DateTimeOrFloatz%ISO 8601 Date Time or float argument.ziso-86091 or floatc              
   C   sb   zt |W S  ttfy   Y nw zt|W S  ttfy0 } z| | W Y d }~d S d }~ww r*   )floatr   r   r   r   r   r   r   r   r     s   

zISO8601DateTimeOrFloat.convertNr   r   r   r   r   r     r   r   c                       ro   )LogLevelzLog level option.c                    s   t  d dS )z:Initialize the log level option with the relevant choices.)DEBUGINFOWARNINGr(   CRITICALFATALN)rs   r   r&   ru   r   r   r     r'   zLogLevel.__init__c                    s0   t |tjr|S | }t |||}t|S r*   )r:   numbersIntegralupperrs   r   r   r   ru   r   r   r   #  s
   zLogLevel.convert)rX   rY   rZ   r[   r   r   rz   r   r   ru   r   r     s    r   )8r[   r?   r   collectionsr   	functoolsr   pprintr   typingr   r+   r   r   kombu.utils.objectsr	   celery._stater
   celery.signalsr   celery.utilsr   celery.utils.logr   celery.utils.timer   pygmentsr   pygments.formattersr   pygments.lexersr   ImportErrorrA   rB   r   rn   Optionrp   Commandr{   r   r   r   r   r   r   r   Choicer   
JSON_ARRAYJSON_OBJECTISO8601ISO8601_OR_FLOAT	LOG_LEVELCOMMA_SEPARATED_LISTr   r   r   r   <module>   sX    V	
