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

Clarify expression alias logic #110

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions src/croniter/croniter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import absolute_import, print_function, division

from collections import namedtuple
import traceback as _traceback
import copy
import math
Expand Down Expand Up @@ -76,6 +77,19 @@ def is_32bit():

EPOCH = datetime.datetime.fromtimestamp(0)

ExpressionAlias = namedtuple("ExpressionAlias", "regular, hashed")

# Non-standard expression aliases
EXPR_ALIASES = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also pulls this up into the module scope so we're not redeclaring this mapping every time

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moving the aliases top module level would be ok, but not in favor of a namedtuple because users shall be allowed to update easily the aliases.

Another problem is that this change is not at all retrocompatible. To allow retrocompat, maybe using a class attribute that dictcopy the module level one would be a solution.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, the above comment was not validated, so you didnt saw it... sorry :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

users shall be allowed to update easily the aliases.

What makes it difficult to update the aliases because we're using a named tuple? Could you share an example of what you're thinking that is more difficult?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dict are mutable, not namedtuple.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just replace it like this though right

# Replace alias with whatever you want
EXPR_ALIASES['@midnight'] = ExpressionAlias("1 2 * * *", "h h * * *")

"@midnight": ExpressionAlias("0 0 * * *", "h h(0-2) * * * h"),
"@hourly": ExpressionAlias("0 * * * *", "h * * * * h"),
"@daily": ExpressionAlias("0 0 * * *", "h h * * * h"),
"@weekly": ExpressionAlias("0 0 * * 0", "h h * * h h"),
"@monthly": ExpressionAlias("0 0 1 * *", "h h h * * h"),
"@yearly": ExpressionAlias("0 0 1 1 *", "h h h h * h"),
"@annually": ExpressionAlias("0 0 1 1 *", "h h h h * h"),
}

# fmt: off
M_ALPHAS = {
"jan": 1, "feb": 2, "mar": 3, "apr": 4, # noqa: E241
Expand Down Expand Up @@ -823,22 +837,14 @@ def _expand(
# Split the expression in components, and normalize L -> l, MON -> mon,
# etc. Keep expr_format untouched so we can use it in the exception
# messages.
expr_aliases = {
"@midnight": ("0 0 * * *", "h h(0-2) * * * h"),
"@hourly": ("0 * * * *", "h * * * * h"),
"@daily": ("0 0 * * *", "h h * * * h"),
"@weekly": ("0 0 * * 0", "h h * * h h"),
"@monthly": ("0 0 1 * *", "h h h * * h"),
"@yearly": ("0 0 1 1 *", "h h h h * h"),
"@annually": ("0 0 1 1 *", "h h h h * h"),
}

efl = expr_format.lower()
hash_id_expr = hash_id is not None and 1 or 0
try:
efl = expr_aliases[efl][hash_id_expr]
except KeyError:
pass

# Expand expression aliases like `@midnight`
if efl in EXPR_ALIASES:
if hash_id is None:
efl = EXPR_ALIASES[efl].regular
else:
efl = EXPR_ALIASES[efl].hashed

expressions = efl.split()

Expand Down
Loading