office_helper.py 1.9 KB

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