add.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. #
  5. import re
  6. from pathlib import Path
  7. from module.logger import logger
  8. # 震惊到我姥姥家 除了第一个函数all_script_files是我自己写的
  9. # 后面的都是github copilot写的
  10. class Add:
  11. @classmethod
  12. def all_script_files(cls) -> list:
  13. """
  14. 获取所有的脚本文件 除了tmplate
  15. :return: ['oas1', 'oas2']
  16. """
  17. # 获取某个路径的所有json文件名
  18. config_path = Path.cwd() / 'config'
  19. json_files = config_path.glob('*.json')
  20. result = []
  21. for json in json_files:
  22. if json.stem == 'template':
  23. continue
  24. result.append(json.stem)
  25. return result
  26. @classmethod
  27. def all_json_file(cls) -> list:
  28. """
  29. 获取所有的json文件
  30. :return: ['oas1', 'oas2']
  31. """
  32. # 获取某个路径的所有json文件名
  33. config_path = Path.cwd() / 'config'
  34. json_files = config_path.glob('*.json')
  35. result = []
  36. for json in json_files:
  37. result.append(json.stem)
  38. return result
  39. @classmethod
  40. def copy(cls, 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') as f:
  54. template_content = f.read()
  55. with open(file_path, 'w') as f:
  56. f.write(template_content)
  57. logger.info(f'copy {template_path} to {file_path}')
  58. @classmethod
  59. def generate_script_name(cls) -> str:
  60. """
  61. 生成一个新的配置的名字
  62. :return:
  63. """
  64. all_script_files = cls.all_script_files()
  65. if not all_script_files:
  66. return 'oas1'
  67. all_script_files.sort()
  68. last_script = all_script_files[-1]
  69. last_script_number = re.findall(r'\d+', last_script)[0]
  70. new_script_number = int(last_script_number) + 1
  71. return f'oas{new_script_number}'
  72. if __name__ == "__main__":
  73. a = Add()
  74. print(a.all_script_files())
  75. print(a.all_json_file())
  76. print(a.generate_script_name())