def message(self, priority, title=None, msg=None, exception=None): import traceback # check log is validate if self.m_bUseLog: self.m_file = _check_valid_file(self.m_dir, self.m_file) error_line = "{0} <{1}>".format(TimeTicker.time_with_msg(), _priority_name(priority)) if title is not None: error_line += "\t[{0}]".format(title) if msg is not None: error_line += "\t{0}".format(msg) # if SystemUtils.is_windows(): if "Windows" in platform.system(): error_line += "\r\n" else: error_line += "\n" if exception is not None: error = ''.join( traceback.format_exception(etype=type(exception), value=exception, tb=exception.__traceback__)) error_line += "{0}".format(error) if self.m_bUseLog: FileUtils.write_file(self.m_file, Convert.string_to_binary(error_line), True) if self.m_bStdOut: print(error_line)
def write_json_file(dictionary: dict, filename: str): """ Write dictionary to json file @Args: * [dictionary] (dict), key value pairs * [filename] (str), filename with path """ if len(dictionary) > 0: raw = convert.string_to_binary(json.dumps(dictionary)) fileutil.write_file(filename, raw)
def data_in_hashcode(self, priority, title: str, data: bytes): # check log is validate f = _check_valid_file(self.m_dir, self.m_file) line = "{0} <{1}> [{2}]".format(TimeTicker.time_with_msg(), _priority_name(priority), title) # if SystemUtils.is_windows(): if "Windows" in platform.system(): line += "\r\n Data: {0}\r\n".format(Hashcode.md5(data)) else: line += "\n Data: {0}\n".format(Hashcode.md5(data)) if self.m_bUseLog: FileUtils.write_file(f, Convert.string_to_binary(line), True) if self.m_bStdOut: print(line)
def decode(self, token: bytes): """ since obtained token from redis, use this method to convert the bytes into correct data type Args: * [token(bytes)] obtained bytes from redis Returns: * [token(dict)] contains data, timestamp, type infomation """ # reset all self.reset() if token is not None: # 1, convert the bytes into dictionary self.token = convert.binary_to_dict(token) # 2. assign to attributes self.dtype = self.token["type"] self.timestamp = self.token["timestamp"] # 3. if bytes, process them carefully if self.dtype == "bytes": self.data = base64.b64decode( convert.string_to_binary(self.token["data"])) else: # normal data self.data = self.token["data"] # 4. return to caller self.token = { "data": self.data, "type": self.dtype, "timestamp": self.timestamp } return self.token else: raise excepts.NullPointerException("token cannot be a null type")