pyxllib 0.3.197__py3-none-any.whl → 0.3.200__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. pyxllib/__init__.py +21 -21
  2. pyxllib/algo/__init__.py +8 -8
  3. pyxllib/algo/disjoint.py +54 -54
  4. pyxllib/algo/geo.py +541 -541
  5. pyxllib/algo/intervals.py +964 -964
  6. pyxllib/algo/matcher.py +389 -389
  7. pyxllib/algo/newbie.py +166 -166
  8. pyxllib/algo/pupil.py +629 -629
  9. pyxllib/algo/shapelylib.py +67 -67
  10. pyxllib/algo/specialist.py +241 -241
  11. pyxllib/algo/stat.py +494 -494
  12. pyxllib/algo/treelib.py +149 -149
  13. pyxllib/algo/unitlib.py +66 -66
  14. pyxllib/autogui/__init__.py +5 -5
  15. pyxllib/autogui/activewin.py +246 -246
  16. pyxllib/autogui/all.py +9 -9
  17. pyxllib/autogui/autogui.py +852 -852
  18. pyxllib/autogui/uiautolib.py +362 -362
  19. pyxllib/autogui/virtualkey.py +102 -102
  20. pyxllib/autogui/wechat.py +827 -827
  21. pyxllib/autogui/wechat_msg.py +421 -421
  22. pyxllib/autogui/wxautolib.py +84 -84
  23. pyxllib/cv/__init__.py +5 -5
  24. pyxllib/cv/expert.py +267 -267
  25. pyxllib/cv/imfile.py +159 -159
  26. pyxllib/cv/imhash.py +39 -39
  27. pyxllib/cv/pupil.py +9 -9
  28. pyxllib/cv/rgbfmt.py +1525 -1525
  29. pyxllib/cv/slidercaptcha.py +137 -137
  30. pyxllib/cv/trackbartools.py +251 -251
  31. pyxllib/cv/xlcvlib.py +1040 -1040
  32. pyxllib/cv/xlpillib.py +423 -423
  33. pyxllib/data/echarts.py +240 -240
  34. pyxllib/data/jsonlib.py +89 -89
  35. pyxllib/data/oss.py +72 -72
  36. pyxllib/data/pglib.py +1127 -1127
  37. pyxllib/data/sqlite.py +568 -568
  38. pyxllib/data/sqllib.py +297 -297
  39. pyxllib/ext/JLineViewer.py +505 -505
  40. pyxllib/ext/__init__.py +6 -6
  41. pyxllib/ext/demolib.py +246 -246
  42. pyxllib/ext/drissionlib.py +277 -277
  43. pyxllib/ext/kq5034lib.py +12 -12
  44. pyxllib/ext/old.py +663 -663
  45. pyxllib/ext/qt.py +449 -449
  46. pyxllib/ext/robustprocfile.py +497 -497
  47. pyxllib/ext/seleniumlib.py +76 -76
  48. pyxllib/ext/tk.py +173 -173
  49. pyxllib/ext/unixlib.py +827 -827
  50. pyxllib/ext/utools.py +351 -351
  51. pyxllib/ext/webhook.py +124 -119
  52. pyxllib/ext/win32lib.py +40 -40
  53. pyxllib/ext/wjxlib.py +88 -88
  54. pyxllib/ext/wpsapi.py +124 -124
  55. pyxllib/ext/xlwork.py +9 -9
  56. pyxllib/ext/yuquelib.py +1105 -1105
  57. pyxllib/file/__init__.py +17 -17
  58. pyxllib/file/docxlib.py +761 -761
  59. pyxllib/file/gitlib.py +309 -309
  60. pyxllib/file/libreoffice.py +165 -165
  61. pyxllib/file/movielib.py +148 -148
  62. pyxllib/file/newbie.py +10 -10
  63. pyxllib/file/onenotelib.py +1469 -1469
  64. pyxllib/file/packlib/__init__.py +330 -330
  65. pyxllib/file/packlib/zipfile.py +2441 -2441
  66. pyxllib/file/pdflib.py +426 -426
  67. pyxllib/file/pupil.py +185 -185
  68. pyxllib/file/specialist/__init__.py +685 -685
  69. pyxllib/file/specialist/dirlib.py +799 -799
  70. pyxllib/file/specialist/download.py +193 -193
  71. pyxllib/file/specialist/filelib.py +2829 -2829
  72. pyxllib/file/xlsxlib.py +3131 -3131
  73. pyxllib/file/xlsyncfile.py +341 -341
  74. pyxllib/prog/__init__.py +5 -5
  75. pyxllib/prog/cachetools.py +64 -64
  76. pyxllib/prog/deprecatedlib.py +233 -233
  77. pyxllib/prog/filelock.py +42 -42
  78. pyxllib/prog/ipyexec.py +253 -253
  79. pyxllib/prog/multiprogs.py +940 -940
  80. pyxllib/prog/newbie.py +451 -451
  81. pyxllib/prog/pupil.py +1197 -1197
  82. pyxllib/prog/sitepackages.py +33 -33
  83. pyxllib/prog/specialist/__init__.py +391 -391
  84. pyxllib/prog/specialist/bc.py +203 -203
  85. pyxllib/prog/specialist/browser.py +497 -497
  86. pyxllib/prog/specialist/common.py +347 -347
  87. pyxllib/prog/specialist/datetime.py +198 -198
  88. pyxllib/prog/specialist/tictoc.py +240 -240
  89. pyxllib/prog/specialist/xllog.py +180 -180
  90. pyxllib/prog/xlosenv.py +108 -108
  91. pyxllib/stdlib/__init__.py +17 -17
  92. pyxllib/stdlib/tablepyxl/__init__.py +10 -10
  93. pyxllib/stdlib/tablepyxl/style.py +303 -303
  94. pyxllib/stdlib/tablepyxl/tablepyxl.py +130 -130
  95. pyxllib/text/__init__.py +8 -8
  96. pyxllib/text/ahocorasick.py +39 -39
  97. pyxllib/text/airscript.js +744 -744
  98. pyxllib/text/charclasslib.py +121 -121
  99. pyxllib/text/jiebalib.py +267 -267
  100. pyxllib/text/jinjalib.py +32 -32
  101. pyxllib/text/jsa_ai_prompt.md +271 -271
  102. pyxllib/text/jscode.py +922 -922
  103. pyxllib/text/latex/__init__.py +158 -158
  104. pyxllib/text/levenshtein.py +303 -303
  105. pyxllib/text/nestenv.py +1215 -1215
  106. pyxllib/text/newbie.py +300 -300
  107. pyxllib/text/pupil/__init__.py +8 -8
  108. pyxllib/text/pupil/common.py +1121 -1121
  109. pyxllib/text/pupil/xlalign.py +326 -326
  110. pyxllib/text/pycode.py +47 -47
  111. pyxllib/text/specialist/__init__.py +8 -8
  112. pyxllib/text/specialist/common.py +112 -112
  113. pyxllib/text/specialist/ptag.py +186 -186
  114. pyxllib/text/spellchecker.py +172 -172
  115. pyxllib/text/templates/echart_base.html +10 -10
  116. pyxllib/text/templates/highlight_code.html +16 -16
  117. pyxllib/text/templates/latex_editor.html +102 -102
  118. pyxllib/text/vbacode.py +17 -17
  119. pyxllib/text/xmllib.py +747 -747
  120. pyxllib/xl.py +42 -39
  121. pyxllib/xlcv.py +17 -17
  122. {pyxllib-0.3.197.dist-info → pyxllib-0.3.200.dist-info}/METADATA +1 -1
  123. pyxllib-0.3.200.dist-info/RECORD +126 -0
  124. {pyxllib-0.3.197.dist-info → pyxllib-0.3.200.dist-info}/licenses/LICENSE +190 -190
  125. pyxllib-0.3.197.dist-info/RECORD +0 -126
  126. {pyxllib-0.3.197.dist-info → pyxllib-0.3.200.dist-info}/WHEEL +0 -0
