Convert a "crontab"-style set of parameters into a test function that will return True when the given datetime matches the parameters set forth in the crontab. For day-of-week, 0=Sunday and 6=Saturday. Acceptable inputs: * = every distinct value */n = run every "n" tim
(minute='*', hour='*', day='*', month='*', day_of_week='*', strict=False)
| 1353 | |
| 1354 | |
| 1355 | def crontab(minute='*', hour='*', day='*', month='*', day_of_week='*', strict=False): |
| 1356 | """ |
| 1357 | Convert a "crontab"-style set of parameters into a test function that will |
| 1358 | return True when the given datetime matches the parameters set forth in |
| 1359 | the crontab. |
| 1360 | |
| 1361 | For day-of-week, 0=Sunday and 6=Saturday. |
| 1362 | |
| 1363 | Acceptable inputs: |
| 1364 | * = every distinct value |
| 1365 | */n = run every "n" times, i.e. hours='*/4' == 0, 4, 8, 12, 16, 20 |
| 1366 | m-n = run every time m..n |
| 1367 | m,n = run on m and n |
| 1368 | |
| 1369 | The strict parameter will cause crontab to raise a ValueError if an input |
| 1370 | does not match a supported crontab input format. This provides backwards |
| 1371 | compatibility. |
| 1372 | """ |
| 1373 | validation = ( |
| 1374 | ('m', month, range(1, 13)), |
| 1375 | ('d', day, range(1, 32)), |
| 1376 | ('w', day_of_week, range(8)), # 0-6, but also 7 for Sunday. |
| 1377 | ('H', hour, range(24)), |
| 1378 | ('M', minute, range(60)) |
| 1379 | ) |
| 1380 | cron_settings = [] |
| 1381 | |
| 1382 | for (date_str, value, acceptable) in validation: |
| 1383 | settings = set([]) |
| 1384 | |
| 1385 | if isinstance(value, int): |
| 1386 | value = str(value) |
| 1387 | |
| 1388 | for piece in value.split(','): |
| 1389 | if piece == '*': |
| 1390 | settings.update(acceptable) |
| 1391 | continue |
| 1392 | |
| 1393 | if piece.isdigit(): |
| 1394 | piece = int(piece) |
| 1395 | if piece not in acceptable: |
| 1396 | raise ValueError('%d is not a valid input' % piece) |
| 1397 | elif date_str == 'w': |
| 1398 | piece %= 7 |
| 1399 | settings.add(piece) |
| 1400 | continue |
| 1401 | |
| 1402 | dash_match = dash_re.match(piece) |
| 1403 | if dash_match: |
| 1404 | lhs, rhs = map(int, dash_match.groups()) |
| 1405 | if lhs not in acceptable or rhs not in acceptable: |
| 1406 | raise ValueError('%s is not a valid input' % piece) |
| 1407 | elif date_str == 'w': |
| 1408 | lhs %= 7 |
| 1409 | rhs %= 7 |
| 1410 | settings.update(range(lhs, rhs + 1)) |
| 1411 | continue |
| 1412 |