-
Notifications
You must be signed in to change notification settings - Fork 471
Expand file tree
/
Copy pathconfig_manager.py
More file actions
137 lines (125 loc) · 4.23 KB
/
Copy pathconfig_manager.py
File metadata and controls
137 lines (125 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# This Python file uses the following encoding: utf-8
# @author runhey
# github https://github.com/runhey
import re
from pathlib import Path
from module.logger import logger
class ConfigManager:
@staticmethod
def all_script_files() -> list[str]:
"""
获取所有的脚本文件 除了tmplate
:return: ['oas1', 'oas2']
"""
# 获取某个路径的所有json文件名
config_path = Path.cwd() / 'config'
json_files = config_path.glob('*.json')
result = []
for json in json_files:
if json.stem == 'template':
continue
result.append(json.stem)
if len(result) == 0:
# 如果没有脚本文件 则创建一个
ConfigManager.copy(file='oas1', template='template')
result.append('oas1')
result.sort()
return result
@staticmethod
def all_json_file() -> list:
"""
获取所有的json文件
:return: ['oas1', 'oas2']
"""
# 获取某个路径的所有json文件名
config_path = Path.cwd() / 'config'
json_files = config_path.glob('*.json')
result = []
for json in json_files:
if json.stem == 'template':
result.insert(0, json.stem)
else:
result.append(json.stem)
return result
@staticmethod
def copy(file: str, template: str = 'template') -> None:
"""
复制一个配置文件
:param file: 不带json后缀
:param template:
:return:
"""
config_path = Path.cwd() / 'config'
template_path = config_path / f'{template}.json'
file_path = config_path / f'{file}.json'
if file_path.exists():
logger.error(f'{file_path} is exists')
return
with open(template_path, 'r', encoding='utf-8') as f:
template_content = f.read()
with open(file_path, 'w', encoding='utf-8') as f:
f.write(template_content)
logger.info(f'copy {template_path} to {file_path}')
@staticmethod
def generate_script_name() -> str:
"""
生成一个新的配置的名字
:return:
"""
all_script_files = ConfigManager.all_script_files()
if not all_script_files:
return 'oas1'
script_numbers = []
for script_file in all_script_files:
match = re.search(r'\d+', script_file)
if match:
script_number = int(match.group())
script_numbers.append(script_number)
if not script_numbers:
return 'oas1'
script_numbers.sort()
new_script_number = script_numbers[-1] + 1
return f'oas{new_script_number}'
@staticmethod
def rename(old_name: str, new_name: str) -> bool:
"""
重命名一个配置文件
:param old_name: 旧的配置文件名称
:param new_name: 新的配置文件名称
:return: True or False
"""
config_path = Path.cwd() / 'config'
old_path = config_path / f'{old_name}.json'
new_path = config_path / f'{new_name}.json'
if not old_path.exists():
logger.error(f'{old_path} is not exists')
return False
if new_path.exists():
logger.error(f'{new_path} is exists')
return False
try:
old_path.rename(new_path)
logger.info(f'rename {old_path} to {new_path}')
return True
except Exception as e:
logger.error(f'rename {old_path} to {new_path} failed: {e}')
return False
@staticmethod
def delete(file: str) -> bool:
"""
删除一个配置文件
:param file: 不带json后缀
:return: True or False
"""
config_path = Path.cwd() / 'config'
file_path = config_path / f'{file}.json'
if not file_path.exists():
logger.error(f'{file_path} is not exists')
return False
try:
file_path.unlink()
logger.info(f'delete {file_path}')
return True
except Exception as e:
logger.error(f'delete {file_path} failed: {e}')
return False