def fetch(self, url=None): request_url = None if url is not None: request_url = str(url) else: request_url = self.url() if request_url is None: Base.log(self, message="HttpOperation " + "request : " + " \error : \n url cannot be None", message_type=MESSAGE_TYPE.ERROR) return try: resp = urllib2.urlopen(request_url) data_string = resp.read().decode('utf-8') Base.log(self, message="HttpOperation " + "request : " + str(request_url) + " \nresponse : \n" + data_string, message_type=MESSAGE_TYPE.INFO) return data_string except urllib2.HTTPError as e: Base.log(self, message="HttpOperation " + "request : " + " \HTTPError : \n " + str(e.code), message_type=MESSAGE_TYPE.ERROR) except urllib2.URLError as e: Base.log(self, message="HttpOperation " + "request : " + " \\URLError : \n " + str(e.reason), message_type=MESSAGE_TYPE.ERROR) except Exception as e: Base.log(self, message="HttpOperation " + "request : " + " \generic exception : \n " + str(e), message_type=MESSAGE_TYPE.ERROR) return None
def remove(self, filePath): if self.isExist(filePath): os.remove(filePath) Base.log(self, message="FileOperation " + "remove : " + filePath + "\n file was removed", messageType=MESSAGETYPE.INFO) else: Base.log(self, message="FileOperation " + "remove : " + filePath + "\n has not found file", messageType=MESSAGETYPE.INFO) return ""
def readContent(self,filePath): global content if self.isExist(filePath): with open(filePath, "r") as data: content = data.read() return content else: Base.log(self, message="FileOperation " + "readContent : " + filePath + "\n has not found file", messageType=MESSAGETYPE.INFO) return ""
def create(self,filePath, content): try: filePath = open(filePath, "w") filePath.write(content) filePath.close() Base.log(self, message="FileOperation " + "create : " + str(filePath) + " \ncontent : \n" + content, messageType=MESSAGETYPE.INFO) except OSError as e: Base.log(self,message = "FileOperation " + "create : " + " \error : \n" + str(e), messageType=MESSAGETYPE.ERROR)
def appendFile(self, fileName, content,isTruncate=False): filePath = self.getPath() + CODING.SLASH + fileName try: with open(filePath, "a+") as f: # f.seek(0) if isTruncate: f.truncate() f.write(content) f.close() Base.log(self,message = "FileOperation " + "appendFile : " + filePath + "\n", messageType=MESSAGETYPE.INFO) except OSError as e: Base.log(self,message = "FileOperation " + "appendFile : " + " \error : \n" + str(e), messageType=MESSAGETYPE.ERROR)
def createFile(self, file_name, content): file_path = self.getPath() + CODING.SLASH + file_name try: file_path = open(file_path, "w") file_path.write(content) file_path.close() Base.log(self, message="FileOperation " + "createFile : " + str(file_path) + " \ncontent : \n" + content, message_type=MESSAGE_TYPE.INFO) except OSError as e: Base.log(self, message="FileOperation " + "createFile : " + " \error : \n" + str(e), message_type=MESSAGE_TYPE.ERROR)
def request(self, url=None, http_method="GET"): request_url = None if url is not None: request_url = str(url) else: request_url = self.url() if request_url is None: Base.log(self, message="HttpOperation " + "request : " + " \error : \n url cannot be None", message_type=MESSAGE_TYPE.ERROR) return try: http = urllib3.PoolManager() resp = http.request(http_method, request_url) data_string = resp.data.decode('utf-8') Base.log(self, message="HttpOperation " + "request : " + str(request_url) + " \nresponse : \n" + data_string, message_type=MESSAGE_TYPE.INFO) self._response = data_string return self except urllib3.exceptions.HTTPError as e: Base.log(self, message="HttpOperation " + "request : " + " \HTTPError : \n " + str(e.code), message_type=MESSAGE_TYPE.ERROR) except urllib3.exceptions.RequestError as e: Base.log(self, message="HttpOperation request : URLError : " + str(e.reason), message_type=MESSAGE_TYPE.ERROR) except Exception as e: Base.log(self, message="HttpOperation " + "request : " + " \generic exception : \n " + str(e), message_type=MESSAGE_TYPE.ERROR) self._response = None return None
def createFolder(self, folderName): folderPath = self.getPath() + CODING.SLASH + folderName try: if not os.path.isdir(folderPath): os.makedirs(folderPath) Base.log(self,message = "FileOperation " + "createFolder : " + folderPath + "\n", messageType=MESSAGETYPE.INFO) else : Base.log(self,message = "FileOperation " + "createFolder : " + folderPath + "\n already path", messageType=MESSAGETYPE.INFO) except OSError as e: Base.log(self,message = "FileOperation " + "createFileWithPath : " + " \error : \n" + str(e), messageType=MESSAGETYPE.ERROR)
def createFolderWithoutPath(self, folder_name): folder_path = folder_name try: if not os.path.isdir(folder_path): os.makedirs(folder_path) Base.log(self, message="FileOperation " + "createFolder : " + folder_path + "\n", message_type=MESSAGE_TYPE.INFO) else: Base.log(self, message="FileOperation " + "createFolder : " + folder_path + "\n already path", message_type=MESSAGE_TYPE.INFO) except OSError as e: Base.log(self, message="FileOperation " + "createFileWithPath : " + " \error : \n" + str(e), message_type=MESSAGE_TYPE.ERROR)