paul@938 | 1 | #!/usr/bin/env python |
paul@938 | 2 | |
paul@938 | 3 | """ |
paul@938 | 4 | Common scheduling functionality. |
paul@938 | 5 | |
paul@1025 | 6 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@938 | 7 | |
paul@938 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@938 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@938 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@938 | 11 | version. |
paul@938 | 12 | |
paul@938 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@938 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@938 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@938 | 16 | details. |
paul@938 | 17 | |
paul@938 | 18 | You should have received a copy of the GNU General Public License along with |
paul@938 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@938 | 20 | """ |
paul@938 | 21 | |
paul@1031 | 22 | from imiptools.text import parse_line |
paul@1028 | 23 | from imiptools.handlers.scheduling.manifest import scheduling_functions |
paul@1031 | 24 | import re |
paul@938 | 25 | |
paul@1025 | 26 | def apply_scheduling_functions(functions, handler): |
paul@1025 | 27 | |
paul@1025 | 28 | """ |
paul@1025 | 29 | Apply the given scheduling 'functions' in the current object of the given |
paul@1025 | 30 | 'handler'. |
paul@1025 | 31 | """ |
paul@1025 | 32 | |
paul@1031 | 33 | # Obtain the actual scheduling functions with arguments. |
paul@1031 | 34 | |
paul@1031 | 35 | functions = get_scheduling_function_calls(functions) |
paul@1031 | 36 | |
paul@1025 | 37 | response = "ACCEPTED" |
paul@1025 | 38 | |
paul@1031 | 39 | for fn, args in functions: |
paul@1025 | 40 | |
paul@1025 | 41 | # NOTE: Should signal an error for incorrectly configured resources. |
paul@1025 | 42 | |
paul@1025 | 43 | if not fn: |
paul@1025 | 44 | return "DECLINED" |
paul@1025 | 45 | |
paul@1025 | 46 | # Keep evaluating scheduling functions, stopping only if one |
paul@1025 | 47 | # declines or gives a null response. |
paul@1025 | 48 | |
paul@1025 | 49 | else: |
paul@1031 | 50 | result = fn(handler, args) |
paul@1025 | 51 | |
paul@1025 | 52 | # Return a negative result immediately. |
paul@1025 | 53 | |
paul@1025 | 54 | if not result or result == "DECLINED": |
paul@1025 | 55 | return result |
paul@1025 | 56 | |
paul@1025 | 57 | # Modify the eventual response from acceptance if a countering |
paul@1025 | 58 | # result is obtained. |
paul@1025 | 59 | |
paul@1025 | 60 | elif response == "ACCEPTED": |
paul@1025 | 61 | response = result |
paul@1025 | 62 | |
paul@1025 | 63 | return response |
paul@1025 | 64 | |
paul@1031 | 65 | def get_scheduling_function_calls(lines): |
paul@1031 | 66 | |
paul@1031 | 67 | """ |
paul@1031 | 68 | Parse the given 'lines', returning a list of (function, arguments) tuples, |
paul@1031 | 69 | with each function being a genuine function object and with the arguments |
paul@1031 | 70 | being a list of strings. |
paul@1031 | 71 | |
paul@1031 | 72 | Each of the 'lines' should employ the function name and argument strings |
paul@1031 | 73 | separated by whitespace, with any whitespace inside arguments quoted using |
paul@1031 | 74 | single or double quotes. |
paul@1031 | 75 | """ |
paul@1031 | 76 | |
paul@1031 | 77 | functions = [] |
paul@1031 | 78 | |
paul@1031 | 79 | for line in lines: |
paul@1031 | 80 | parts = parse_line(line) |
paul@1031 | 81 | functions.append((scheduling_functions.get(parts[0]), parts[1:])) |
paul@1031 | 82 | |
paul@1031 | 83 | return functions |
paul@1031 | 84 | |
paul@938 | 85 | # vim: tabstop=4 expandtab shiftwidth=4 |