2020# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121# SOFTWARE.
2222
23+ # /// script
24+ # requires-python = ">=3.11"
25+ # dependencies = [
26+ # "cron-descriptor",
27+ # ]
28+ # ///
29+
2330import re
2431from pathlib import Path
2532
3340
3441
3542class CrontabReader :
36- """Simple example reading /etc/contab
37- """
43+ """Simple example reading /etc/contab"""
3844
3945 rex = re .compile (r"^(\S{1,3}\s+\S{1,3}\s+\S{1,3}\s+\S{1,3}\s+\S{1,3}).+$" )
4046
@@ -45,15 +51,13 @@ def __init__(self, cronfile: Path) -> None:
4551 cronfile: Path to cronfile
4652 Returns:
4753 None
48-
4954 """
5055 options = Options ()
5156 options .day_of_week_start_index_zero = False
5257 options .use_24hour_time_format = True
5358 with cronfile .open ("r" , encoding = "utf-8" ) as f :
5459 for line in f .readlines ():
55- parsed_line = self .parse_cron_line (line )
56- if parsed_line :
60+ if parsed_line := self .parse_cron_line (line ):
5761 print (f"{ parsed_line } -> { ExpressionDescriptor (parsed_line , options )} " )
5862
5963 def parse_cron_line (self , line : str ) -> str | None :
@@ -62,17 +66,15 @@ def parse_cron_line(self, line: str) -> str | None:
6266 Args:
6367 line: crontab line
6468 Returns:
65- Time part of cron line
66-
69+ Time part of cron line or None
6770 """
68- stripped = line .strip ()
69-
70- if stripped and stripped .startswith ("#" ) is False :
71+ if (stripped := line .strip ()) and not stripped .startswith ("#" ):
7172 rexres = self .rex .search (stripped )
7273 if rexres :
7374 return " " .join (rexres .group (1 ).split ())
7475
7576 return None
7677
7778
78- CrontabReader (Path ("/etc/crontab" ))
79+ if __name__ == "__main__" :
80+ CrontabReader (Path ("/etc/crontab" ))
0 commit comments