msg_header_lines = msg_header.splitlines() request_type, request_path = msg_header_lines[0].split(' ',1) print "Request Type: %s" % request_type print "Request Path: %s" % request_path if request_type == "PUT": fullpath = './static/data/' + request_path response_code = FileSync.put_file(fullpath, 'text/plain', msg_body) client_sock.send('HTTP/1.1 ' + response_code + '\n') files_received += 1 elif request_type == "GET": fullpath = './static/data/' + request_path if os.path.isdir(fullpath): file_list = FileSync.get_file_list(fullpath) response_body = '' for file_name in file_list: response_body += file_name + '\n' client_sock.send('HTTP/1.1 ' + '200 OK' + '\n\n') client_sock.send(response_body + '\n') else: response_body = FileSync.get_file(fullpath) if response_body != '': client_sock.send('HTTP/1.1 ' + '200 OK' + '\n\n') client_sock.send(response_body + '\n\n') else: client_sock.send('HTTP/1.1 ' + '404 Not Found' + '\n\n') print "Request Complete\n"
def processClientConnection( self, client_sock, client_info ): print "Accepted connection from ", client_info files_received = 0 try: while True: msg_header, msg_body, content_type = self.read_request( client_sock ) if len(msg_header) == 0: break print "Message Header: %s" % msg_header print "Message Body Length: %d" % len(msg_body) msg_header_lines = msg_header.splitlines() request_type, request_path = msg_header_lines[0].split(' ',1) print "Request Type: %s" % request_type print "Request Path: %s" % request_path request_complete = False # retrieve any params attached to the requested entity params_offset = request_path.find('?') if params_offset != -1: request_params = request_path[params_offset:] request_params = request_params.lstrip('?') request_path = request_path[0:params_offset] request_path = request_path.lstrip('/') # if the requested path starts with 'static', then let's assume that # the request knows the full path that it's looking for, otherwise, # we will prepend the path with the path to the data directory if request_path.startswith('static'): fullpath = './' + request_path else: fullpath = './static/data/' + request_path if request_type == "PUT": # make sure that the destination directory exists if not os.path.exists(os.path.dirname(fullpath)): os.makedirs(os.path.dirname(fullpath)) response_code = FileSync.put_file(fullpath, content_type, msg_body) client_sock.send('HTTP/1.1 ' + response_code + '\r\n') files_received += 1 elif request_type == "POST": response_code = "400 Bad Request" path_elems = request_path.split('/') if len(path_elems) >= 2: comp_season_list = WebCommonUtils.split_comp_str(path_elems[0]) if comp_season_list != None: result = False error = False # for the sync of event and team data, the URI path is of the following # format /Sync/<comp>/EventData/[TeamData/]. if just EventData is provided, # then the event data is regenerated, if TeamData is provided, then both # the event data and team data is regenerated if len(path_elems) >= 2 and path_elems[1] == 'EventData': result = WebEventData.update_event_data_files( self.global_config, comp_season_list[1], comp_season_list[0], path_elems[1] ) if result == True: result = WebTeamData.update_team_event_files( self.global_config, comp_season_list[1], comp_season_list[0], path_elems[1] ) if result == True: result = WebAttributeDefinitions.update_event_data_files( self.global_config, path_elems[1] ) if result == False: error = True if len(path_elems) >= 3 and path_elems[2] == 'TeamData' and error is False: try: team = path_elems[3] if team == '': team = None except: team = None result = WebTeamData.update_team_data_files( self.global_config, comp_season_list[1], comp_season_list[0], path_elems[2], team ) if result == True: response_code = "200 OK" client_sock.send('HTTP/1.1 ' + response_code + '\r\n') elif request_type == "GET": # Parse any params attached to this GET request params = request_params.split(';') for param in params: # split the parameter into the tag and value parsed_param = param.split('=') tag = parsed_param[0] value = parsed_param[1] # process the parameter # check to see if the requested path exists. We may need to handle that # condition separately, treating non-existent directories as empty (as # opposed to sending a 404 not found. # TODO: update the client side to handle the 404 not found as an empty directory # and then update this block to send the 404 in all cases. if not os.path.exists(fullpath): if request_path[-1] == '/': # if the requested path refers to a directory, let's return an empty # response indicating that there are no files in that directory client_sock.send('HTTP/1.1 ' + '200 OK' + '\r\n') client_sock.send('Content-Length: 0\r\n') client_sock.send('\r\n\r\n') else: client_sock.send('HTTP/1.1 ' + '404 Not Found' + '\r\n\r\n') request_complete = True if not request_complete: if os.path.isdir(fullpath): file_list = FileSync.get_file_list(fullpath) response_body = '' for file_name in file_list: response_body += file_name + '\n' client_sock.send('HTTP/1.1 ' + '200 OK' + '\r\n') client_sock.send('Content-Length: %d\r\n' % len(response_body)) client_sock.send('\r\n') client_sock.send(response_body + '\r\n') else: response_body = FileSync.get_file(fullpath) if response_body != '': client_sock.send('HTTP/1.1 ' + '200 OK' + '\r\n') client_sock.send('Content-Length: %d\r\n' % len(response_body)) client_sock.send('\r\n') client_sock.send(response_body + '\r\n') else: client_sock.send('HTTP/1.1 ' + '404 Not Found' + '\r\n\r\n') print "Request Complete\n" except IOError: pass print "disconnected" client_sock.close()
def process_json_files(global_config, competition, output_file, input_dir, reprocess_files=False): # Initialize the database session connection db_name = global_config['db_name'] + global_config['this_season'] session = DbSession.open_db_session(db_name) # get all the verified files from the input directory. These files are # candidates to be processed verified_files = FileSync.get_file_list(input_dir, ext='.verified', recurse=True ) verified_files.sort(key=match_sort) # For the normal case, get all the processed files, too. We'll use the processed list to # determine which files are actually newly verified and need to be processed. If the # reprocess flag is true, then we'll process all verified files. if reprocess_files is not True: processed_files = FileSync.get_file_list(input_dir, ext='.processed', recurse=True ) for processed_file in processed_files: verified_files.remove( processed_file.replace('processed','verified') ) xlsx_workbook = None excel_intf_ctrl = global_config.get('excel_sheets_intf', 'Disabled') if excel_intf_ctrl == 'Enabled': # read in the output file, which is expected to be an XLSX file try: xlsx_workbook = openpyxl.load_workbook(output_file) except: print 'Error Reading Spreadsheet %s For Input' % output_file google_intf_ctrl = global_config.get('google_sheets_intf', 'Disabled') ''' # took out for now until we have local dictionary storage events = global_config.get('events') if events is None: events = {} global_config['events'] = events event_data = events.get( competition ) if event_data is None: events[competition] = { 'ScoutingData': { 'TeamData': {} } } event_data = events[competition] event_scouting_data = event_data['ScoutingData']['TeamData'] ''' for verified_file in verified_files: filename = verified_file.split('/')[-1] # read the file into a dictionary with open(input_dir+verified_file) as fd: scouting_data = json.load(fd) if filename.startswith('Match'): team = scouting_data['Setup'].get('Team') category = 'Match' elif filename.startswith('Pit'): team = scouting_data['Pit'].get('Team') category = 'Pit' else: category = 'Unknown' if team is not None and len(team) > 0: # ######################################################### # # store the scouting data to the local database DataModel.addTeamToEvent(session, int(team), competition) attr_definitions = AttributeDefinitions.AttrDefinitions(global_config) for section_name, section_data in scouting_data.iteritems(): if isinstance(section_data,dict): for attr_name, attr_value in section_data.iteritems(): # use the attribute definitions to control whether information gets # stored to the database rather than the hard coded stuff here. # also need to consider the section/category name as the attributes # and definitions are processed # don't store the team number in the database if attr_name == 'Team': continue # augment the attribute name with the section name in order to make the attribute # unique attr_name = '%s:%s' % (section_name, attr_name) attribute_def = {} attribute_def['Name'] = attr_name if attr_value.isdigit(): attribute_def['Type'] = 'Integer' attribute_def['Weight'] = 1.0 else: attribute_def['Type'] = 'String' attribute_def['Weight'] = 0.0 attribute_def['Statistic_Type'] = 'Average' attr_definitions.add_definition(attribute_def) try: DataModel.createOrUpdateAttribute(session, int(team), competition, category, attr_name, attr_value, attribute_def) except Exception, exception: traceback.print_exc(file=sys.stdout) exc_type, exc_value, exc_traceback = sys.exc_info() exception_info = traceback.format_exception(exc_type, exc_value,exc_traceback) for line in exception_info: line = line.replace('\n','') global_config['logger'].debug(line) else: print 'Unexpected entry in scouting data file, name: %s, value: %s' % (section_name,section_data) score = DataModel.calculateTeamScore(session, int(team), competition, attr_definitions) DataModel.setTeamScore(session, int(team), competition, score) session.commit() # ######################################################### # # Google spreadsheet update if google_intf_ctrl == 'Enabled': row_data = [] for section_name, section_data in scouting_data.iteritems(): if isinstance(section_data,dict): for attr_name, attr_value in section_data.iteritems(): # augment the attribute name with the section name in order to make the attribute # unique attr_name = '%s_%s' % (section_name, attr_name) row_data.append((attr_name,attr_value)) else: print 'Unexpected entry in scouting data file, name: %s, value: %s' % (section_name,section_data) sheet_name = '%s_%s_Data' % (competition,category) GoogleSheetsIntf.add_scouting_data_row( sheet_name, row_data ) # ######################################################### # # ######################################################### # ''' # ######################################################### # # store the scouting data to a local dictionary team_data = event_scouting_data.get(team) if team_data is None: team_data = { 'Summary': {}, 'MatchData': [] } event_scouting_data[team] = team_data team_match_data = team_data['MatchData'] # if this match has already been scored, then update the data by removing the existing # match data and then add the updated data update_match = False for match_data in team_match_data: if scouting_data['Setup']['Match'] == match_data['Setup']['Match']: update_match = True break if update_match is True: team_match_data.remove(match_data) team_match_data.append(scouting_data) # ######################################################### # ''' # ######################################################### # # store the match scouting data information to the spreadsheet if excel_intf_ctrl == 'Enabled' and xlsx_workbook is not None: if category == 'Match': team_name = 'Team %s' % team try: team_sheet = xlsx_workbook.get_sheet_by_name(team_name) except: team_sheet = create_team_sheet( xlsx_workbook, team_name ) curr_matches = team_sheet['B2'].value if curr_matches is None: curr_matches = 0 # get max row and column count and iterate over the sheet max_row= team_sheet.max_row for i in range(1,max_row+1): # scan for a row that has Match in the first column to identify rows where data will be stored cell_value = team_sheet.cell(row=i,column=1).value if team_sheet.cell(row=i,column=1).value == 'Match': attr_row = i data_row = i+1 data_cell = team_sheet.cell(row=i+1,column=1).value if data_cell is None: team_sheet = update_data_row( team_sheet, attr_row, data_row, scouting_data ) team_sheet['B2'].value = curr_matches+1 break elif data_cell == int(scouting_data['Setup']['Match']): # Update an existing row team_sheet = update_data_row( team_sheet, attr_row, data_row, scouting_data ) break # Jump over the next two rows i += 2 # ######################################################### # shutil.copyfile(input_dir+verified_file, input_dir+verified_file.replace('verified','processed')) if xlsx_workbook is not None: xlsx_workbook.save(output_file)
def run(self): server_sock=BluetoothSocket( RFCOMM ) server_sock.bind(("",PORT_ANY)) server_sock.listen(1) port = server_sock.getsockname()[1] uuid = "00001073-0000-1000-8000-00805F9B34F7" advertise_service( server_sock, "TTTService", service_id = uuid, service_classes = [ uuid, SERIAL_PORT_CLASS ], profiles = [ SERIAL_PORT_PROFILE ] ) while not self.shutdown: print "Waiting for connection on RFCOMM channel %d" % port client_sock, client_info = server_sock.accept() print "Accepted connection from ", client_info files_received = 0 try: while True: msg_header, msg_body, content_type = self.read_request( client_sock ) if len(msg_header) == 0: break print "Message Header: %s" % msg_header print "Message Body Length: %d" % len(msg_body) msg_header_lines = msg_header.splitlines() request_type, request_path = msg_header_lines[0].split(' ',1) print "Request Type: %s" % request_type print "Request Path: %s" % request_path if request_type == "PUT": fullpath = './static/data/' + request_path response_code = FileSync.put_file(fullpath, content_type, msg_body) client_sock.send('HTTP/1.1 ' + response_code + '\r\n') files_received += 1 elif request_type == "GET": fullpath = './static/data/' + request_path if os.path.isdir(fullpath): file_list = FileSync.get_file_list(fullpath) response_body = '' for file_name in file_list: response_body += file_name + '\n' client_sock.send('HTTP/1.1 ' + '200 OK' + '\r\n') client_sock.send('Content-Length: %d\r\n' % len(response_body)) client_sock.send('\r\n') client_sock.send(response_body + '\r\n') else: response_body = FileSync.get_file(fullpath) if response_body != '': client_sock.send('HTTP/1.1 ' + '200 OK' + '\r\n') client_sock.send('Content-Length: %d\r\n' % len(response_body)) client_sock.send('\r\n') client_sock.send(response_body + '\r\n') else: client_sock.send('HTTP/1.1 ' + '404 Not Found' + '\r\n\r\n') print "Request Complete\n" except IOError: pass print "disconnected" client_sock.close() server_sock.close() print "Bluetooth Sync Server Terminated"