PageRenderTime 69ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/apollo/server/__init__.py

https://github.com/thecowboy/apollo
Python | 103 lines | 51 code | 17 blank | 35 comment | 5 complexity | 8333086a872f9996441fea77ac95825c MD5 | raw file
  1. #
  2. # Copyright (C) 2011 by Tony Young
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. #
  22. """
  23. Apollo server core package.
  24. """
  25. import urlparse
  26. import urllib
  27. import logging
  28. from sqlalchemy.engine import create_engine
  29. from apollo.server.models.meta import bindSession
  30. from tornado.options import define, options, parse_config_file, parse_command_line
  31. from tornado.options import Error as OptionsError
  32. def skeletonSetup(configpath="apollod.conf"):
  33. try:
  34. setupOptions()
  35. except OptionsError:
  36. # if we're using multiprocessing, stuff might happen that causes weird
  37. # double definitions
  38. #
  39. # TODO: i don't know the exact cause of this yet. investigate
  40. pass
  41. parse_config_file(configpath)
  42. parse_command_line()
  43. setupDBSession()
  44. def setupOptions():
  45. """
  46. Initialize options for use with Tornado.
  47. """
  48. define("debug", default=False, help="run in debug mode", type=bool, metavar="DEBUG")
  49. define("address", default="127.0.0.1", help="bind to the given address", metavar="ADDRESS")
  50. define("port", default=8081, help="run on the given port", type=int, metavar="PORT")
  51. define("plugins", default=[], help="plugins to load", type=list, metavar="PLUGINS")
  52. define("render_process_num", default=4, help="number of renderer processes to run", type=int, metavar="NUM")
  53. define("cron_interval", default=360, help="run cron every specified seconds", type=int, metavar="INTERVAL")
  54. define("session_expiry", default=3600, help="expire inactive sessions after specified seconds", type=int, metavar="EXPIRY")
  55. define("sql_store", default="postgresql", help="sql server store", metavar="HOST")
  56. define("sql_host", default="localhost", help="sql server host", metavar="HOST")
  57. define("sql_port", default=5432, help="sql server port", type=int, metavar="PORT")
  58. define("sql_username", default="apollo", help="sql server username", metavar="USERNAME")
  59. define("sql_password", default="apollo", help="sql server password (put in apollod.conf)", metavar="PASSWORD")
  60. define("sql_database", default="apollo", help="sql database name", metavar="DATABASE")
  61. define("amqp_host", default="localhost", help="amqp server host", metavar="HOST")
  62. define("amqp_port", default=5672, help="amqp server port", type=int, metavar="PORT")
  63. define("amqp_username", default="guest", help="amqp server username", metavar="USERNAME")
  64. define("amqp_password", default="guest", help="amqp server password (put in apollod.conf)", metavar="PASSWORD")
  65. define("amqp_vhost", default="/", help="amqp vhost", metavar="VHOST")
  66. define("logging_level", default=logging.WARN, help="logging level", type=int, metavar="LEVEL")
  67. def setupDBSession():
  68. """
  69. Set up the session for SQLAlchemy.
  70. """
  71. # netloc
  72. netloc = options.sql_host
  73. if options.sql_port:
  74. netloc += ":%d" % options.sql_port
  75. if options.sql_username:
  76. auth = urllib.unquote_plus(options.sql_username)
  77. if options.sql_password:
  78. auth += ":%s" % urllib.unquote_plus(options.sql_password)
  79. netloc = auth + "@" + netloc
  80. bindSession(create_engine(urlparse.urlunsplit((
  81. options.sql_store,
  82. netloc,
  83. options.sql_database,
  84. "",
  85. ""
  86. ))))