Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export crontab line from CronTrigger #945

Open
1 task done
fcrozetta opened this issue Jul 26, 2024 · 7 comments
Open
1 task done

Export crontab line from CronTrigger #945

fcrozetta opened this issue Jul 26, 2024 · 7 comments

Comments

@fcrozetta
Copy link

Things to check first

  • I have searched the existing issues and didn't find my feature already requested there

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
Copy link
Owner

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

@fcrozetta
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

fcrozetta commented Jul 30, 2024

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
Copy link
Owner

agronholm commented Jul 31, 2024

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
Copy link
Author

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
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants