upplib 3.1.7__py3-none-any.whl → 3.3.1__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.
- upplib/__init__.py +3 -0
- upplib/config_data.py +71 -0
- upplib/file.py +70 -49
- upplib/file_text.py +977 -0
- upplib/index.py +0 -1031
- upplib/util.py +2 -3
- {upplib-3.1.7.dist-info → upplib-3.3.1.dist-info}/METADATA +1 -1
- {upplib-3.1.7.dist-info → upplib-3.3.1.dist-info}/RECORD +11 -9
- {upplib-3.1.7.dist-info → upplib-3.3.1.dist-info}/WHEEL +0 -0
- {upplib-3.1.7.dist-info → upplib-3.3.1.dist-info}/licenses/LICENSE +0 -0
- {upplib-3.1.7.dist-info → upplib-3.3.1.dist-info}/top_level.txt +0 -0
upplib/__init__.py
CHANGED
upplib/config_data.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
from upplib import *
|
|
2
|
+
from upplib.common_package import *
|
|
3
|
+
from upplib.file import *
|
|
4
|
+
|
|
5
|
+
__CONFIG_PATH = '.upp.config'
|
|
6
|
+
|
|
7
|
+
__CONFIG_PATH_BAK = '_upp.config'
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# 将数据写入到 config 中
|
|
11
|
+
def set_config_data(file_name: str = 'config',
|
|
12
|
+
param: dict[str, Any] = None) -> None:
|
|
13
|
+
set_data_in_user_home(file_name, {} if param is None else param)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# 从 config 中获得 配置数据
|
|
17
|
+
def get_config_data(file_name: str = 'config') -> dict[str, Any]:
|
|
18
|
+
# print('get_config_data', file_name)
|
|
19
|
+
config_data = get_data_from_user_home(file_name)
|
|
20
|
+
# print('get_data_from_user_home', config_data)
|
|
21
|
+
if not config_data:
|
|
22
|
+
config_data = get_data_from_path(file_name)
|
|
23
|
+
# print('get_data_from_path', config_data)
|
|
24
|
+
return config_data
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# 在当前用户的主目录中, 获得指定文件的数据
|
|
28
|
+
def get_data_from_user_home(file_name: str = 'config') -> dict[str, Any]:
|
|
29
|
+
return get_data_from_path(file_name, os.path.expanduser("~"))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# 将 data 数据,在当前用户的主目录中, 获得指定文件的数据
|
|
33
|
+
def set_data_in_user_home(file_name: str = 'config',
|
|
34
|
+
param: dict[str, Any] = None) -> None:
|
|
35
|
+
set_data_in_path(file_name, {} if param is None else param, os.path.expanduser("~"))
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# 在当前的目录中, 获得指定文件的数据
|
|
39
|
+
def get_data_from_path(file_name: str = 'config',
|
|
40
|
+
file_path: str = None) -> dict[str, Any]:
|
|
41
|
+
param = get_data_from_path_detail(file_name, file_path, __CONFIG_PATH)
|
|
42
|
+
return param if param else get_data_from_path_detail(file_name, file_path, __CONFIG_PATH_BAK)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def get_data_from_path_detail(file_name: str = 'config',
|
|
46
|
+
file_path: str = None,
|
|
47
|
+
path_name: str = __CONFIG_PATH) -> dict[str, Any]:
|
|
48
|
+
config_path = file_path + '/' + path_name if file_path else path_name
|
|
49
|
+
# print('config_path_1', config_path)
|
|
50
|
+
if not os.path.exists(config_path):
|
|
51
|
+
# print('config_path_2', config_path)
|
|
52
|
+
return {}
|
|
53
|
+
file_path = config_path + '/' + file_name + '.json'
|
|
54
|
+
# print('config_path_3', file_path)
|
|
55
|
+
if not os.path.exists(file_path):
|
|
56
|
+
return {}
|
|
57
|
+
# print('to_json_from_file', file_path)
|
|
58
|
+
return to_json_from_file(file_path)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# 在当前的目录中, 设置数据到指定路径下
|
|
62
|
+
def set_data_in_path(file_name: str = 'config',
|
|
63
|
+
param: str = None,
|
|
64
|
+
file_path: str = '') -> None:
|
|
65
|
+
config_path = file_path + '/' + __CONFIG_PATH
|
|
66
|
+
if not os.path.exists(config_path):
|
|
67
|
+
os.mkdir(config_path)
|
|
68
|
+
file_path = config_path + '/' + file_name + '.json'
|
|
69
|
+
text_file = open(file_path, 'w', encoding='utf-8')
|
|
70
|
+
text_file.write(to_str({} if param is None else param))
|
|
71
|
+
text_file.close()
|
upplib/file.py
CHANGED
|
@@ -1,32 +1,33 @@
|
|
|
1
1
|
from upplib import *
|
|
2
2
|
from upplib.index import *
|
|
3
|
+
from upplib.index import is_win
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
def get_file(file_path: str = None,
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
path_startswith: str = None,
|
|
8
|
+
startswith: str = None,
|
|
8
9
|
path_contain: str = None,
|
|
9
10
|
contain: str = None,
|
|
10
|
-
sort_asc: bool | None =
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
sort_asc: bool | None = True,
|
|
12
|
+
path_endswith: str = None,
|
|
13
|
+
endswith: str = None) -> list[str]:
|
|
13
14
|
"""
|
|
14
15
|
有关文件的操作
|
|
15
16
|
查询指定文件夹下面的所有的文件信息, 也可以是指定的文件
|
|
16
|
-
file_path
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
path_contain
|
|
20
|
-
contain
|
|
21
|
-
sort_asc
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
file_path : 文件路径
|
|
18
|
+
path_startswith : 文件路径,以 path_startswith 开头
|
|
19
|
+
startswith : 文件名称,以 startswith 开头
|
|
20
|
+
path_contain : 文件路径,含有
|
|
21
|
+
contain : 文件名称,含有
|
|
22
|
+
sort_asc : 是否按升序排序,也就是从小到大排序
|
|
23
|
+
path_endswith : 文件路径,以 path_endswith 结尾
|
|
24
|
+
endswith : 文件名称,以 endswith 结尾
|
|
24
25
|
return list
|
|
25
26
|
"""
|
|
26
27
|
if file_path is None:
|
|
27
28
|
file_path = os.path.dirname(os.path.abspath('.'))
|
|
28
29
|
list_data = []
|
|
29
|
-
get_file_all(file_path, list_data,
|
|
30
|
+
get_file_all(file_path, list_data, path_startswith, startswith, path_contain, contain, path_endswith, endswith)
|
|
30
31
|
# 去一下重复的数据
|
|
31
32
|
r_list = list(set(list_data))
|
|
32
33
|
if sort_asc is not None:
|
|
@@ -49,10 +50,31 @@ def get_file_folder(file_name_one: str = None) -> str:
|
|
|
49
50
|
return all_file_path
|
|
50
51
|
|
|
51
52
|
|
|
53
|
+
def remove_folder_file(file_path: str = None,
|
|
54
|
+
path_startswith: str = None,
|
|
55
|
+
startswith: str = None,
|
|
56
|
+
path_contain: str = None,
|
|
57
|
+
contain: str = None,
|
|
58
|
+
path_endswith: str = None,
|
|
59
|
+
endswith: str = None):
|
|
60
|
+
"""
|
|
61
|
+
删除指定文件夹下面的指定的文件
|
|
62
|
+
"""
|
|
63
|
+
file_part_all_list = get_file(file_path=file_path,
|
|
64
|
+
path_startswith=path_startswith,
|
|
65
|
+
startswith=startswith,
|
|
66
|
+
path_contain=path_contain,
|
|
67
|
+
contain=contain,
|
|
68
|
+
path_endswith=path_endswith,
|
|
69
|
+
endswith=endswith)
|
|
70
|
+
for file_part_all_one in file_part_all_list:
|
|
71
|
+
os.remove(file_part_all_one)
|
|
72
|
+
|
|
73
|
+
|
|
52
74
|
def get_folder(file_path: str = None,
|
|
53
|
-
|
|
75
|
+
startswith: str = None,
|
|
54
76
|
contain: str = None,
|
|
55
|
-
|
|
77
|
+
endswith: str = None) -> list[str]:
|
|
56
78
|
"""
|
|
57
79
|
有关文件的操作, 只查询文件夹
|
|
58
80
|
查询指定文件夹下面的所有的文件信息, 也可以是指定的文件
|
|
@@ -62,17 +84,17 @@ def get_folder(file_path: str = None,
|
|
|
62
84
|
if file_path is None:
|
|
63
85
|
file_path = os.path.dirname(os.path.abspath('.'))
|
|
64
86
|
list_data = []
|
|
65
|
-
get_folder_all(file_path, list_data,
|
|
87
|
+
get_folder_all(file_path, list_data, startswith, contain, endswith)
|
|
66
88
|
# 去一下重复的数据
|
|
67
89
|
return list(set(list_data))
|
|
68
90
|
|
|
69
91
|
|
|
70
92
|
# 是否包含指定的文件
|
|
71
93
|
def contain_file(file_path: str = None,
|
|
72
|
-
|
|
94
|
+
startswith: str = None,
|
|
73
95
|
contain: str = None,
|
|
74
|
-
|
|
75
|
-
return len(get_file(file_path,
|
|
96
|
+
endswith: str = None) -> bool:
|
|
97
|
+
return len(get_file(file_path, startswith, contain, endswith)) > 0
|
|
76
98
|
|
|
77
99
|
|
|
78
100
|
def get_file_data_line(file_path: str = None,
|
|
@@ -104,78 +126,77 @@ def get_file_data_line(file_path: str = None,
|
|
|
104
126
|
# 查询指定文件夹下面的所有的文件信息, 也可以是指定的文件
|
|
105
127
|
def get_file_all(file_path: str = None,
|
|
106
128
|
list_data: list = None,
|
|
107
|
-
|
|
108
|
-
|
|
129
|
+
path_startswith: str = None,
|
|
130
|
+
startswith: str = None,
|
|
109
131
|
path_contain: str = None,
|
|
110
132
|
contain: str = None,
|
|
111
|
-
|
|
112
|
-
|
|
133
|
+
path_endswith: str = None,
|
|
134
|
+
endswith: str = None) -> None:
|
|
113
135
|
if os.path.isdir(file_path):
|
|
114
136
|
for root, dir_names, file_names in os.walk(file_path):
|
|
115
137
|
for file_name in file_names:
|
|
116
|
-
if (get_file_check(os.path.join(root, file_name),
|
|
117
|
-
and get_file_check(file_name,
|
|
138
|
+
if (get_file_check(os.path.join(root, file_name), path_startswith, path_contain, path_endswith)
|
|
139
|
+
and get_file_check(file_name, startswith, contain, endswith)):
|
|
118
140
|
list_data.append(os.path.join(root, file_name))
|
|
119
141
|
for dir_name in dir_names:
|
|
120
|
-
get_file_all(os.path.join(root, dir_name), list_data,
|
|
121
|
-
elif (get_file_check(file_path,
|
|
122
|
-
and get_file_check(file_path,
|
|
142
|
+
get_file_all(os.path.join(root, dir_name), list_data, path_startswith, startswith, path_contain, contain, path_endswith, endswith)
|
|
143
|
+
elif (get_file_check(file_path, startswith, contain, endswith)
|
|
144
|
+
and get_file_check(file_path, path_startswith, path_contain, path_endswith)):
|
|
123
145
|
list_data.append(file_path)
|
|
124
146
|
|
|
125
147
|
|
|
126
148
|
# 查询指定文件夹下面的所有的文件信息, 也可以是指定的文件
|
|
127
149
|
def get_folder_all(file_path: str = None,
|
|
128
150
|
list_data: list = None,
|
|
129
|
-
|
|
151
|
+
startswith: str = None,
|
|
130
152
|
contain: str = None,
|
|
131
|
-
|
|
153
|
+
endswith: str = None) -> None:
|
|
132
154
|
if os.path.isdir(file_path):
|
|
133
155
|
for root, dir_names, file_names in os.walk(file_path):
|
|
134
156
|
for dir_name in dir_names:
|
|
135
157
|
dir_name_path = os.path.join(root, dir_name)
|
|
136
|
-
if get_file_check(dir_name_path,
|
|
158
|
+
if get_file_check(dir_name_path, startswith, contain, endswith):
|
|
137
159
|
list_data.append(dir_name_path)
|
|
138
160
|
else:
|
|
139
|
-
get_folder_all(dir_name_path, list_data,
|
|
161
|
+
get_folder_all(dir_name_path, list_data, startswith, contain, endswith)
|
|
140
162
|
|
|
141
163
|
|
|
142
|
-
def get_file_check(
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
suffix: str = None) -> bool:
|
|
164
|
+
def get_file_check(name: str = None,
|
|
165
|
+
startswith: str = None,
|
|
166
|
+
contain: str = None,
|
|
167
|
+
endswith: str = None) -> bool:
|
|
147
168
|
"""
|
|
148
169
|
检查文件是否符合要求
|
|
149
|
-
|
|
150
|
-
contain
|
|
151
|
-
|
|
170
|
+
startswith : 以这个开头
|
|
171
|
+
contain : 包含这个字符
|
|
172
|
+
endswith : 以这个结尾
|
|
152
173
|
"""
|
|
153
174
|
if name is None or name == '':
|
|
154
175
|
return False
|
|
155
176
|
p = True
|
|
156
177
|
c = True
|
|
157
178
|
s = True
|
|
158
|
-
if
|
|
159
|
-
p = name.startswith(
|
|
179
|
+
if startswith is not None:
|
|
180
|
+
p = name.startswith(startswith)
|
|
160
181
|
if contain is not None:
|
|
161
182
|
c = name.find(contain) > -1
|
|
162
|
-
if
|
|
163
|
-
s = name.endswith(
|
|
183
|
+
if endswith is not None:
|
|
184
|
+
s = name.endswith(endswith)
|
|
164
185
|
return p and c and s
|
|
165
186
|
|
|
166
187
|
|
|
167
188
|
def find_file_by_content(file_path: str = '',
|
|
168
189
|
contain_txt: str = None,
|
|
169
|
-
|
|
190
|
+
startswith: str = None,
|
|
170
191
|
contain: str = None,
|
|
171
|
-
|
|
192
|
+
endswith: str = None) -> bool | None:
|
|
172
193
|
"""
|
|
173
194
|
检查文件内容是否包含指定的字符串
|
|
174
195
|
慎用,否则, 执行时间可能比较长
|
|
175
196
|
"""
|
|
176
|
-
list_file = get_file(file_path,
|
|
197
|
+
list_file = get_file(file_path, startswith, contain, endswith)
|
|
177
198
|
if len(list_file) == 0:
|
|
178
|
-
to_log(f'no_matched_file : {file_path} , {contain_txt} , {
|
|
199
|
+
to_log(f'no_matched_file : {file_path} , {contain_txt} , {startswith} , {contain} , {endswith}')
|
|
179
200
|
return False
|
|
180
201
|
if contain_txt is None:
|
|
181
202
|
to_log(list_file)
|