office_helper.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import functools
  2. import json
  3. import os.path
  4. import sys
  5. import threading
  6. import time
  7. if getattr(sys, 'frozen', False):
  8. # 打包后的 exe
  9. os.chdir(os.path.dirname(sys.executable))
  10. else:
  11. # 普通 Python 脚本
  12. os.chdir(os.path.dirname(os.path.abspath(__file__)))
  13. from api.api_app import start_flask
  14. from modify_folder.monitor_file import start_watchdog
  15. from tools.logger_handle import logger
  16. from tools.serve_client import ServerClient
  17. from config import *
  18. def ApplicationInstance(func):
  19. @functools.wraps(func)
  20. def fun(*args, **kwargs):
  21. import socket
  22. try:
  23. global s
  24. s = socket.socket()
  25. host = socket.gethostname()
  26. s.bind((host, 60123))
  27. except:
  28. logger.error('不能重复打开已存在的程序')
  29. return None
  30. return func(*args, **kwargs)
  31. return fun
  32. # 启动所有后台服务
  33. @ApplicationInstance
  34. def start_all_services(serve_client, work_path):
  35. # 启动 Flask 服务
  36. threading.Thread(target=start_flask, args=[serve_client, work_path], daemon=True).start()
  37. # 启动文件监控
  38. threading.Thread(target=start_watchdog, args=[serve_client, work_path], daemon=True).start()
  39. logger.info('server running')
  40. while True:
  41. time.sleep(10)
  42. a = 0
  43. if __name__ == "__main__":
  44. if not os.path.exists(f'{dl_code}:\\ProgramData\\OfficeAssistant\\config.json'):
  45. logger.error('"config.json" is not find')
  46. sys.exit(0)
  47. with open(f'{dl_code}:\\ProgramData\\OfficeAssistant\\config.json', 'r', encoding='utf-8') as f:
  48. # with open(f'config.json', 'r', encoding='utf-8') as f:
  49. args = json.load(f)
  50. args['worker_path'] = args['worker_path'].replace('\\\\', '\\')
  51. if not (args.get('server') and args.get('username') and args.get('password')):
  52. logger.error('The config is missing critical information')
  53. sys.exit(0)
  54. client = ServerClient(args['server'], args['username'], args['password'])
  55. if not os.path.exists(os.path.join(args['worker_path'], args['username'])):
  56. os.makedirs(os.path.join(args['worker_path'], args['username']))
  57. start_all_services(client, os.path.join(args['worker_path'], args['username']))