agronholm / apscheduler

Task scheduling library for Python
MIT License
6.11k stars 698 forks source link

Export crontab line from CronTrigger #945

Open fcrozetta opened 1 month ago

fcrozetta commented 1 month ago

Things to check first

Feature description

I've been looking for a way to export crontab line from the trigger and it seems this feature does not exist. In the CronTrigger class, there is a from_crontab method that receives a crontab string. It would be good to have a to_crontab() method that would return a crontab line.

Here are some examples in how I would assume this would work:


trigger = CronTrigger.from_crontab("20 04 25 12 *")
trigger.to_crontab() // returns: "20 04 25 12 *"

In case this feature gets approve, I would like to contribute to the project and work on it. Thanks :)

Use case

Example of usage


trigger = CronTrigger.from_crontab("20 04 25 12 *")
trigger.to_crontab() // returns: "20 04 25 12 *"
agronholm commented 1 month ago

And what will it do with the year and second fields?

fcrozetta commented 1 month ago

My original idea was to have it crontab compatible, but I can add an optional parameter strict (name to be defined) that would return a line that is unix compatible, or return all the fields.

I did not saw any standard that APScheduler uses in reading the year and secondfields in the from_crontab() method, but if there is one then the new method should follow the same rules IMHO

agronholm commented 1 month ago

The point I was trying to make is that since APScheduler's cron trigger has more options than the original cron spec, the reverse conversion doesn't work, so I'm not going to provide such a method.

I took a look at the CronTrigger code and noticed that it's not really possible to implement such a thing using its public interface, so I'm going to refactor it a bit to make it possible. After that, you could fairly easily implement this feature yourself if you're only using crontab-compatible triggers in your own app.

fcrozetta commented 1 month ago

I was poking around and ended up with a small piece of code that could retrieve all the information into a dictionary:

trigger = CronTrigger.from_crontab("20 04 25 12 *")
cron_dict = { k:str(v) for (k,v) in zip(trigger.FIELD_NAMES, trigger.fields)}

It is not the most convenient, or straightforward way of getting the information, but it seems to work as expected. Are you planning to change the code to have something like this? If I could export as a dict, it could be possible to format the string as well

agronholm commented 1 month ago

Did you mean _fields and not fields? Because I can't find a fields attribute in CronTrigger. Other than that, you could instead do:

trigger = CronTrigger.from_crontab("20 04 25 12 *")
cron_dict = {f.name:  str(f) in trigger._fields}
fcrozetta commented 1 month ago

yes, _fields indeed. mistype on my side I believe the f variable in your snippet should be the trigger, right? I didn't knew I could use .name to retrieve the field name! Thanks for pointing it out for me.

fcrozetta commented 1 month ago

If the feature to export the crontab line won't be implemented, should I close this issue already, or is this going to be related to any other changes you will do in the code?