config_manager.py 4.2 KB

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