office_helper.py 1.8 KB

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