Search code examples
pythongoogle-app-engineantlrhuman-readable

human readable schedule syntax like functionality in my app?


cron in google app engine have human readable syntax for cron job scheduling.

https://developers.google.com/appengine/docs/python/config/cron#The_Schedule_Format

How do I implement similar functionality in my application, where use enter schedule in human readable format and program parse and store the actual value ?

edit: I think they are using antlr3 library, But I could not able to figure out how they are using it.


Solution

  • Ok, in this instance there are two spots you need to look into.

    First antlr is what gives you your parser to parse the english/human readable formats into your program as a stream of tokens. From this string you will then determine the meaning such as:

    run backup every 2 days
    

    which would turn into a stream of tokens such as:

    <command_type> := run
    <command_to_run> := backup
    <time_frame_times> := every
    <digit> := 2
    <time_frame_modifier> := days
    

    From here you will then determine the meaning of the sentence using some kind of state machine that could be written (simplified form) as:

    if token == command_type:
        switch( token.val )
            case "run":
                state = run
                program = tokens.pop().val
    if token == time_frame_times:
        switch( token.val )
            case "every":
                time_frame_state = repeat_indefinitely
    if token == time_frame_modifier:
        switch( token.val )
            case "days":
                time_frame_modifier = every_N_days
    if token == digit:
        switch( time_Frame_modifier )
            case every_N_days:
                time_frame_modifier_value = token.val
    

    Something like that would suffice to parse the string into your required values.

    The second part would be to connect this into cron itself or write your own scheduler within your program itself which I think is more along the lines of your use case.

    Antlr Python: http://www.antlr.org/wiki/display/ANTLR3/Python+runtime