def __split_message(message: any): if is_tuple(message): message = list(message) if is_list(message): text = MessageManager.__format_text( message[0]) if message.__len__() > 0 else None callbacks = message[1] if message.__len__() > 1 else None sizes = message[2] if message.__len__() > 2 else None return text, callbacks, sizes elif is_str(message): return MessageManager.__format_text(message), None, None
def find_file(re_filter=None, branch=".", recurse=True, depth=0, get_file=True, get_dir=True, case_i=True): """Search a branch directory for files and / or directories whose name matches one or more of the filter patterns Args: branch (str): (optional) Directory to search. Suggestion: use absolute path. Defaults to current working directory. re_filter (list): (optional) List of strings of wildcard patterns. Defaults to None, which skips any filtering. recurse (bool): (optional) Recursive search flag. Defaults to True. depth (int): (optional) Set the recursion depth. Defaults to 0, which has a max of Python's recursion limit. get_file (bool): (optional) Return entry for file. Defaults to True. get_dir (bool): (optional) Return entry for directory. Defaults to True. case_i (bool): (optional) Case insensitive search. Defaults to True. Returns: list: List of strings with paths of files. """ if not is_str_not_empty(branch): raise ValueError("branch must be a non empty string") if not os.path.isdir(branch): raise ValueError("branch must be a valid directory") if not is_bool(recurse): raise ValueError("recurse must be True or False") if is_int_neg(depth): raise ValueError("depth must be >= 0") if not is_bool(get_file): raise ValueError("get_file must be True or False") if not is_bool(get_dir): raise ValueError("get_dir must be True or False") if not is_bool(case_i): raise ValueError("case_i must be True or False") search_results = list_dir(branch, recurse, depth, get_file, get_dir) if re_filter is None or len(re_filter) < 1: return search_results elif re_filter == "*": return search_results elif is_list(re_filter) and "*" in re_filter: return search_results if is_str(re_filter): re_filter = [re_filter] re_flag = 0 if case_i: re_flag = re.IGNORECASE bre = partial(wildcard_re, wildcard="*", escape="\\") return list_filter(map(bre, re_filter), search_results, re_flag)
def get_disk_info(route: str, title: str = None): text = '' try: disk_usage = psutil.disk_usage(route) text += format_table_row(title if is_str(title) else route) text += format_table_row('Used', format_bytes(disk_usage.used)) text += format_table_row('Used%', format_percent(disk_usage.percent)) text += format_table_row('Free', format_bytes(disk_usage.free)) text += format_table_row('Free%', format_percent(100 - disk_usage.percent)) except FileNotFoundError as e: logging.error(e.strerror) return text
def find_file_re(re_filter=None, branch=".", recurse=True, depth=0, get_file=True, get_dir=True, re_flag=0): """Search a branch directory for files and / or directories whose name matches one or more of the regex filter patterns Args: branch (str): (optional) Directory to search. Suggestion: use absolute path. Defaults to current working directory. re_filter (list): (optional) List of strings of regular expression patterns. Defaults to None, which skips any filtering. recurse (bool): (optional) Recursive search flag. Defaults to True. depth (int): (optional) Set the recursion depth. Defaults to 0, which has a max of Python's recursion limit. get_file (bool): (optional) Return entry for file. Defaults to True. get_dir (bool): (optional) Return entry for directory. Defaults to True. re_flag (int): (optional) See :py:mod:`re` Returns: list: List of strings with paths of files. """ if not is_str_not_empty(branch): raise ValueError("branch must be a non empty string") if not os.path.isdir(branch): raise ValueError("branch must be a valid directory") if not is_bool(recurse): raise TypeError("recurse must be True or False") if is_int_neg(depth): raise ValueError("depth must be >= 0") if not is_bool(get_file): raise ValueError("get_file must be True or False") if not is_bool(get_dir): raise ValueError("get_dir must be True or False") search_results = list_dir(branch, recurse, depth, get_file, get_dir) if re_filter is None or len(re_filter) < 1: return search_results if is_str(re_filter): re_filter = [re_filter] return list_filter(re_filter, search_results, re_flag)
def __make_reply_markup(callbacks: list, sizes: list, add_dismiss: bool): if add_dismiss: callbacks, sizes = MessageManager.__append_dismiss_callback( callbacks, sizes) if is_list_of_min_size(callbacks) and is_list_of_min_size(sizes): keyboard = [] line = [] keyboard.append(line) # i, n = 0, 1 for callback in callbacks: if is_list_of_size(callback, 2) and i < sizes.__len__() \ and is_str(callback[0]) and callback[1] is not None: text = emoji.emojize(callback[0]) line.append( InlineKeyboardButton(text, callback_data=str(callback[1]))) # if sizes[i] <= n: n = 1 line = [] keyboard.append(line) # else: n += 1 i += 1 return InlineKeyboardMarkup(keyboard) return None