utils.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. from dataclasses import dataclass, asdict
  3. from panoramisk import Message
  4. TRUEs = ('true', '1', 'y', 'yes')
  5. NONEs = (None,'none','')
  6. NO_AUTH_ROUTES = ('/ui','/openapi.json','/favicon.ico')
  7. SWAGGER_JS_URL = "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.37.2/swagger-ui-bundle.js"
  8. SWAGGER_CSS_URL = "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.37.2/swagger-ui.min.css"
  9. SWAGGER_TEMPLATE = '''
  10. <head>
  11. <link type="text/css" rel="stylesheet" href="{{ css_url }}">
  12. <title>{{ title }}</title>
  13. </head>
  14. <body>
  15. <div id="swagger-ui"></div>
  16. <script src="{{ js_url }}"></script>
  17. <script>
  18. const ui = SwaggerUIBundle({
  19. deepLinking: true,
  20. dom_id: "#swagger-ui",
  21. layout: "BaseLayout",
  22. presets: [
  23. SwaggerUIBundle.presets.apis,
  24. SwaggerUIBundle.SwaggerUIStandalonePreset
  25. ],
  26. showExtensions: true,
  27. showCommonExtensions: true,
  28. url: "/openapi.json"
  29. });
  30. </script>
  31. </body>'''
  32. def followMe2DevState(followMeState):
  33. if followMeState == 'DIRECT':
  34. return 'INUSE'
  35. if followMeState == 'EXTENSION':
  36. return 'NOT_INUSE'
  37. return 'INVALID'
  38. @dataclass
  39. class QueueMember:
  40. user: str
  41. name: str = ''
  42. location: str = ''
  43. membership: str = ''
  44. stateinterface: str = ''
  45. status: str = ''
  46. def fromMessage(self, _m: Message):
  47. for key in asdict(self):
  48. if key in _m.getdict():
  49. setattr(self, key, _m[key])
  50. return self
  51. @dataclass
  52. class GlobalVars:
  53. FMDEVSTATE: str = ''
  54. QUEDEVSTATE: str = ''
  55. QUEUETOGGLE: str = ''
  56. QUEUEPAUSETOGGLE: str = ''
  57. INTERCOMCODE: str = ''
  58. CAMPONTOGGLE: str = ''
  59. DNDDEVSTATE: str = ''
  60. CFDEVSTATE: str = ''
  61. def d(self):
  62. return asdict(self)
  63. def jsonAPIReply(success, result):
  64. return {'success':success, 'result': result}
  65. def noUser(user):
  66. return jsonAPIReply(False, 'user {} does not exist'.format(user))
  67. def noDevice(device):
  68. return jsonAPIReply(False, 'device {} does not exist'.format(device))
  69. def beenBound(user, device):
  70. return jsonAPIReply(True, '{} is bound to {}'.format(user, device))
  71. def bindError(user, device):
  72. return jsonAPIReply(False, 'Failed binding {} to {}'.format(user, device))
  73. def hintError(user, device):
  74. return jsonAPIReply(False, 'Failed setting hint for {}@{}'.format(user, device))
  75. def noUserBound(device):
  76. return jsonAPIReply(False, 'no user is bound to {}'.format(device))
  77. def beenUnbound(user, device):
  78. return jsonAPIReply(True, '{} unbound from {}'.format(user, device))