add.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 PySide6.QtCore import QObject, Slot, Signal
  8. from module.logger import logger
  9. # 震惊到我姥姥家 除了第一个函数all_script_files是我自己写的
  10. # 后面的都是github copilot写的
  11. class Add(QObject):
  12. def __init__(self) -> None:
  13. super(Add, self).__init__()
  14. @Slot(result="QVariantList")
  15. def all_script_files(self) -> list:
  16. """
  17. 获取所有的脚本文件 除了tmplate
  18. :return: ['oas1', 'oas2']
  19. """
  20. # 获取某个路径的所有json文件名
  21. config_path = Path.cwd() / 'config'
  22. json_files = config_path.glob('*.json')
  23. result = []
  24. for json in json_files:
  25. if json.stem == 'template':
  26. continue
  27. result.append(json.stem)
  28. return result
  29. @Slot(result="QVariantList")
  30. def all_json_file(self) -> list:
  31. """
  32. 获取所有的json文件
  33. :return: ['oas1', 'oas2']
  34. """
  35. # 获取某个路径的所有json文件名
  36. config_path = Path.cwd() / 'config'
  37. json_files = config_path.glob('*.json')
  38. result = []
  39. for json in json_files:
  40. if json.stem == 'template':
  41. result.insert(0, json.stem)
  42. else:
  43. result.append(json.stem)
  44. return result
  45. @Slot(str, str)
  46. def copy(self, file: str, template: str = 'template') -> None:
  47. """
  48. 复制一个配置文件
  49. :param file: 不带json后缀
  50. :param template:
  51. :return:
  52. """
  53. config_path = Path.cwd() / 'config'
  54. template_path = config_path / f'{template}.json'
  55. file_path = config_path / f'{file}.json'
  56. if file_path.exists():
  57. logger.error(f'{file_path} is exists')
  58. return
  59. with open(template_path, 'r', encoding='utf-8') as f:
  60. template_content = f.read()
  61. with open(file_path, 'w', encoding='utf-8') as f:
  62. f.write(template_content)
  63. logger.info(f'copy {template_path} to {file_path}')
  64. @Slot(result="QString")
  65. def generate_script_name(self) -> str:
  66. """
  67. 生成一个新的配置的名字
  68. :return:
  69. """
  70. all_script_files = self.all_script_files()
  71. if not all_script_files:
  72. return 'oas1'
  73. script_numbers = []
  74. for script_file in all_script_files:
  75. match = re.search(r'\d+', script_file)
  76. if match:
  77. script_number = int(match.group())
  78. script_numbers.append(script_number)
  79. if not script_numbers:
  80. return 'oas1'
  81. script_numbers.sort()
  82. new_script_number = script_numbers[-1] + 1
  83. return f'oas{new_script_number}'
  84. if __name__ == "__main__":
  85. a = Add()
  86. print(a.all_script_files())
  87. print(a.all_json_file())
  88. print(a.generate_script_name())