morepath_mount.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import morepath
  2. from os import environ
  3. from platform import python_version
  4. from posixpath import join
  5. from finitelycomputable_microsites_setup import version
  6. base_path = join('/', environ.get('BASE_PATH', ''))
  7. version_text = environ.get('MICROSITES_VERSION_TEXT', '')
  8. included_apps = []
  9. class CoreApp(morepath.App):
  10. pass
  11. @CoreApp.path(path=base_path)
  12. class Root(object):
  13. pass
  14. @CoreApp.view(model=Root, name='wsgi_info')
  15. def wsgi_info(self, request):
  16. return (
  17. f'''{version_text} using {__name__} {version} on Python {python_version()}
  18. at {base_path} with {', '.join(included_apps) or "nothing"}\n'''
  19. )
  20. try:
  21. from finitelycomputable.helloworld_morepath import HelloWorldApp
  22. @CoreApp.mount(path=join(base_path, 'hello_world'), app=HelloWorldApp)
  23. def mount_hello_world():
  24. return HelloWorldApp()
  25. included_apps.append('helloworld_morepath')
  26. except ModuleNotFoundError:
  27. pass
  28. application = CoreApp()
  29. def run():
  30. from sys import argv, exit, stderr
  31. usage = f'usage: {argv[0]} run|routes [port]\n'
  32. if len(argv) < 2:
  33. stderr.write(usage)
  34. exit(1)
  35. if argv[1] == 'run':
  36. try:
  37. port=int(argv[2])
  38. except IndexError:
  39. port=8080
  40. morepath.run(application, ignore_cli=True, port=port)
  41. elif argv[1] == 'routes':
  42. import dectate
  43. for app in application.commit():
  44. for view in dectate.query_app(app, 'view'):
  45. print(view[0].key_dict())
  46. else:
  47. stderr.write(usage)
  48. exit(1)