loris_log.ftpClient
The library to push log message to FTP server.
1""" 2The library to push log message to FTP server. 3""" 4 5import io 6import datetime 7import ipaddress 8from ftplib import FTP, all_errors 9from .myException import FTPConnectionFailedException,\ 10 FTPFileCreationException,\ 11 NoneValueException,\ 12 InvalidAttributeException,\ 13 EmptyParameterException,\ 14 InvalidFTPHostNameException,\ 15 InvalidFTPPortNumberException,\ 16 InvalidFTPUserNameException,\ 17 InvalidFTPPasswordException 18 19# pylint: disable=E0302 20# pylint: disable=W0212 21class FtpClient: 22 """Class to establish connection and writing to FTP server. 23 """ 24 25 def __init__(self, hostname, port_num, username, password): 26 """ 27 Establish communication with the remote FTP server. 28 29 Args:\n 30 hostname (string): FTP server hostname. 31 port_num (integer): FTP server port number. 32 username (string): FTP server username. 33 password (string): FTP server password. 34 35 Raise InvalidFTPPortNumberException:\n 36 If the port number is not of type int and is equal or less 37 than zero. 38 39 Raise InvalidFTPUserNameException:\n 40 If the username is not of type string or it is an empty 41 string or it is a None value. 42 43 Raise InvalidFTPPasswordException:\n 44 If the password is not of type string or it is an empty 45 string or it is a None value. 46 47 Raise FTPConnectionFailedException:\n 48 If the connection to the FTP remote server was not 49 a success. 50 51 Raise InvalidFTPHostNameException:\n 52 If any of the port number, username, and password 53 are invalid. 54 """ 55 56 if isinstance(port_num, int) is False\ 57 or port_num <= 0: 58 raise InvalidFTPPortNumberException() 59 60 if isinstance(username, str) is False\ 61 or username is None or\ 62 username == "": 63 raise InvalidFTPUserNameException() 64 65 if isinstance(password, str) is False\ 66 or password is None or\ 67 password == "": 68 raise InvalidFTPPasswordException() 69 70 self.ftp_client = FTP() 71 72 try: 73 ipaddress.ip_address(hostname) 74 self.ftp_client.connect(hostname, port_num) 75 self.ftp_client.login(username, password) 76 except all_errors as exc: 77 raise FTPConnectionFailedException() from exc 78 except ValueError as valuerr: 79 raise InvalidFTPHostNameException() from valuerr 80 81 def __get_ftp_directories(self): 82 """ 83 To retrieve all the directories and files that are present 84 in the current working directory inside this directory 85 in this FTP server. 86 87 Returns:\n 88 list: A complete list of directories and files that are 89 currently available in present working directory. 90 """ 91 dir_list = [] 92 self.ftp_client.retrlines('NLST', dir_list.append) 93 return [item.split(" ")[-1] for item in dir_list] 94 95 def __get_datetime(self): 96 """ 97 Get the today date. 98 99 Returns:\n 100 string: Today's date. 101 """ 102 return datetime.datetime.now().strftime("%Y-%m-%d") 103 104 def __set_data_file_name(self, file_name): 105 """ 106 Set the complete filename of the desired csv file. 107 108 Args: 109 file_name (string): The name of the csv file that wanted 110 to be named. 111 112 Returns:\n 113 string: The complete filename of a csv file. 114 """ 115 return self.__get_datetime() + "_" + str(file_name) + ".csv" 116 117 def __set_log_file_name(self, file_name): 118 """ 119 Get the complete filename of the desired log file. 120 121 Args:\n 122 file_name (string): The name of the log file that wanted 123 to be named. 124 125 Returns:\n 126 string: The complete fiilename of a log file. 127 """ 128 return self.__get_datetime() + "_" + str(file_name) + ".log" 129 130 def create_ftp_log_data(self, directory_path, file_name): 131 """ 132 Create a csv log file inside a FTP server based on the 133 predefined path. 134 135 Args:\n 136 directory_path (string): The path to the csv log file. 137 file_name (string): Filename of the csv log file. 138 139 Raise FTPFileCreationException:\n 140 If the filename and directory path are invalid, a None value 141 or is empty, or is unable to write to a log file in the 142 FTP server. 143 144 """ 145 if file_name == "" or file_name is None or\ 146 isinstance(file_name, str) is False: 147 raise FTPFileCreationException() 148 149 if directory_path == "" or directory_path is None or\ 150 isinstance(directory_path, str) is False: 151 raise FTPFileCreationException() 152 153 try: 154 # get all the directory names 155 directories = directory_path.split('/') 156 for directory in directories: 157 # check if the directory name is present 158 if directory not in self.__get_ftp_directories(): 159 # if not present create the directory 160 # then move to the corresponding directory 161 self.ftp_client.mkd(directory) 162 self.ftp_client.cwd(directory) 163 else: 164 # if already present, 165 # just switch to the corresponding directory 166 self.ftp_client.cwd(directory) 167 168 # Then create the desired log file if the log file 169 # was not present before. Otherwise done nothing. 170 filename = self.__set_data_file_name(file_name) 171 if filename not in self.__get_ftp_directories(): 172 buf = io.BytesIO() 173 buf.write(b"uuid,start,end,result,groundtruth\n") 174 buf.seek(0) 175 self.ftp_client.storbinary(f"STOR {filename}", buf) 176 except all_errors as error: 177 raise FTPFileCreationException() from error 178 179 def set_ftp_log_data(self, directory_path, file_name, log_data): 180 """ 181 Set the csv log file with the desired log data. The new log data 182 will continue log onto the existing file, if the log file was 183 present. Otherwise, new file gonna be created for this new log data. 184 Please do take note that the file type must be of csv type. 185 186 Args:\n 187 directory_path (string): The path to the csv log file. 188 file_name (string): The name of the csv log file. 189 log_data (bytes): The log data. 190 191 Raise NoneValueException:\n 192 If the directory path, file name or the data to be logged 193 is of None value. 194 195 Raise InvalidAttributeException:\n 196 If the directory path, file name is not the type of string, 197 while the log data is not type of byte. 198 199 Raise EmptyParameterException:\n 200 If the directory path, file name or log data is empty. 201 """ 202 if directory_path is None or file_name is None or\ 203 log_data is None: 204 raise NoneValueException() 205 206 if isinstance(directory_path, str) is False or\ 207 isinstance(file_name, str) is False or\ 208 isinstance(log_data, bytes) is False: 209 raise InvalidAttributeException() 210 211 if len(directory_path) == 0 or len(file_name) == 0 or\ 212 log_data == b"": 213 raise EmptyParameterException() 214 215 # get the desired directory name 216 directories = directory_path.split('/') 217 # loop through all the directory name 218 for directory in directories: 219 # if the name present move to the next directory 220 if directory in self.__get_ftp_directories(): 221 self.ftp_client.cwd(directory) 222 223 filename = self.__set_data_file_name(file_name) 224 # check if the corrresponding file name present 225 if filename in self.__get_ftp_directories(): 226 # if present write the log message to the relevant log file 227 buf=io.BytesIO() 228 buf.write(log_data) 229 buf.seek(0) 230 self.ftp_client.storbinary(f"APPE {filename}", buf, 1) 231 232 def create_ftp_log_file(self, directory_path, file_name): 233 """ 234 Create the log file inside an FTP server. 235 236 Args:\n 237 directory_name (string): The path to the created log 238 file. 239 file_name (string): The name of the log file. 240 241 Raise FTPFileCreationException:\n 242 If the file name or directory path is empty, a none value, is not 243 of the type string, or is failed to write the log onto the FTP server. 244 """ 245 if file_name == "" or file_name is None or\ 246 isinstance(file_name, str) is False: 247 raise FTPFileCreationException() 248 249 if directory_path == "" or directory_path is None or\ 250 isinstance(directory_path, str) is False: 251 raise FTPFileCreationException() 252 253 try: 254 # get all the directories 255 directories = directory_path.split("/") 256 for directory in directories: 257 # check if the directory is present 258 # if not present create the directory 259 # then move to the newly create directory 260 if directory not in self.__get_ftp_directories(): 261 self.ftp_client.mkd(directory) 262 self.ftp_client.cwd(directory) 263 # if the directory present 264 # just move to the newly create directory 265 else: 266 self.ftp_client.cwd(directory) 267 268 filename = self.__set_log_file_name(file_name) 269 # check if the log file exist. If not create the 270 # log file. otherwise, done nothing. 271 if filename not in self.__get_ftp_directories(): 272 buf = io.BytesIO() 273 buf.seek(0) 274 self.ftp_client.storbinary(f"STOR {filename}", buf) 275 return True 276 return False 277 except all_errors as error: 278 raise FTPFileCreationException from error 279 280 def set_ftp_log_file(self, directory_path, file_name, message): 281 """ 282 Set the log file with application or system log. If the log file was 283 already existed, the new log will continue appended on to the existing log 284 file. Otherwise, new log file gonna be created. 285 286 Args:\n 287 directory_path (string): The path to the log file. 288 file_name (string): The log file name. 289 message (string): The log message. 290 291 Raise NoneValueException:\n 292 If the directory path and file name is a type None. 293 294 Raise InvalidAttributeException:\n 295 If the directory path, file name or log message is 296 not the type string. 297 298 Raise EmptyParameterException:\n 299 If the directory path, file name or log message is 300 not empty. 301 """ 302 if directory_path is None or file_name is None or\ 303 message is None: 304 raise NoneValueException() 305 306 if isinstance(directory_path, str) is False or\ 307 isinstance(file_name, str) is False or\ 308 isinstance(message, str) is False: 309 raise InvalidAttributeException() 310 311 if len(directory_path) == 0 or len(file_name) == 0 or\ 312 len(message) == 0: 313 raise EmptyParameterException() 314 315 # get the directories name 316 directories = directory_path.split('/') 317 # move to the corresponding directory if it 318 # present 319 for directory in directories: 320 if directory in self.__get_ftp_directories(): 321 self.ftp_client.cwd(directory) 322 323 # check for log file existence. If it exist only 324 # write the log onto the corresponding log file. 325 filename = self.__set_log_file_name(file_name) 326 if filename in self.__get_ftp_directories(): 327 buf = io.BytesIO() 328 buf.write(bytes("["+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+"]"+message+"\n", 'utf-8')) 329 buf.seek(0) 330 self.ftp_client.storbinary(f"APPE {filename}", buf, 1)
22class FtpClient: 23 """Class to establish connection and writing to FTP server. 24 """ 25 26 def __init__(self, hostname, port_num, username, password): 27 """ 28 Establish communication with the remote FTP server. 29 30 Args:\n 31 hostname (string): FTP server hostname. 32 port_num (integer): FTP server port number. 33 username (string): FTP server username. 34 password (string): FTP server password. 35 36 Raise InvalidFTPPortNumberException:\n 37 If the port number is not of type int and is equal or less 38 than zero. 39 40 Raise InvalidFTPUserNameException:\n 41 If the username is not of type string or it is an empty 42 string or it is a None value. 43 44 Raise InvalidFTPPasswordException:\n 45 If the password is not of type string or it is an empty 46 string or it is a None value. 47 48 Raise FTPConnectionFailedException:\n 49 If the connection to the FTP remote server was not 50 a success. 51 52 Raise InvalidFTPHostNameException:\n 53 If any of the port number, username, and password 54 are invalid. 55 """ 56 57 if isinstance(port_num, int) is False\ 58 or port_num <= 0: 59 raise InvalidFTPPortNumberException() 60 61 if isinstance(username, str) is False\ 62 or username is None or\ 63 username == "": 64 raise InvalidFTPUserNameException() 65 66 if isinstance(password, str) is False\ 67 or password is None or\ 68 password == "": 69 raise InvalidFTPPasswordException() 70 71 self.ftp_client = FTP() 72 73 try: 74 ipaddress.ip_address(hostname) 75 self.ftp_client.connect(hostname, port_num) 76 self.ftp_client.login(username, password) 77 except all_errors as exc: 78 raise FTPConnectionFailedException() from exc 79 except ValueError as valuerr: 80 raise InvalidFTPHostNameException() from valuerr 81 82 def __get_ftp_directories(self): 83 """ 84 To retrieve all the directories and files that are present 85 in the current working directory inside this directory 86 in this FTP server. 87 88 Returns:\n 89 list: A complete list of directories and files that are 90 currently available in present working directory. 91 """ 92 dir_list = [] 93 self.ftp_client.retrlines('NLST', dir_list.append) 94 return [item.split(" ")[-1] for item in dir_list] 95 96 def __get_datetime(self): 97 """ 98 Get the today date. 99 100 Returns:\n 101 string: Today's date. 102 """ 103 return datetime.datetime.now().strftime("%Y-%m-%d") 104 105 def __set_data_file_name(self, file_name): 106 """ 107 Set the complete filename of the desired csv file. 108 109 Args: 110 file_name (string): The name of the csv file that wanted 111 to be named. 112 113 Returns:\n 114 string: The complete filename of a csv file. 115 """ 116 return self.__get_datetime() + "_" + str(file_name) + ".csv" 117 118 def __set_log_file_name(self, file_name): 119 """ 120 Get the complete filename of the desired log file. 121 122 Args:\n 123 file_name (string): The name of the log file that wanted 124 to be named. 125 126 Returns:\n 127 string: The complete fiilename of a log file. 128 """ 129 return self.__get_datetime() + "_" + str(file_name) + ".log" 130 131 def create_ftp_log_data(self, directory_path, file_name): 132 """ 133 Create a csv log file inside a FTP server based on the 134 predefined path. 135 136 Args:\n 137 directory_path (string): The path to the csv log file. 138 file_name (string): Filename of the csv log file. 139 140 Raise FTPFileCreationException:\n 141 If the filename and directory path are invalid, a None value 142 or is empty, or is unable to write to a log file in the 143 FTP server. 144 145 """ 146 if file_name == "" or file_name is None or\ 147 isinstance(file_name, str) is False: 148 raise FTPFileCreationException() 149 150 if directory_path == "" or directory_path is None or\ 151 isinstance(directory_path, str) is False: 152 raise FTPFileCreationException() 153 154 try: 155 # get all the directory names 156 directories = directory_path.split('/') 157 for directory in directories: 158 # check if the directory name is present 159 if directory not in self.__get_ftp_directories(): 160 # if not present create the directory 161 # then move to the corresponding directory 162 self.ftp_client.mkd(directory) 163 self.ftp_client.cwd(directory) 164 else: 165 # if already present, 166 # just switch to the corresponding directory 167 self.ftp_client.cwd(directory) 168 169 # Then create the desired log file if the log file 170 # was not present before. Otherwise done nothing. 171 filename = self.__set_data_file_name(file_name) 172 if filename not in self.__get_ftp_directories(): 173 buf = io.BytesIO() 174 buf.write(b"uuid,start,end,result,groundtruth\n") 175 buf.seek(0) 176 self.ftp_client.storbinary(f"STOR {filename}", buf) 177 except all_errors as error: 178 raise FTPFileCreationException() from error 179 180 def set_ftp_log_data(self, directory_path, file_name, log_data): 181 """ 182 Set the csv log file with the desired log data. The new log data 183 will continue log onto the existing file, if the log file was 184 present. Otherwise, new file gonna be created for this new log data. 185 Please do take note that the file type must be of csv type. 186 187 Args:\n 188 directory_path (string): The path to the csv log file. 189 file_name (string): The name of the csv log file. 190 log_data (bytes): The log data. 191 192 Raise NoneValueException:\n 193 If the directory path, file name or the data to be logged 194 is of None value. 195 196 Raise InvalidAttributeException:\n 197 If the directory path, file name is not the type of string, 198 while the log data is not type of byte. 199 200 Raise EmptyParameterException:\n 201 If the directory path, file name or log data is empty. 202 """ 203 if directory_path is None or file_name is None or\ 204 log_data is None: 205 raise NoneValueException() 206 207 if isinstance(directory_path, str) is False or\ 208 isinstance(file_name, str) is False or\ 209 isinstance(log_data, bytes) is False: 210 raise InvalidAttributeException() 211 212 if len(directory_path) == 0 or len(file_name) == 0 or\ 213 log_data == b"": 214 raise EmptyParameterException() 215 216 # get the desired directory name 217 directories = directory_path.split('/') 218 # loop through all the directory name 219 for directory in directories: 220 # if the name present move to the next directory 221 if directory in self.__get_ftp_directories(): 222 self.ftp_client.cwd(directory) 223 224 filename = self.__set_data_file_name(file_name) 225 # check if the corrresponding file name present 226 if filename in self.__get_ftp_directories(): 227 # if present write the log message to the relevant log file 228 buf=io.BytesIO() 229 buf.write(log_data) 230 buf.seek(0) 231 self.ftp_client.storbinary(f"APPE {filename}", buf, 1) 232 233 def create_ftp_log_file(self, directory_path, file_name): 234 """ 235 Create the log file inside an FTP server. 236 237 Args:\n 238 directory_name (string): The path to the created log 239 file. 240 file_name (string): The name of the log file. 241 242 Raise FTPFileCreationException:\n 243 If the file name or directory path is empty, a none value, is not 244 of the type string, or is failed to write the log onto the FTP server. 245 """ 246 if file_name == "" or file_name is None or\ 247 isinstance(file_name, str) is False: 248 raise FTPFileCreationException() 249 250 if directory_path == "" or directory_path is None or\ 251 isinstance(directory_path, str) is False: 252 raise FTPFileCreationException() 253 254 try: 255 # get all the directories 256 directories = directory_path.split("/") 257 for directory in directories: 258 # check if the directory is present 259 # if not present create the directory 260 # then move to the newly create directory 261 if directory not in self.__get_ftp_directories(): 262 self.ftp_client.mkd(directory) 263 self.ftp_client.cwd(directory) 264 # if the directory present 265 # just move to the newly create directory 266 else: 267 self.ftp_client.cwd(directory) 268 269 filename = self.__set_log_file_name(file_name) 270 # check if the log file exist. If not create the 271 # log file. otherwise, done nothing. 272 if filename not in self.__get_ftp_directories(): 273 buf = io.BytesIO() 274 buf.seek(0) 275 self.ftp_client.storbinary(f"STOR {filename}", buf) 276 return True 277 return False 278 except all_errors as error: 279 raise FTPFileCreationException from error 280 281 def set_ftp_log_file(self, directory_path, file_name, message): 282 """ 283 Set the log file with application or system log. If the log file was 284 already existed, the new log will continue appended on to the existing log 285 file. Otherwise, new log file gonna be created. 286 287 Args:\n 288 directory_path (string): The path to the log file. 289 file_name (string): The log file name. 290 message (string): The log message. 291 292 Raise NoneValueException:\n 293 If the directory path and file name is a type None. 294 295 Raise InvalidAttributeException:\n 296 If the directory path, file name or log message is 297 not the type string. 298 299 Raise EmptyParameterException:\n 300 If the directory path, file name or log message is 301 not empty. 302 """ 303 if directory_path is None or file_name is None or\ 304 message is None: 305 raise NoneValueException() 306 307 if isinstance(directory_path, str) is False or\ 308 isinstance(file_name, str) is False or\ 309 isinstance(message, str) is False: 310 raise InvalidAttributeException() 311 312 if len(directory_path) == 0 or len(file_name) == 0 or\ 313 len(message) == 0: 314 raise EmptyParameterException() 315 316 # get the directories name 317 directories = directory_path.split('/') 318 # move to the corresponding directory if it 319 # present 320 for directory in directories: 321 if directory in self.__get_ftp_directories(): 322 self.ftp_client.cwd(directory) 323 324 # check for log file existence. If it exist only 325 # write the log onto the corresponding log file. 326 filename = self.__set_log_file_name(file_name) 327 if filename in self.__get_ftp_directories(): 328 buf = io.BytesIO() 329 buf.write(bytes("["+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+"]"+message+"\n", 'utf-8')) 330 buf.seek(0) 331 self.ftp_client.storbinary(f"APPE {filename}", buf, 1)
Class to establish connection and writing to FTP server.
26 def __init__(self, hostname, port_num, username, password): 27 """ 28 Establish communication with the remote FTP server. 29 30 Args:\n 31 hostname (string): FTP server hostname. 32 port_num (integer): FTP server port number. 33 username (string): FTP server username. 34 password (string): FTP server password. 35 36 Raise InvalidFTPPortNumberException:\n 37 If the port number is not of type int and is equal or less 38 than zero. 39 40 Raise InvalidFTPUserNameException:\n 41 If the username is not of type string or it is an empty 42 string or it is a None value. 43 44 Raise InvalidFTPPasswordException:\n 45 If the password is not of type string or it is an empty 46 string or it is a None value. 47 48 Raise FTPConnectionFailedException:\n 49 If the connection to the FTP remote server was not 50 a success. 51 52 Raise InvalidFTPHostNameException:\n 53 If any of the port number, username, and password 54 are invalid. 55 """ 56 57 if isinstance(port_num, int) is False\ 58 or port_num <= 0: 59 raise InvalidFTPPortNumberException() 60 61 if isinstance(username, str) is False\ 62 or username is None or\ 63 username == "": 64 raise InvalidFTPUserNameException() 65 66 if isinstance(password, str) is False\ 67 or password is None or\ 68 password == "": 69 raise InvalidFTPPasswordException() 70 71 self.ftp_client = FTP() 72 73 try: 74 ipaddress.ip_address(hostname) 75 self.ftp_client.connect(hostname, port_num) 76 self.ftp_client.login(username, password) 77 except all_errors as exc: 78 raise FTPConnectionFailedException() from exc 79 except ValueError as valuerr: 80 raise InvalidFTPHostNameException() from valuerr
Establish communication with the remote FTP server.
Args:
hostname (string): FTP server hostname.
port_num (integer): FTP server port number.
username (string): FTP server username.
password (string): FTP server password.
Raise InvalidFTPPortNumberException:
If the port number is not of type int and is equal or less
than zero.
Raise InvalidFTPUserNameException:
If the username is not of type string or it is an empty
string or it is a None value.
Raise InvalidFTPPasswordException:
If the password is not of type string or it is an empty
string or it is a None value.
Raise FTPConnectionFailedException:
If the connection to the FTP remote server was not
a success.
Raise InvalidFTPHostNameException:
If any of the port number, username, and password
are invalid.
131 def create_ftp_log_data(self, directory_path, file_name): 132 """ 133 Create a csv log file inside a FTP server based on the 134 predefined path. 135 136 Args:\n 137 directory_path (string): The path to the csv log file. 138 file_name (string): Filename of the csv log file. 139 140 Raise FTPFileCreationException:\n 141 If the filename and directory path are invalid, a None value 142 or is empty, or is unable to write to a log file in the 143 FTP server. 144 145 """ 146 if file_name == "" or file_name is None or\ 147 isinstance(file_name, str) is False: 148 raise FTPFileCreationException() 149 150 if directory_path == "" or directory_path is None or\ 151 isinstance(directory_path, str) is False: 152 raise FTPFileCreationException() 153 154 try: 155 # get all the directory names 156 directories = directory_path.split('/') 157 for directory in directories: 158 # check if the directory name is present 159 if directory not in self.__get_ftp_directories(): 160 # if not present create the directory 161 # then move to the corresponding directory 162 self.ftp_client.mkd(directory) 163 self.ftp_client.cwd(directory) 164 else: 165 # if already present, 166 # just switch to the corresponding directory 167 self.ftp_client.cwd(directory) 168 169 # Then create the desired log file if the log file 170 # was not present before. Otherwise done nothing. 171 filename = self.__set_data_file_name(file_name) 172 if filename not in self.__get_ftp_directories(): 173 buf = io.BytesIO() 174 buf.write(b"uuid,start,end,result,groundtruth\n") 175 buf.seek(0) 176 self.ftp_client.storbinary(f"STOR {filename}", buf) 177 except all_errors as error: 178 raise FTPFileCreationException() from error
Create a csv log file inside a FTP server based on the predefined path.
Args:
directory_path (string): The path to the csv log file.
file_name (string): Filename of the csv log file.
Raise FTPFileCreationException:
If the filename and directory path are invalid, a None value
or is empty, or is unable to write to a log file in the
FTP server.
180 def set_ftp_log_data(self, directory_path, file_name, log_data): 181 """ 182 Set the csv log file with the desired log data. The new log data 183 will continue log onto the existing file, if the log file was 184 present. Otherwise, new file gonna be created for this new log data. 185 Please do take note that the file type must be of csv type. 186 187 Args:\n 188 directory_path (string): The path to the csv log file. 189 file_name (string): The name of the csv log file. 190 log_data (bytes): The log data. 191 192 Raise NoneValueException:\n 193 If the directory path, file name or the data to be logged 194 is of None value. 195 196 Raise InvalidAttributeException:\n 197 If the directory path, file name is not the type of string, 198 while the log data is not type of byte. 199 200 Raise EmptyParameterException:\n 201 If the directory path, file name or log data is empty. 202 """ 203 if directory_path is None or file_name is None or\ 204 log_data is None: 205 raise NoneValueException() 206 207 if isinstance(directory_path, str) is False or\ 208 isinstance(file_name, str) is False or\ 209 isinstance(log_data, bytes) is False: 210 raise InvalidAttributeException() 211 212 if len(directory_path) == 0 or len(file_name) == 0 or\ 213 log_data == b"": 214 raise EmptyParameterException() 215 216 # get the desired directory name 217 directories = directory_path.split('/') 218 # loop through all the directory name 219 for directory in directories: 220 # if the name present move to the next directory 221 if directory in self.__get_ftp_directories(): 222 self.ftp_client.cwd(directory) 223 224 filename = self.__set_data_file_name(file_name) 225 # check if the corrresponding file name present 226 if filename in self.__get_ftp_directories(): 227 # if present write the log message to the relevant log file 228 buf=io.BytesIO() 229 buf.write(log_data) 230 buf.seek(0) 231 self.ftp_client.storbinary(f"APPE {filename}", buf, 1)
Set the csv log file with the desired log data. The new log data will continue log onto the existing file, if the log file was present. Otherwise, new file gonna be created for this new log data. Please do take note that the file type must be of csv type.
Args:
directory_path (string): The path to the csv log file.
file_name (string): The name of the csv log file.
log_data (bytes): The log data.
Raise NoneValueException:
If the directory path, file name or the data to be logged
is of None value.
Raise InvalidAttributeException:
If the directory path, file name is not the type of string,
while the log data is not type of byte.
Raise EmptyParameterException:
If the directory path, file name or log data is empty.
233 def create_ftp_log_file(self, directory_path, file_name): 234 """ 235 Create the log file inside an FTP server. 236 237 Args:\n 238 directory_name (string): The path to the created log 239 file. 240 file_name (string): The name of the log file. 241 242 Raise FTPFileCreationException:\n 243 If the file name or directory path is empty, a none value, is not 244 of the type string, or is failed to write the log onto the FTP server. 245 """ 246 if file_name == "" or file_name is None or\ 247 isinstance(file_name, str) is False: 248 raise FTPFileCreationException() 249 250 if directory_path == "" or directory_path is None or\ 251 isinstance(directory_path, str) is False: 252 raise FTPFileCreationException() 253 254 try: 255 # get all the directories 256 directories = directory_path.split("/") 257 for directory in directories: 258 # check if the directory is present 259 # if not present create the directory 260 # then move to the newly create directory 261 if directory not in self.__get_ftp_directories(): 262 self.ftp_client.mkd(directory) 263 self.ftp_client.cwd(directory) 264 # if the directory present 265 # just move to the newly create directory 266 else: 267 self.ftp_client.cwd(directory) 268 269 filename = self.__set_log_file_name(file_name) 270 # check if the log file exist. If not create the 271 # log file. otherwise, done nothing. 272 if filename not in self.__get_ftp_directories(): 273 buf = io.BytesIO() 274 buf.seek(0) 275 self.ftp_client.storbinary(f"STOR {filename}", buf) 276 return True 277 return False 278 except all_errors as error: 279 raise FTPFileCreationException from error
Create the log file inside an FTP server.
Args:
directory_name (string): The path to the created log
file.
file_name (string): The name of the log file.
Raise FTPFileCreationException:
If the file name or directory path is empty, a none value, is not
of the type string, or is failed to write the log onto the FTP server.
281 def set_ftp_log_file(self, directory_path, file_name, message): 282 """ 283 Set the log file with application or system log. If the log file was 284 already existed, the new log will continue appended on to the existing log 285 file. Otherwise, new log file gonna be created. 286 287 Args:\n 288 directory_path (string): The path to the log file. 289 file_name (string): The log file name. 290 message (string): The log message. 291 292 Raise NoneValueException:\n 293 If the directory path and file name is a type None. 294 295 Raise InvalidAttributeException:\n 296 If the directory path, file name or log message is 297 not the type string. 298 299 Raise EmptyParameterException:\n 300 If the directory path, file name or log message is 301 not empty. 302 """ 303 if directory_path is None or file_name is None or\ 304 message is None: 305 raise NoneValueException() 306 307 if isinstance(directory_path, str) is False or\ 308 isinstance(file_name, str) is False or\ 309 isinstance(message, str) is False: 310 raise InvalidAttributeException() 311 312 if len(directory_path) == 0 or len(file_name) == 0 or\ 313 len(message) == 0: 314 raise EmptyParameterException() 315 316 # get the directories name 317 directories = directory_path.split('/') 318 # move to the corresponding directory if it 319 # present 320 for directory in directories: 321 if directory in self.__get_ftp_directories(): 322 self.ftp_client.cwd(directory) 323 324 # check for log file existence. If it exist only 325 # write the log onto the corresponding log file. 326 filename = self.__set_log_file_name(file_name) 327 if filename in self.__get_ftp_directories(): 328 buf = io.BytesIO() 329 buf.write(bytes("["+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+"]"+message+"\n", 'utf-8')) 330 buf.seek(0) 331 self.ftp_client.storbinary(f"APPE {filename}", buf, 1)
Set the log file with application or system log. If the log file was already existed, the new log will continue appended on to the existing log file. Otherwise, new log file gonna be created.
Args:
directory_path (string): The path to the log file.
file_name (string): The log file name.
message (string): The log message.
Raise NoneValueException:
If the directory path and file name is a type None.
Raise InvalidAttributeException:
If the directory path, file name or log message is
not the type string.
Raise EmptyParameterException:
If the directory path, file name or log message is
not empty.