@@ -1,203 +1,203 @@
1
- #!/usr/bin/env python3
2
- # -*- coding: utf-8 -*-
3
- # @Author : 陈坤泽
4
- # @Email : 877362867@qq.com
5
- # @Date : 2020/06/01 18:13
6
-
7
- import copy
8
-
9
- from pyxllib.prog.pupil import dprint, prettifystr
10
- from pyxllib.prog.specialist.browser import Explorer
11
- from pyxllib.algo.pupil import intersection_split
12
- from pyxllib.file.specialist import File, Dir, filesmatch, get_encoding, XlPath
13
-
14
-
15
- # 需要使用的第三方软件
16
- # BCompare.exe, bcompare函数要用
17
-
18
- class BCompare(Explorer):
19
- def __init__(self, app='bcomp', shell=False):
20
- """
21
- 240512周日20:06,本来写的是BCompare,但是友鑫mac电脑上发现似乎有问题,所以改成bcomp,这种在windows上也能用
22
- """
23
- super().__init__(app, shell)
24
-
25
- @classmethod
26
- def to_bcompare_files(cls, *args, files=None):
27
- """ 这个需要一次性获得所有的数据,才适合分析整体上要怎么获取对应的多个文件
28
-
29
- :param files: 每个arg对应的文件名,默认按 'left'、'right', 'base' 来生成
30
- 也可以输入一个list[str],表示多个args依次对应的文件名
31
- filename的长度可以跟args不一致,多的不用,少的自动生成
32
- """
33
- # 1 如果oldfile和newfile都是dict、set、list、tuple,则使用特殊方法文本化
34
- # 如果两个都是list,不应该提取key后比较,所以限制第1个类型必须是dict或set,然后第二个类型可以适当放宽条件
35
- # if not oldfile: oldfile = str(oldfile)
36
- if len(args) > 1 and isinstance(args[0], (dict, set)) and isinstance(args[1], (dict, set, list, tuple)):
37
- args = copy.copy(list(args))
38
- t = [prettifystr(li) for li in intersection_split(args[0], args[1])]
39
- args[0] = f'【共有部分】,{t[0]}\n\n【独有部分】,{t[1]}'
40
- args[1] = f'【共有部分】,{t[2]}\n\n【独有部分】,{t[3]}'
41
-
42
- # 2 参数对齐
43
- if not isinstance(files, (list, tuple)):
44
- files = [files]
45
- if len(files) < len(args):
46
- files += [None] * (len(args) - len(files))
47
- ref_names = ['left', 'right', 'base']
48
-
49
- # 3 将每个参数转成一个文件
50
- new_args = []
51
- default_suffix = None
52
- for i, arg in enumerate(args):
53
- f = XlPath.safe_init(arg)
54
- if f is not None and f.is_file(): # 是文件对象,且存在
55
- new_args.append(f)
56
- if not default_suffix:
57
- default_suffix = f.suffix
58
- # elif isinstance(f, File): # 文本内容也可能生成合法的伪路径,既然找不到还是统一按字符串对比差异好
59
- # # 是文件对象,但不存在 -> 报错
60
- # raise FileNotFoundError(f'{f}')
61
- else: # 不是文件对象,要转存到文件
62
- if not files[i]: # 没有设置文件名则生成一个
63
- files[i] = XlPath.init(ref_names[i], XlPath.tempdir(), suffix=default_suffix)
64
- else:
65
- files[i] = XlPath(files[i])
66
- files[i].write_text(str(arg))
67
- new_args.append(files[i])
68
-
69
- return new_args
70
-
71
- def __call__(self, *args, wait=True, files=None, sameoff=False, **kwargs):
72
- r"""
73
- :param wait:
74
- 一般调用bcompare的时候,默认值wait=True,python是打开一个子进程并等待bcompare软件关闭后,才继续执行后续代码。
75
- 如果你的业务场景并不需要等待bcompare关闭后执行后续python代码,可以设为wait=False,不等待。
76
- :param sameoff: 如果设为True,则会判断内容,两份内容如果相同则不打开bc
77
- 这个参数一定程度会影响性能,非必要的时候不要开。
78
- 或者在外部的时候,更清楚数据情况,可以在外部判断内容不重复再跑bcompare函数
79
- 这个目前的实现策略,是读取文件重新判断的,会有性能开销,默认关闭该功能
80
- :return: 程序返回被修改的oldfile内容
81
- 注意如果没有修改,或者wait=False,会返回原始值
82
- """
83
- files = self.to_bcompare_files(*args, files=files)
84
-
85
- if sameoff and len(files) > 1:
86
- if files[0].read_auto() == files[1].read_auto():
87
- return
88
- super().__call__(*([str(f) for f in files]), wait=wait, **kwargs)
89
- # bc软件操作中可能会修改原文内容,所以这里需要重新读取,不能用前面算过的结果
90
- return XlPath(files[0]).read_auto()
91
-
92
-
93
- bcompare = BCompare() # nowatch: 调试阶段,不需要自动watch的变量
94
-
95
-
96
- def modify_file(file, func, *, outfile=None, file_mode=None, debug=0):
97
- """ 对单个文件就行优化的功能函数
98
-
99
- 这样一些底层函数功能可以写成数据级的接口,然后由这个函数负责文件的读写操作,并且支持debug比较前后内容差异
100
-
101
- :param outfile: 默认是对file原地操作,如果使用该参数,则将处理后的内容写入outfile文件
102
- :param file_mode: 指定文件读取类型格式,例如'.json'是json文件,读取为字典
103
- :param debug: 这个功能可以对照refine分级轮理解
104
- 无outfile参数时,原地操作
105
- 0,【直接原地操作】关闭调试,直接运行 (根据outfile=None选择原地操作,或者指定生成新文件)
106
- 1,【进行审核】打开BC比较差异,左边原始内容,右边参考内容 (打开前后差异内容对比)
107
- -1,【先斩后奏】介于完全不调试和全部人工检查之间,特殊的差异比较。左边放原始文件修改后的结果,右边对照原内容。
108
- 有outfile参数时
109
- 0 | False,直接生成目标文件,如果outfile已存在会被覆盖
110
- 1 | True,直接生成目标文件,但是会弹出bc比较前后内容差异 (相同内容不会弹出)
111
- """
112
- infile = File(file)
113
- enc = get_encoding(infile.read(mode='b'))
114
- data = infile.read(mode=file_mode, encoding=enc)
115
- origin_content = str(data)
116
- new_data = func(data)
117
-
118
- isdiff = origin_content != str(new_data)
119
- if outfile is None: # 原地操作
120
- if isdiff: # 内容不同才会有相关debug功能,否则静默跳过就好
121
- if debug == 0:
122
- infile.write(new_data, mode=file_mode) # 直接处理
123
- elif debug == 1:
124
- temp_file = File('refine_content', Dir.TEMP, suffix=infile.suffix).write(new_data)
125
- bcompare(infile, temp_file) # 使用beyond compare软件打开对比查看
126
- elif debug == -1:
127
- temp_file = File('origin_content', Dir.TEMP, suffix=infile.suffix)
128
- infile.copy(temp_file)
129
- infile.write(new_data, mode=file_mode, encoding=enc) # 把原文件内容替换了
130
- bcompare(infile, temp_file) # 然后显示与旧内容进行对比
131
- else:
132
- raise ValueError(f'{debug}')
133
- else:
134
- outfile = File(outfile)
135
- outfile.write(new_data, mode=file_mode, encoding=enc) # 直接处理
136
- if debug and isdiff:
137
- bcompare(infile, outfile)
138
-
139
- return isdiff
140
-
141
-
142
- class PairContent:
143
- """ 配对文本类,主要用于bc差异比较 """
144
-
145
- def __init__(self, left_file_name=None, right_file_name=None):
146
- self.left_file = File(left_file_name) if left_file_name else left_file_name
147
- self.right_file = File(right_file_name) if right_file_name else right_file_name
148
- self.left, self.right = [], []
149
-
150
- def add(self, lt, rt=None):
151
- """ rt不加,默认本轮内容是同lt """
152
- lt = str(lt)
153
- self.left.append(lt)
154
- rt = lt if rt is None else str(rt)
155
- self.right.append(rt)
156
-
157
- def bcompare(self, **kwargs):
158
- left, right = '\n'.join(self.left), '\n'.join(self.right)
159
- if self.left_file is not None:
160
- left = self.left_file.write(left)
161
- if self.right_file is not None:
162
- right = self.right_file.write(right)
163
- bcompare(left, right, **kwargs)
164
-
165
-
166
- def filetext_replace(files, func, *,
167
- count=-1, start=1, bc=False, write=False, if_exists=None):
168
- r"""遍历目录下的文本文件进行批量处理的功能函数
169
-
170
- :param files: 文件匹配规则,详见filesmatch用法
171
- :param func: 通用文本处理函数
172
- :param count: 匹配到count个文件后结束,防止满足条件的文件太多,程序会跑死
173
- :param start: 从编号几的文件开始查找,一般在遇到意外调试的时候使用
174
- :param bc: 使用beyond compare软件
175
- 注意bc的优先级比write高,如果bc和write同时为True,则会开bc,但并不会执行write
176
- :param write: 是否原地修改文件内容进行保存
177
- :param if_exists: 是否进行备份,详见writefile里的参数文件
178
- :return: 满足条件的文件清单
179
- """
180
- ls = []
181
- total = 0
182
- for f in filesmatch(files):
183
- # if 'A4-Exam' in f:
184
- # continue
185
- total += 1
186
- if total < start:
187
- continue
188
- s0 = File(f).read()
189
- s1 = func(s0)
190
- if s0 != s1:
191
- match = len(ls) + 1
192
- dprint(f, total, match)
193
- if bc:
194
- bcompare(f, s1)
195
- elif write: # 如果开了bc,程序是绝对不会自动写入的
196
- File(f).write(s1, if_exists=if_exists)
197
- ls.append(f)
198
- if len(ls) == count:
199
- break
200
-
201
- match_num = len(ls)
202
- dprint(total, match_num)
203
- return ls
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ # @Author : 陈坤泽
4
+ # @Email : 877362867@qq.com
5
+ # @Date : 2020/06/01 18:13
6
+
7
+ import copy
8
+
9
+ from pyxllib.prog.pupil import dprint, prettifystr
10
+ from pyxllib.prog.specialist.browser import Explorer
11
+ from pyxllib.algo.pupil import intersection_split
12
+ from pyxllib.file.specialist import File, Dir, filesmatch, get_encoding, XlPath
13
+
14
+
15
+ # 需要使用的第三方软件
16
+ # BCompare.exe, bcompare函数要用
17
+
18
+ class BCompare(Explorer):
19
+ def __init__(self, app='bcomp', shell=False):
20
+ """
21
+ 240512周日20:06,本来写的是BCompare,但是友鑫mac电脑上发现似乎有问题,所以改成bcomp,这种在windows上也能用
22
+ """
23
+ super().__init__(app, shell)
24
+
25
+ @classmethod
26
+ def to_bcompare_files(cls, *args, files=None):
27
+ """ 这个需要一次性获得所有的数据,才适合分析整体上要怎么获取对应的多个文件
28
+
29
+ :param files: 每个arg对应的文件名,默认按 'left'、'right', 'base' 来生成
30
+ 也可以输入一个list[str],表示多个args依次对应的文件名
31
+ filename的长度可以跟args不一致,多的不用,少的自动生成
32
+ """
33
+ # 1 如果oldfile和newfile都是dict、set、list、tuple,则使用特殊方法文本化
34
+ # 如果两个都是list,不应该提取key后比较,所以限制第1个类型必须是dict或set,然后第二个类型可以适当放宽条件
35
+ # if not oldfile: oldfile = str(oldfile)
36
+ if len(args) > 1 and isinstance(args[0], (dict, set)) and isinstance(args[1], (dict, set, list, tuple)):
37
+ args = copy.copy(list(args))
38
+ t = [prettifystr(li) for li in intersection_split(args[0], args[1])]
39
+ args[0] = f'【共有部分】,{t[0]}\n\n【独有部分】,{t[1]}'
40
+ args[1] = f'【共有部分】,{t[2]}\n\n【独有部分】,{t[3]}'
41
+
42
+ # 2 参数对齐
43
+ if not isinstance(files, (list, tuple)):
44
+ files = [files]
45
+ if len(files) < len(args):
46
+ files += [None] * (len(args) - len(files))
47
+ ref_names = ['left', 'right', 'base']
48
+
49
+ # 3 将每个参数转成一个文件
50
+ new_args = []
51
+ default_suffix = None
52
+ for i, arg in enumerate(args):
53
+ f = XlPath.safe_init(arg)
54
+ if f is not None and f.is_file(): # 是文件对象,且存在
55
+ new_args.append(f)
56
+ if not default_suffix:
57
+ default_suffix = f.suffix
58
+ # elif isinstance(f, File): # 文本内容也可能生成合法的伪路径,既然找不到还是统一按字符串对比差异好
59
+ # # 是文件对象,但不存在 -> 报错
60
+ # raise FileNotFoundError(f'{f}')
61
+ else: # 不是文件对象,要转存到文件
62
+ if not files[i]: # 没有设置文件名则生成一个
63
+ files[i] = XlPath.init(ref_names[i], XlPath.tempdir(), suffix=default_suffix)
64
+ else:
65
+ files[i] = XlPath(files[i])
66
+ files[i].write_text(str(arg))
67
+ new_args.append(files[i])
68
+
69
+ return new_args
70
+
71
+ def __call__(self, *args, wait=True, files=None, sameoff=False, **kwargs):
72
+ r"""
73
+ :param wait:
74
+ 一般调用bcompare的时候,默认值wait=True,python是打开一个子进程并等待bcompare软件关闭后,才继续执行后续代码。
75
+ 如果你的业务场景并不需要等待bcompare关闭后执行后续python代码,可以设为wait=False,不等待。
76
+ :param sameoff: 如果设为True,则会判断内容,两份内容如果相同则不打开bc
77
+ 这个参数一定程度会影响性能,非必要的时候不要开。
78
+ 或者在外部的时候,更清楚数据情况,可以在外部判断内容不重复再跑bcompare函数
79
+ 这个目前的实现策略,是读取文件重新判断的,会有性能开销,默认关闭该功能
80
+ :return: 程序返回被修改的oldfile内容
81
+ 注意如果没有修改,或者wait=False,会返回原始值
82
+ """
83
+ files = self.to_bcompare_files(*args, files=files)
84
+
85
+ if sameoff and len(files) > 1:
86
+ if files[0].read_auto() == files[1].read_auto():
87
+ return
88
+ super().__call__(*([str(f) for f in files]), wait=wait, **kwargs)
89
+ # bc软件操作中可能会修改原文内容,所以这里需要重新读取,不能用前面算过的结果
90
+ return XlPath(files[0]).read_auto()
91
+
92
+
93
+ bcompare = BCompare() # nowatch: 调试阶段,不需要自动watch的变量
94
+
95
+
96
+ def modify_file(file, func, *, outfile=None, file_mode=None, debug=0):
97
+ """ 对单个文件就行优化的功能函数
98
+
99
+ 这样一些底层函数功能可以写成数据级的接口,然后由这个函数负责文件的读写操作,并且支持debug比较前后内容差异
100
+
101
+ :param outfile: 默认是对file原地操作,如果使用该参数,则将处理后的内容写入outfile文件
102
+ :param file_mode: 指定文件读取类型格式,例如'.json'是json文件,读取为字典
103
+ :param debug: 这个功能可以对照refine分级轮理解
104
+ 无outfile参数时,原地操作
105
+ 0,【直接原地操作】关闭调试,直接运行 (根据outfile=None选择原地操作,或者指定生成新文件)
106
+ 1,【进行审核】打开BC比较差异,左边原始内容,右边参考内容 (打开前后差异内容对比)
107
+ -1,【先斩后奏】介于完全不调试和全部人工检查之间,特殊的差异比较。左边放原始文件修改后的结果,右边对照原内容。
108
+ 有outfile参数时
109
+ 0 | False,直接生成目标文件,如果outfile已存在会被覆盖
110
+ 1 | True,直接生成目标文件,但是会弹出bc比较前后内容差异 (相同内容不会弹出)
111
+ """
112
+ infile = File(file)
113
+ enc = get_encoding(infile.read(mode='b'))
114
+ data = infile.read(mode=file_mode, encoding=enc)
115
+ origin_content = str(data)
116
+ new_data = func(data)
117
+
118
+ isdiff = origin_content != str(new_data)
119
+ if outfile is None: # 原地操作
120
+ if isdiff: # 内容不同才会有相关debug功能,否则静默跳过就好
121
+ if debug == 0:
122
+ infile.write(new_data, mode=file_mode) # 直接处理
123
+ elif debug == 1:
124
+ temp_file = File('refine_content', Dir.TEMP, suffix=infile.suffix).write(new_data)
125
+ bcompare(infile, temp_file) # 使用beyond compare软件打开对比查看
126
+ elif debug == -1:
127
+ temp_file = File('origin_content', Dir.TEMP, suffix=infile.suffix)
128
+ infile.copy(temp_file)
129
+ infile.write(new_data, mode=file_mode, encoding=enc) # 把原文件内容替换了
130
+ bcompare(infile, temp_file) # 然后显示与旧内容进行对比
131
+ else:
132
+ raise ValueError(f'{debug}')
133
+ else:
134
+ outfile = File(outfile)
135
+ outfile.write(new_data, mode=file_mode, encoding=enc) # 直接处理
136
+ if debug and isdiff:
137
+ bcompare(infile, outfile)
138
+
139
+ return isdiff
140
+
141
+
142
+ class PairContent:
143
+ """ 配对文本类,主要用于bc差异比较 """
144
+
145
+ def __init__(self, left_file_name=None, right_file_name=None):
146
+ self.left_file = File(left_file_name) if left_file_name else left_file_name
147
+ self.right_file = File(right_file_name) if right_file_name else right_file_name
148
+ self.left, self.right = [], []
149
+
150
+ def add(self, lt, rt=None):
151
+ """ rt不加,默认本轮内容是同lt """
152
+ lt = str(lt)
153
+ self.left.append(lt)
154
+ rt = lt if rt is None else str(rt)
155
+ self.right.append(rt)
156
+
157
+ def bcompare(self, **kwargs):
158
+ left, right = '\n'.join(self.left), '\n'.join(self.right)
159
+ if self.left_file is not None:
160
+ left = self.left_file.write(left)
161
+ if self.right_file is not None:
162
+ right = self.right_file.write(right)
163
+ bcompare(left, right, **kwargs)
164
+
165
+
166
+ def filetext_replace(files, func, *,
167
+ count=-1, start=1, bc=False, write=False, if_exists=None):
168
+ r"""遍历目录下的文本文件进行批量处理的功能函数
169
+
170
+ :param files: 文件匹配规则,详见filesmatch用法
171
+ :param func: 通用文本处理函数
172
+ :param count: 匹配到count个文件后结束,防止满足条件的文件太多,程序会跑死
173
+ :param start: 从编号几的文件开始查找,一般在遇到意外调试的时候使用
174
+ :param bc: 使用beyond compare软件
175
+ 注意bc的优先级比write高,如果bc和write同时为True,则会开bc,但并不会执行write
176
+ :param write: 是否原地修改文件内容进行保存
177
+ :param if_exists: 是否进行备份,详见writefile里的参数文件
178
+ :return: 满足条件的文件清单
179
+ """
180
+ ls = []
181
+ total = 0
182
+ for f in filesmatch(files):
183
+ # if 'A4-Exam' in f:
184
+ # continue
185
+ total += 1
186
+ if total < start:
187
+ continue
188
+ s0 = File(f).read()
189
+ s1 = func(s0)
190
+ if s0 != s1:
191
+ match = len(ls) + 1
192
+ dprint(f, total, match)
193
+ if bc:
194
+ bcompare(f, s1)
195
+ elif write: # 如果开了bc,程序是绝对不会自动写入的
196
+ File(f).write(s1, if_exists=if_exists)
197
+ ls.append(f)
198
+ if len(ls) == count:
199
+ break
200
+
201
+ match_num = len(ls)
202
+ dprint(total, match_num)
203
+ return ls