PageRenderTime 83ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/htpc/root.py

https://github.com/grimmjow8/HTPC-Manager
Python | 52 lines | 35 code | 7 blank | 10 comment | 2 complexity | 04ca63a5f3803a497a15e80e1142a61a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. Root for webserver. Specifies frontpage, errorpage (default),
  3. and pages for restarting and shutting down server.
  4. """
  5. import os
  6. import sys
  7. import cherrypy
  8. import htpc
  9. import logging
  10. from threading import Thread
  11. def do_restart():
  12. arguments = sys.argv[:]
  13. arguments.insert(0, sys.executable)
  14. if sys.platform == 'win32':
  15. arguments = ['"%s"' % arg for arg in arguments]
  16. os.chdir(os.getcwd())
  17. cherrypy.engine.exit()
  18. os.execv(sys.executable, arguments)
  19. class Root:
  20. """ Root class """
  21. def __init__(self):
  22. """ Do nothing on load """
  23. self.logger = logging.getLogger('htpc.root')
  24. pass
  25. @cherrypy.expose()
  26. def index(self):
  27. """ Load template for frontpage """
  28. return htpc.LOOKUP.get_template('dash.html').render(scriptname='dash')
  29. @cherrypy.expose()
  30. def default(self, *args, **kwargs):
  31. """ Show error if no matching page can be found """
  32. return "An error occured"
  33. @cherrypy.expose()
  34. def shutdown(self):
  35. """ Shutdown CherryPy and exit script """
  36. self.logger.info("Shutting down htpc-manager.")
  37. cherrypy.engine.exit()
  38. return "HTPC Manager has shut down"
  39. @cherrypy.tools.json_out()
  40. @cherrypy.expose()
  41. def restart(self):
  42. """ Shutdown script and rerun with the same variables """
  43. self.logger.info("Restarting htpc-manager.")
  44. Thread(target=do_restart).start()
  45. return "Restart in progress."