config_manager.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import re
  5. from pathlib import Path
  6. from module.logger import logger
  7. class ConfigManager:
  8. @staticmethod
  9. def all_script_files() -> list[str]:
  10. """
  11. 获取所有的脚本文件 除了tmplate
  12. :return: ['oas1', 'oas2']
  13. """
  14. # 获取某个路径的所有json文件名
  15. config_path = Path.cwd() / 'config'
  16. json_files = config_path.glob('*.json')
  17. result = []
  18. for json in json_files:
  19. if json.stem == 'template':
  20. continue
  21. result.append(json.stem)
  22. return result
  23. @staticmethod
  24. def all_json_file() -> list:
  25. """
  26. 获取所有的json文件
  27. :return: ['oas1', 'oas2']
  28. """
  29. # 获取某个路径的所有json文件名
  30. config_path = Path.cwd() / 'config'
  31. json_files = config_path.glob('*.json')
  32. result = []
  33. for json in json_files:
  34. if json.stem == 'template':
  35. result.insert(0, json.stem)
  36. else:
  37. result.append(json.stem)
  38. return result
  39. @staticmethod
  40. def copy(file: str, template: str = 'template') -> None:
  41. """
  42. 复制一个配置文件
  43. :param file: 不带json后缀
  44. :param template:
  45. :return:
  46. """
  47. config_path = Path.cwd() / 'config'
  48. template_path = config_path / f'{template}.json'
  49. file_path = config_path / f'{file}.json'
  50. if file_path.exists():
  51. logger.error(f'{file_path} is exists')
  52. return
  53. with open(template_path, 'r', encoding='utf-8') as f:
  54. template_content = f.read()
  55. with open(file_path, 'w', encoding='utf-8') as f:
  56. f.write(template_content)
  57. logger.info(f'copy {template_path} to {file_path}')
  58. @staticmethod
  59. def generate_script_name() -> str:
  60. """
  61. 生成一个新的配置的名字
  62. :return:
  63. """
  64. all_script_files = ConfigManager.all_script_files()
  65. if not all_script_files:
  66. return 'oas1'
  67. script_numbers = []
  68. for script_file in all_script_files:
  69. match = re.search(r'\d+', script_file)
  70. if match:
  71. script_number = int(match.group())
  72. script_numbers.append(script_number)
  73. if not script_numbers:
  74. return 'oas1'
  75. script_numbers.sort()
  76. new_script_number = script_numbers[-1] + 1
  77. return f'oas{new_script_number}'