1import sys 2import typing 3import os 4import glob 5import re 6from pathlib import Path 7from typing import * 8 9def unit_adaptive(size: int) -> str: 10 unit_list = ["Byte", "KB", "MB", "GB"] 11 index = 0 12 while index < len(unit_list) and size >= 1024: 13 size /= 1024 14 index += 1 15 if index == len(unit_list): 16 index = len(unit_list)-1 17 size *= 1024 18 return str(round(size,2))+unit_list[index] 19 20class BasicTool: 21 @classmethod 22 def match_paragraph(cls, content: str, start_pattern: str = r"\w+\(\".*?\"\) *{", end_pattern: str = "\}") -> \ 23 Iterator[re.Match]: 24 """ 25 匹配代码段,支持单行 26 注意:ptrn中已经包含前面的空格,所以start_pattern中可以省略 27 :param content: 被匹配的字符串 28 :param start_pattern: 模式的开头 29 :param end_pattern: 模式的结尾 30 :return: 匹配到的段落的迭代器 31 """ 32 ptrn = r'^( *){s}(?#匹配开头).*?(?#中间非贪婪)\1(?#如果开头前面有空格,则结尾的前面应该有相同数量的空格)?{e}$(?#匹配结尾)'.format( 33 s=start_pattern, e=end_pattern) 34 ptrn = re.compile(ptrn, re.M | re.S) 35 result = re.finditer(ptrn, content) 36 return result 37 38 @classmethod 39 def find_all_files(cls, folder: str, real_path: bool = True, apply_abs: bool = True, de_duplicate: bool = True, 40 p_filter: typing.Callable = lambda x: True) -> list: 41 filepath_list = set() 42 for root, _, file_names in os.walk(folder): 43 filepath_list.update( 44 [os.path.abspath(os.path.realpath( 45 os.path.join(root, f) if real_path else os.path.join(root, f))) if apply_abs else os.path.relpath( 46 os.path.realpath(os.path.join(root, f) if real_path else os.path.join(root, f))) for f in file_names 47 if p_filter(os.path.join(root, f))]) 48 if de_duplicate: 49 filepath_list = set(filepath_list) 50 filepath_list = sorted(filepath_list, key=str.lower) 51 return filepath_list 52 53 @classmethod 54 def get_abs_path(cls, path: str) -> str: 55 return os.path.abspath(os.path.expanduser(path)) 56 57 @classmethod 58 def re_group_1(cls, content: str, pattern: str, **kwargs) -> str: 59 """ 60 匹配正则表达式,如果有匹配到内容,返回group(1)的内容 61 :param content: 要被匹配的内容 62 :param pattern: 进行匹配的模式 63 :return: 匹配到的结果(group(1)) 64 TODO 对()的检查应该更严格 65 """ 66 if not (r'(' in pattern and r')' in pattern): 67 raise ValueError("parentheses'()' must in the pattern") 68 result = re.search(pattern, content, **kwargs) 69 if result: 70 return result.group(1) 71 return str() 72 73 @classmethod 74 def execute(cls, cmd: str, post_processor: Callable[[Text], Text] = lambda x:x) -> Any: 75 """ 76 封装popen,返回标准输出的列表 77 :param post_processor: 对执行结果进行处理 78 :param cmd: 待执行的命令 79 :return: 经处理过后的字符串列表 80 81 """ 82 output = os.popen(cmd).read() 83 output = post_processor(output) 84 return output 85 86 87if __name__ == '__main__': 88 for i in BasicTool.find_all_files(".", apply_abs=False): 89 print(i)