def write_file(file_path: str, data: object, append=False): """ Write the data back to file at once """ # data is null if not data: raise NoAvailableResourcesFoundException( "Data cannot be null or empty") # file path is null if not file_path: raise NoAvailableResourcesFoundException("File path is incorrect") # list conversion if isinstance(data, list): data = Convert.list_to_string(data) # dictionary conversion if isinstance(data, dict): data = Convert.dict_to_string(data) # neither str nor bytes # trying to convert the data to string if not isinstance(data, str) and not isinstance(data, bytes): data = str(data) # data is bytes if isinstance(data, bytes): if append: f = open(file=file_path, mode='a+b') else: f = open(file=file_path, mode='wb') # data is str elif isinstance(data, str): if append: f = open(file=file_path, mode='a+') else: f = open(file=file_path, mode='w') else: raise InvalidParamException( 'Cannot write data to file, because of unknown type of data') if f is None or not f.writable(): raise NoAvailableResourcesFoundException("Cannot write file") # write file f.write(data) f.close()
def encode(self, data: object): """ convert given data into redis token Args: * [data(any)] data can be anything, bytes, int, float or whatever Returns: * [token(bytes)] composed token consisted of data, type, timestamp """ # reset self.reset() if data is None: raise excepts.NullPointerException("data cannot be a null type") # 1. create timestamp self.timestamp = ticker.time_since1970() # 2. assign bytes to self.data if type(data) is bytes: self.dtype = "bytes" self.data = convert.binary_to_string(base64.b64encode(data)) else: # other types self.data = data if type(data) is int: self.dtype = "int" elif type(data) is float: self.dtype = "float" elif type(data) is str: self.dtype = "str" elif type(data) is list: self.dtype = "list" elif type(data) is dict: self.dtype = "dict" else: raise excepts.InvalidParamException( "{} cannot be processed into string nor bytes".format( type(data))) # 3. composing a token dictionary type self.token = { "data": self.data, "type": self.dtype, "timestamp": self.timestamp } # return to caller return convert.dict_to_binary(self.token)
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 response(httpcode, matsukicode, msg = None, data = None): res = {} # convert enum to int res['http'] = int(httpcode) res['code'] = int(matsukicode) # convert msg if necessary if msg is not None: if isinstance(msg, bytes): res['feedback'] = Convert.binary_to_string(msg) elif isinstance(msg, list): res['feedback'] = Convert.list_to_string(msg) elif isinstance(msg, dict): res['feedback'] = Convert.dict_to_string(msg) else: res['feedback'] = str(msg) # convert data if necessary if data is not None: if isinstance(data, bytes): res['data'] = Convert.binary_to_string(data) elif isinstance(data, list): res['data'] = Convert.list_to_string(data) elif isinstance(data, dict): res['data'] = Convert.dict_to_string(data) else: res['data'] = str(data) # return to caller return res
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")
def _compute_base64(data: bytes): import base64 # To be extra safe in python 3, encode text conditionally before concatenating with pad. if isinstance(data, str): data = data.encode('utf-8') if not isinstance(data, bytes): raise InvalidParamException( "The parameter is neither a string nor byte array type") r = base64.b64encode(data) return Convert.binary_to_string(r)
def load_json_file(filename: str): """ Create a blank file or load data from json file @Args: * [filename] (str), filename with path @Returns: *[dict] if load success. None, failed, empty data """ # load json file from given filename raw = fileutil.read_file(filename) if raw: return decode_json(convert.binary_to_string(raw)) else: return {}
def read_file_by_line(file_path: str): """ Read the data line by line of a given file """ # read whole file content data = read_file(file_path) if data is not None and len(data) > 0: str_context = Convert.binary_to_string(data) lines = str_context.split('\n') for line in lines: yield line else: yield ''
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 __str__(self): return Convert.dict_to_string(self.generate_tree())