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