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 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 __str__(self): return Convert.dict_to_string(self.generate_tree())