def validate_args(self): if self.parsed_args.command not in ['file_manager', 'network_manager']: self.parser.error( "Command invalid. Must use file manager or network manager") elif self.parsed_args.command == 'file_manager': if self.parsed_args.action == 'send' and not self.parsed_args.data: self.parser.error("Must specify data to send.") if self.parsed_args.action != "send" and self.parsed_args.new_line: self.parser.error("Cannot enter new line using this action.") if self.parsed_args.action == "replace" and FileHelper.get_ext( self.parsed_args.file) == 'csv': if not self.parsed_args.replace_data or not self.parsed_args.data or not self.parsed_args.row or not self.parsed_args.column: self.parser.error( "Must specify row, column, data to replace with, and data being replaced in csv file." ) elif self.parsed_args.action == "replace" and FileHelper.get_ext( self.parsed_args.file) == 'txt': if not self.parsed_args.replace_data or not self.parsed_args.data: self.parser.error( "Must specify data to replace with and data being replaced in txt file." ) elif self.parsed_args.command == 'network_manager': if not self.parsed_args.host or not self.parsed_args.port: self.parser.error("Must specify host and port.") if self.parsed_args.action == 'send' and not self.parsed_args.data: self.parser.error("Must specify data to send.")
def format_message(self, action:str, log_file:str) -> str: """ This function formats the log message. Input: - str - str Output: - str """ log = getattr(self, "_{}_log".format(action))() logfile_ext = FileHelper.get_ext(log_file) if logfile_ext == 'txt': base = "{} - {} {}:{} [{}] - {}: {} (Source - {}:{}) (Destination - {}:{}), Size: {}, Protocol: {}" elif logfile_ext == 'csv': base = "{},{},{},{},{},{},{},{},{},{},{},{},{}" message = base.format( self.user_name, self.timestamp, self.process_name, self.process_id, self.command, self.status.upper(), log, self.data['sock']['source']['host'], self.data['sock']['source']['port'], self.data['sock']['destination']['host'], self.data['sock']['destination']['port'], self.data['size'], 'TCP' # TODO: Get protocol from socket. ) return message
def format_message(self, action: str, log_file: str) -> str: """ This function formats the log message. Input: - str - str Output: - str """ log = getattr(self, "_{}_log".format(action))() logfile_ext = FileHelper.get_ext(log_file) if logfile_ext == 'txt': base = "{} - {} {}:{} [{}] - {}: {} ({})" elif logfile_ext == 'csv': base = "{},{},{},{},{},{},{},{}" message = base.format( self.user_name, self.timestamp, self.process_name, self.process_id, self.command, self.status.upper(), log, self.abs_file_path, ) return message
def replace(self) -> dict: """ This function determines how to replace data. Output: - dict """ data = {'message': self.args['data'], 'replace_message': self.args['replace_data'], 'row': '', 'column': '', 'filename': self.filename} file_ext = FileHelper.get_ext(self.filename) if file_ext == 'csv': status = FileHelper.replace_in_file( self.filename, self.args['data'], self.args['replace_data'], file_ext, self.args['row'], self.args['column'], ) data['row'] = self.args['row'] data['column'] = self.args['column'] elif file_ext == 'txt': status = FileHelper.replace_in_file( self.filename, self.args['data'], self.args['replace_data'], file_ext ) return ServiceHelper.construct_response(status, data)
def __send_to_file(self): """ This function sends the data to the output file. """ file_ext = FileHelper.get_ext(self.output_file) response = FileHelper.send_to_file(self.output_file, self.message, file_ext, True) if not response: # TODO: quickfix.. write better error message/ use better structure. sys.stdout.write("Unhandled log filename specified.")
def _replace_log(self) -> str: """ This function formats a message when replacing data in a file. Output: - str """ file_ext = FileHelper.get_ext(self.data['filename']) if file_ext == 'csv': logger_msg = "Replacing {} to {} at ({}, {}) in {}".format( self.data['replace_message'], self.data['message'], self.data['row'], self.data['column'], self.data['filename']) elif file_ext == 'txt': logger_msg = "Replacing {} to {} in {}".format( self.data['replace_data'], self.data['data'], self.data['filename']) return logger_msg
def send(self) -> dict: """ This function sends data to a file. Output: - dict """ # NOTE: potentially move 'try' block to this method data = {'message': self.args['data'], 'filename': self.filename} file_ext = FileHelper.get_ext(self.filename) status = FileHelper.send_to_file( self.filename, self.args['data'], file_ext, self.args['new_line'] ) return ServiceHelper.construct_response(status, data)
def format_message(self, log_file: str) -> str: """ This function formats the log message. Input: - str Output: - str """ logfile_ext = FileHelper.get_ext(log_file) if logfile_ext == 'txt': base = "{} - {} {}:{} [{}] - {}: {}" elif logfile_ext == 'csv': base = "{},{},{},{},{},{},{}" message = base.format( self.user_name, self.timestamp, self.process_name, self.process_id, self.command, self.status.upper(), self.set_message(), ) return message