o
    _J@h2                     @   s   d Z ddlZddl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mZ dd	lmZ dd
lmZmZ edZG dd deZdS )zAPScheduler implementation.    N)	EVENT_ALL)BackgroundScheduler)JobLookupError)make_response)get_debug_flag   )api)fix_job_defpop_triggerZflask_apschedulerc                   @   s&  e Zd ZdZd?ddZedd Zedd Zed	d
 Zedd Z	edd Z
dd Zd@ddZdAddZdd Zdd ZefddZdd Zdd  ZdBd!d"ZdBd#d$ZdBd%d&ZdBd'd(ZdBd)d*ZdBd+d,ZdBd-d.ZdBd/d0Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Z d;d< Z!d=d> Z"dS )CAPSchedulerz)Provides a scheduler integrated to Flask.Nc                 C   s\   |pt  | _t  | _d | _dg| _d | _d| _	d| _
d| _d | _|r,| | d S d S )N*Fz
/schedulerz
scheduler.)r   
_schedulersocketgethostnamelower
_host_name_authentication_callbackallowed_hostsauthapi_enabled
api_prefixendpoint_prefixappinit_app)self	schedulerr    r   N/var/www/html/venv/lib/python3.10/site-packages/flask_apscheduler/scheduler.py__init__$   s   zAPScheduler.__init__c                 C      | j S )zGet the host name.)r   r   r   r   r   	host_name3      zAPScheduler.host_namec                 C      | j jS )z*Get true whether the scheduler is running.)r   runningr    r   r   r   r$   8      zAPScheduler.runningc                 C   r#   )zGet the state of the scheduler.)r   stater    r   r   r   r&   =   r%   zAPScheduler.statec                 C   r   )zGet the base scheduler.)r   r    r   r   r   r   B   r"   zAPScheduler.schedulerc                 C   r#   )z Get the base scheduler decorator)r   Zscheduled_jobr    r   r   r   taskG   r%   zAPScheduler.taskc                 C   s4   || _ | | j _|   |   | jr|   dS dS )z=Initialize the APScheduler with a Flask application instance.N)r   Zapscheduler_load_config
_load_jobsr   	_load_api)r   r   r   r   r   r   L   s   zAPScheduler.init_appFc                 C   s`   t  r
tj s
dS | j| jvr'd| jvr'td| j dd| j  dS | j	j
|d dS )z~
        Start the scheduler.
        :param bool paused: if True, don't start job processing until resume is called.
        Nr   z
Host name z; is not allowed to start the APScheduler. Servers allowed: ,)paused)r   werkzeugZservingZis_running_from_reloaderr!   r   LOGGERdebugjoinr   start)r   r,   r   r   r   r1   X   s    zAPScheduler.startTc                 C      | j | dS )a
  
        Shut down the scheduler. Does not interrupt any currently running jobs.

        :param bool wait: ``True`` to wait until all currently executing jobs have finished
        :raises SchedulerNotRunningError: if the scheduler has not been started yet
        N)r   shutdown)r   waitr   r   r   r3   i   s   zAPScheduler.shutdownc                 C      | j   dS )z
        Pause job processing in the scheduler.

        This will prevent the scheduler from waking up to do job processing until :meth:`resume`
        is called. It will not however stop any already running job processing.
        N)r   pauser    r   r   r   r6   s   s   zAPScheduler.pausec                 C   r5   )z9
        Resume job processing in the scheduler.
        N)r   resumer    r   r   r   r7   |   s   zAPScheduler.resumec                 C      | j || dS )a  
        Add a listener for scheduler events.

        When a matching event  occurs, ``callback`` is executed with the event object as its
        sole argument. If the ``mask`` parameter is not provided, the callback will receive events
        of all types.

        For further info: https://apscheduler.readthedocs.io/en/latest/userguide.html#scheduler-events

        :param callback: any callable that takes one argument
        :param int mask: bitmask that indicates which events should be listened to
        N)r   add_listener)r   callbackmaskr   r   r   r9      s   zAPScheduler.add_listenerc                 C   r2   )z;
        Remove a previously added event listener.
        N)r   remove_listener)r   r:   r   r   r   r<      s   zAPScheduler.remove_listenerc                 K   sD   t |}||d< ||d< |dp||d< t| | jjdi |S )a  
        Add the given job to the job list and wakes up the scheduler if it's already running.

        :param str id: explicit identifier for the job (for modifying it later)
        :param func: callable (or a textual reference to one) to run at the given time
        idfuncnameNr   )dictgetr	   r   add_job)r   r=   r>   kwargsZjob_defr   r   r   rB      s   zAPScheduler.add_jobc                 C   r8   )z
        Remove a job, preventing it from being run any more.

        :param str id: the identifier of the job
        :param str jobstore: alias of the job store that contains the job
        N)r   
remove_jobr   r=   jobstorer   r   r   rD      s   zAPScheduler.remove_jobc                 C   r2   )z
        Remove all jobs from the specified job store, or all job stores if none is given.

        :param str|unicode jobstore: alias of the job store
        N)r   remove_all_jobsr   rF   r   r   r   rG      s   zAPScheduler.remove_all_jobsc                 C   s   | j ||S )a$  
        Return the Job that matches the given ``id``.

        :param str id: the identifier of the job
        :param str jobstore: alias of the job store that most likely contains the job
        :return: the Job by the given ID, or ``None`` if it wasn't found
        :rtype: Job
        )r   get_jobrE   r   r   r   rI      s   
zAPScheduler.get_jobc                 C   s   | j |S )z
        Return a list of pending jobs (if the scheduler hasn't been started yet) and scheduled jobs, either from a
        specific job store or from all of them.

        :param str jobstore: alias of the job store
        :rtype: list[Job]
        )r   get_jobsrH   r   r   r   rJ      s   	zAPScheduler.get_jobsc                 K   sJ   t | d|v rt|\}}| jj|||fi | | jj||fi |S )z
        Modify the properties of a single job. Modifications are passed to this method as extra keyword arguments.

        :param str id: the identifier of the job
        :param str jobstore: alias of the job store that contains the job
        trigger)r	   r
   r   Zreschedule_job
modify_job)r   r=   rF   changesrK   Ztrigger_argsr   r   r   rL      s
   zAPScheduler.modify_jobc                 C   r8   )z
        Pause the given job until it is explicitly resumed.

        :param str id: the identifier of the job
        :param str jobstore: alias of the job store that contains the job
        N)r   	pause_jobrE   r   r   r   rN         zAPScheduler.pause_jobc                 C   r8   )z
        Resume the schedule of the given job, or removes the job if its schedule is finished.

        :param str id: the identifier of the job
        :param str jobstore: alias of the job store that contains the job
        N)r   
resume_jobrE   r   r   r   rP      rO   zAPScheduler.resume_jobc                 C   s2   | j ||}|st||j|ji |j dS )z
        Run the given job without scheduling it.
        :param id: the identifier of the job.
        :param str jobstore: alias of the job store that contains the job
        :return:
        N)r   rI   r   r>   argsrC   )r   r=   rF   jobr   r   r   run_job   s   zAPScheduler.run_jobc                 C   s
   || _ |S )z
        A decorator that is used to register a function to authenticate a user.
        :param func: The callback to authenticate.
        )r   )r   r>   r   r   r   authenticate  s   zAPScheduler.authenticatec                 C   s   t  }| jjd}|r||d< | jjd}|r||d< | jjd}|r*||d< | jjd}|r7||d< | jjdi | | jjd	| j| _| jjd
| j| _| jjd| j| _| jjd| j| _| jjd| j	| _	| jjd| j
| _
dS )zF
        Load the configuration from the Flask configuration.
        ZSCHEDULER_JOBSTORESZ	jobstoresZSCHEDULER_EXECUTORS	executorsZSCHEDULER_JOB_DEFAULTSjob_defaultsZSCHEDULER_TIMEZONEtimezoneZSCHEDULER_AUTHZSCHEDULER_VIEWS_ENABLEDZSCHEDULER_API_ENABLEDZSCHEDULER_API_PREFIXZSCHEDULER_ENDPOINT_PREFIXZSCHEDULER_ALLOWED_HOSTSNr   )r@   r   configrA   r   	configurer   r   r   r   r   )r   optionsZ
job_storesrU   rV   rW   r   r   r   r(   
  s(   zAPScheduler._load_configc                 C   sF   | j jd}|s| j jd}|r|D ]}| jdi | qdS dS )zH
        Load the job definitions from the Flask configuration.
        ZSCHEDULER_JOBSZJOBSNr   )r   rX   rA   rB   )r   jobsrR   r   r   r   r)   )  s   zAPScheduler._load_jobsc                 C   s   |  ddtjd |  ddtjd |  ddtjd |  d	d
tjd |  ddtjd |  ddtjd |  ddtjd |  ddtj	d |  ddtj
d |  ddtjd |  ddtjd |  ddtjd |  ddtjd dS )z7
        Add the routes for the scheduler API.
        get_scheduler_info GETpause_schedulerz/pausePOSTresume_schedulerz/resumestart_schedulerz/startshutdown_schedulerz	/shutdownrB   z/jobsrI   z/jobs/<job_id>rJ   
delete_jobDELETE
update_jobPATCHrN   z/jobs/<job_id>/pauserP   z/jobs/<job_id>/resumerS   z/jobs/<job_id>/runN)_add_url_router   r\   r_   ra   rb   rc   rB   rI   rJ   rd   rf   rN   rP   rS   r    r   r   r   r*   6  s   zAPScheduler._load_apic                 C   s@   | j r| j | }| jr| j| }| jj||| ||gd dS )z
        Add a Flask route.
        :param str endpoint: The endpoint name.
        :param str rule: The endpoint url.
        :param view_func: The endpoint func
        :param str method: The http method.
        )methodsN)r   r   r   Zadd_url_rule_apply_auth)r   Zendpointrule	view_funcmethodr   r   r   rh   H  s   


zAPScheduler._add_url_routec                    s   t  fdd}|S )z
        Apply decorator to authenticate the user who is making the request.
        :param view_func: The flask view func.
        c                     sT    j s
| i |S  j  }|d u r  S  jr |s#  S | i |S N)r   get_authorization_handle_authentication_errorr   )rQ   rC   Z	auth_datar   rl   r   r   	decoratedb  s   
z*APScheduler._apply_auth.<locals>.decorated)	functoolswraps)r   rl   rr   r   rq   r   rj   ]  s   zAPScheduler._apply_authc                 C   s"   t d}| j |jd< d|_|S )z1
        Return an authentication error.
        zAccess DeniedzWWW-Authenticatei  )r   r   Zget_authenticate_headerheadersstatus_code)r   responser   r   r   rp   s  s   z(APScheduler._handle_authentication_error)NN)F)Trn   )#__name__
__module____qualname____doc__r   propertyr!   r$   r&   r   r'   r   r1   r3   r6   r7   r   r9   r<   rB   rD   rG   rI   rJ   rL   rN   rP   rS   rT   r(   r)   r*   rh   rj   rp   r   r   r   r   r   !   sH    








	



	



	
	r   )r{   rs   loggingr   r-   Zapscheduler.eventsr   Z!apscheduler.schedulers.backgroundr   Zapscheduler.jobstores.baser   Zflaskr   Zflask.helpersr   r]   r   utilsr	   r
   	getLoggerr.   objectr   r   r   r   r   <module>   s   
