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