| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- # 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)
- 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}'
|