def get_comment_value_by_assignment_id(marked_grade_cell: Cell, marked_comment_cells: Cell, row_number, ws: worksheet): """ Helper method to find the corresponding comment value for a given grade_cell. Column index marked by assignment ID in upload_marker row. Row indexing by given row_number. The list of cells (marked_comment_cells) is cleaned before and do not contain the CommentMarkerPrefix anymore, but assignment IDs. :param marked_grade_cell: Grade cell marked by assignment ID in the upload_marker row. :param marked_comment_cells: List of all comment cells we want to find the assignment ID of marked_grade_cell in. :param row_number: Number of current row. :param ws: Worksheet to read from (data_only). :return: Empty string if cell value is None or if cell is not found. """ if Config.get_behavior_comments_switch(): for coment_cell in marked_comment_cells: # find matching assignment id if str(coment_cell.value) == str(marked_grade_cell.value): # extract coordinates (column index) column_index_of_comment_cell = column_index_from_string( coordinate_from_string(coment_cell.coordinate)[0]) # read value of cell comment_value = ws.cell( row=row_number, column=column_index_of_comment_cell).value if not comment_value: # normalize - we want string only as return type comment_value = '' return comment_value if Config.get_behavior_log(): print('Comment for grade cell ' + str(marked_grade_cell.coordinate) + ' not found!') return ''
def do_post_on_webservice(ws_function, payload): """ Wrapper function to shorten the call on moodle webservice api. Also we want to do logging and exception handling here. The security token for this client is put into request body to prevent sniffing on client- / server side. :param ws_function: Webservice function name - defined in Moodle Webservices API :param payload: Request Body content - defined in Moodle Webservices API :return: Response of moodle server (not RESTful) as JSON. """ # TODO: logging? # TODO: exception / error handling # concat moodle url from config.ini moodle_ws_address = Config.get_moodle_domain( ) + Config.get_moodle_webservice_address() # add security token into POST body to hide it payload.update({'wstoken': Config.get_moodle_token()}) # necessary url parameter as defined in Moodle Webservices API params = { # the webservice function 'wsfunction': str(ws_function), # REST service - JSON as answer 'moodlewsrestformat': 'json' } r = requests.post(moodle_ws_address, params=params, data=payload) if Config.get_behavior_log(): print('********************* ' + ws_function + ' *********************') print('### PAYLOAD ###') util.print_json_pretty(payload) # check for errors in response and ask user to continue check_for_errors_in_response(r, ws_function, payload) result_as_json = json.loads(r.text) return result_as_json
def check_for_errors_in_response(response, ws_function, payload): error_list = ('error', 'exception') error_occurred = False if response: if response.text: response_text_as_json = json.loads(response.text) if response_text_as_json: if any(entity in response_text_as_json for entity in error_list): error_occurred = True elif isinstance(response_text_as_json, list): for list_item in response_text_as_json: if list_item.get('warnings'): error_occurred = True break elif bool(response_text_as_json.get('warnings')): error_occurred = True if error_occurred: print('********************* ERROR *********************') print('### WS_FUNCTION ###') print(str(ws_function)) print('### PAYLOAD ###') util.print_json_pretty(payload) print('### STATUS CODE ###') print(response) print('### RESPONSE HEADERS ###') print(response.headers) print('### RESPONSE BODY ###') if json.loads(response.text): util.print_json_pretty(json.loads(response.text)) util.ask_user_to_continue( "Moodle meldet Fehler. Fortfahren? (ja/nein)", ('j', 'ja')) elif Config.get_behavior_log(): print('### STATUS CODE ###') print(response) print('### RESPONSE BODY ###') if json.loads(response.text): util.print_json_pretty(json.loads(response.text))
def send_commands_to_ovid(commands): hostname = Config.get_ovid_ssh_host_name() username = Config.get_ovid_ssh_user_name() password = Config.get_ovid_ssh_password() client = pm.SSHClient() client.set_missing_host_key_policy( pm.AutoAddPolicy()) client.connect(hostname, username=username, password=password) if isinstance(commands, list): prepared_commands = '; '.join(commands) else: prepared_commands = commands if Config.get_behavior_log(): print('*********** COMMANDS TO OVID ***********') if isinstance(commands, list): for command in commands: print(command) else: print(commands) print('****************************') # util.ask_user_to_continue('Diese Befehle werden an OVID gesendet.\nMöchten Sie fortfahren? (ja/j = ja, ANY = nein)') stdin, stdout, stderr = client.exec_command(prepared_commands) exit_status = str(stdout.channel.recv_exit_status()) ssh_stdout = stdout.readlines() ssh_stderr = stderr.readlines() client.close() if str(exit_status) != '0': print('****** ERROR IN SSH TO OVID ******') print('SSH Exit Status:') print(str(exit_status)) print('SSH Output:') print(ssh_stdout) print('SSH Error Output:') print(ssh_stderr) print('') return {'exit_status': exit_status, 'errors': ssh_stderr, 'commands': prepared_commands}