controller.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import os
  2. import shutil
  3. from system import LOG_DIR
  4. PATH = os.getcwd() + rf'{os.sep}template'
  5. class NamingError(BaseException):
  6. pass
  7. class ConflictError(BaseException):
  8. pass
  9. class Systemctl:
  10. def __init__(self):
  11. self.dir_list = []
  12. self.plugin_list = []
  13. def get_dir(self):
  14. self.dir_list = os.listdir(PATH)
  15. return self.dir_list.copy()
  16. def get_all_plugin(self):
  17. self.get_dir()
  18. self.plugin_list = []
  19. for name in self.dir_list:
  20. try:
  21. plugin = os.listdir(f'{PATH}{os.sep}{name}')
  22. self.plugin_list += [f'{name}{os.sep}{i}' for i in plugin]
  23. finally:
  24. pass
  25. return self.plugin_list.copy()
  26. def get_plugin(self, index):
  27. dir_name = self.dir_list[index]
  28. return [f'{dir_name}{os.sep}{i}' for i in os.listdir(f'{PATH}{os.sep}{dir_name}')]
  29. def add_plugin(self, index, plugin_dir):
  30. plugin_name = os.path.split(plugin_dir)[1]
  31. if plugin_name.startswith('template_') and plugin_name.endswith('.py'):
  32. dir_name = self.dir_list[index]
  33. targets_name = f'{PATH}{os.sep}{dir_name}{os.sep}{plugin_name}'
  34. if os.path.isfile(targets_name):
  35. raise ConflictError
  36. shutil.copyfile(plugin_dir, targets_name)
  37. return self.get_all_plugin()
  38. else:
  39. print(plugin_name)
  40. raise NamingError
  41. def del_plugin(self, index):
  42. targets_name = f'{PATH}{os.sep}{self.plugin_list[index]}'
  43. os.remove(targets_name)
  44. return self.get_all_plugin()
  45. def merge_plugin(self, index, plugin_dir):
  46. plugin_name = os.path.split(plugin_dir)[1]
  47. targets_name = f'{PATH}{os.sep}{self.dir_list[index]}{os.sep}{plugin_name}'
  48. name = plugin_name[9:-3]
  49. with open(targets_name, 'a+') as f:
  50. with open(plugin_dir, 'r') as g:
  51. f.write('\n' + g.read().replace('base = None', f'base = {name}'))
  52. def show_plugin(self, index):
  53. with open(f'{PATH}{os.sep}{self.plugin_list[index]}') as f:
  54. code = f.read() + '\n[END]'
  55. return code, self.plugin_list[index]
  56. def show_log(self):
  57. with open(LOG_DIR) as f:
  58. log = f.read()
  59. return log