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 confirmation_functions, \ 24 retraction_functions, \ 25 scheduling_functions 26 27 # Function application/invocation. 28 29 def apply_scheduling_functions(functions, handler): 30 31 """ 32 Apply the given scheduling 'functions' in the current object of the given 33 'handler'. 34 """ 35 36 # Obtain the actual scheduling functions with arguments. 37 38 functions = get_function_calls(functions, scheduling_functions) 39 40 response = "ACCEPTED" 41 42 for fn, args in functions: 43 44 # NOTE: Should signal an error for incorrectly configured resources. 45 46 if not fn: 47 return "DECLINED" 48 49 # Keep evaluating scheduling functions, stopping only if one 50 # declines or gives a null response. 51 52 else: 53 result = fn(handler, args) 54 55 # Return a negative result immediately. 56 57 if not result or result == "DECLINED": 58 return result 59 60 # Modify the eventual response from acceptance if a countering 61 # result is obtained. 62 63 elif response == "ACCEPTED": 64 response = result 65 66 return response 67 68 def confirm_scheduling(functions, handler): 69 70 """ 71 Confirm scheduling using the given listener 'functions' for the current 72 object of the given 'handler'. 73 """ 74 75 # Obtain the actual listener functions with arguments. 76 77 functions = get_function_calls(functions, confirmation_functions) 78 apply_functions(functions, handler) 79 80 def retract_scheduling(functions, handler): 81 82 """ 83 Retract scheduling using the given listener 'functions' for the current 84 object of the given 'handler'. 85 """ 86 87 # Obtain the actual listener functions with arguments. 88 89 functions = get_function_calls(functions, retraction_functions) 90 apply_functions(functions, handler) 91 92 def apply_functions(functions, handler): 93 94 """ 95 Apply the given notification 'functions' for the current object of the given 96 'handler'. 97 """ 98 99 for fn, args in functions: 100 101 # NOTE: Should signal an error for incorrectly configured resources. 102 103 if not fn: 104 continue 105 106 fn(handler, args) 107 108 # Function lookup. 109 110 def get_function_calls(lines, registry): 111 112 """ 113 Parse the given 'lines', returning a list of (function, arguments) tuples, 114 with each function being a genuine function object and with the arguments 115 being a list of strings. 116 117 Each of the 'lines' should employ the function name and argument strings 118 separated by whitespace, with any whitespace inside arguments quoted using 119 single or double quotes. 120 121 The given 'registry' indicates the mapping from function names to actual 122 functions. 123 """ 124 125 functions = [] 126 127 for line in lines: 128 parts = parse_line(line) 129 functions.append((registry.get(parts[0]), parts[1:])) 130 131 return functions 132 133 # vim: tabstop=4 expandtab shiftwidth=4