add.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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') as f:
  60. template_content = f.read()
  61. with open(file_path, 'w') 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. all_script_files.sort()
  74. last_script = all_script_files[-1]
  75. last_script_number = re.findall(r'\d+', last_script)[0]
  76. new_script_number = int(last_script_number) + 1
  77. return f'oas{new_script_number}'
  78. if __name__ == "__main__":
  79. a = Add()
  80. print(a.all_script_files())
  81. print(a.all_json_file())
  82. print(a.generate_script_name())