config_manager.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. if len(result) == 0:
  23. # 如果没有脚本文件 则创建一个
  24. ConfigManager.copy(file='oas1', template='template')
  25. result.append('oas1')
  26. result.sort()
  27. return result
  28. @staticmethod
  29. def all_json_file() -> list:
  30. """
  31. 获取所有的json文件
  32. :return: ['oas1', 'oas2']
  33. """
  34. # 获取某个路径的所有json文件名
  35. config_path = Path.cwd() / 'config'
  36. json_files = config_path.glob('*.json')
  37. result = []
  38. for json in json_files:
  39. if json.stem == 'template':
  40. result.insert(0, json.stem)
  41. else:
  42. result.append(json.stem)
  43. return result
  44. @staticmethod
  45. def copy(file: str, template: str = 'template') -> None:
  46. """
  47. 复制一个配置文件
  48. :param file: 不带json后缀
  49. :param template:
  50. :return:
  51. """
  52. config_path = Path.cwd() / 'config'
  53. template_path = config_path / f'{template}.json'
  54. file_path = config_path / f'{file}.json'
  55. if file_path.exists():
  56. logger.error(f'{file_path} is exists')
  57. return
  58. with open(template_path, 'r', encoding='utf-8') as f:
  59. template_content = f.read()
  60. with open(file_path, 'w', encoding='utf-8') as f:
  61. f.write(template_content)
  62. logger.info(f'copy {template_path} to {file_path}')
  63. @staticmethod
  64. def generate_script_name() -> str:
  65. """
  66. 生成一个新的配置的名字
  67. :return:
  68. """
  69. all_script_files = ConfigManager.all_script_files()
  70. if not all_script_files:
  71. return 'oas1'
  72. script_numbers = []
  73. for script_file in all_script_files:
  74. match = re.search(r'\d+', script_file)
  75. if match:
  76. script_number = int(match.group())
  77. script_numbers.append(script_number)
  78. if not script_numbers:
  79. return 'oas1'
  80. script_numbers.sort()
  81. new_script_number = script_numbers[-1] + 1
  82. return f'oas{new_script_number}'
  83. @staticmethod
  84. def rename(old_name: str, new_name: str) -> bool:
  85. """
  86. 重命名一个配置文件
  87. :param old_name: 旧的配置文件名称
  88. :param new_name: 新的配置文件名称
  89. :return: True or False
  90. """
  91. config_path = Path.cwd() / 'config'
  92. old_path = config_path / f'{old_name}.json'
  93. new_path = config_path / f'{new_name}.json'
  94. if not old_path.exists():
  95. logger.error(f'{old_path} is not exists')
  96. return False
  97. if new_path.exists():
  98. logger.error(f'{new_path} is exists')
  99. return False
  100. try:
  101. old_path.rename(new_path)
  102. logger.info(f'rename {old_path} to {new_path}')
  103. return True
  104. except Exception as e:
  105. logger.error(f'rename {old_path} to {new_path} failed: {e}')
  106. return False
  107. @staticmethod
  108. def delete(file: str) -> bool:
  109. """
  110. 删除一个配置文件
  111. :param file: 不带json后缀
  112. :return: True or False
  113. """
  114. config_path = Path.cwd() / 'config'
  115. file_path = config_path / f'{file}.json'
  116. if not file_path.exists():
  117. logger.error(f'{file_path} is not exists')
  118. return False
  119. try:
  120. file_path.unlink()
  121. logger.info(f'delete {file_path}')
  122. return True
  123. except Exception as e:
  124. logger.error(f'delete {file_path} failed: {e}')
  125. return False