Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bootstrap.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ##@brief Contains functions allowing to bootstrap a lodel instance
  2. #
  3. #Typically a bootstrap process consist of settings preload ,
  4. #plugins start and finally settings loading
  5. #
  6. #Assertions that are made in this file :
  7. #- cwd is an instance directory
  8. #- nothing as been done yet (no import, no context etc)
  9. #
  10. import warnings
  11. import os, os.path
  12. import lodel.buildconf
  13. from lodel.context import LodelContext
  14. ##@brief Hardcoded configuration dirnames given an instance type
  15. #
  16. #First item is for monosite the second for multisite
  17. #First item for multisite is server conf the second the lodelsites instance
  18. #conf
  19. #
  20. #@note obsolete ! We are preparing a merge of server_conf.d and
  21. #lodelsites.conf.d
  22. CONFS_DIRNAMES = [
  23. 'conf.d',
  24. ('server_conf.d', lodel.buildconf.LODELSITE_CONFDIR)]
  25. ##@brief Test if current instance is monosite or multisite
  26. #@return if monosite return True else False
  27. def _monosite_test():
  28. return os.path.isdir('./conf.d')
  29. ##@brief Initialize the context class taking care of the instance type (MONO
  30. #or MULTI site)
  31. def _context_initialisation():
  32. LodelContext.init(
  33. LodelContext.MONOSITE if _monosite_test() else LodelContext.MULTISITE)
  34. ##@brief Return confdir name given instance type and context type
  35. #@param ctx_type : see @ref boostrap()
  36. #@return a configuration directory
  37. #@todo return abspath
  38. def _get_confdir(ctx_type):
  39. if _monosite_test():
  40. return CONFS_DIRNAMES[0]
  41. return CONFS_DIRNAMES[1][1]
  42. #elif ctx_type == '__loader__':
  43. # return CONFS_DIRNAMES[1][0]
  44. #elif ctx_type == 'lodelsites':
  45. # return CONFS_DIRNAMES[1][1]
  46. #raise ValueError("ctx_type is not one of '__loader__' nor 'lodelsites' \
  47. #authorized values")
  48. ##@brief Return confspec associated with current context & context type
  49. #@param ctx_type str
  50. #@return None (for default confspecs) or a confspecs dict
  51. def _get_confspec(ctx_type):
  52. if not _monosite_test():
  53. LodelContext.expose_modules(globals(), {
  54. 'lodel.plugins.multisite.confspecs': 'multisite_confspecs'})
  55. return multisite_confspecs.LODEL2_CONFSPECS
  56. return None
  57. ##@brief After calling this function you should use your instance as it
  58. #
  59. #@param ctx_type str : ONLY FOR MULTISITE specify wich multisite context to
  60. #bootstrap. The two choices are '__loader__' and 'lodelsites'. Default is
  61. #__loader__
  62. def bootstrap(ctx_type = None):
  63. _context_initialisation()
  64. monosite = _monosite_test()
  65. if ctx_type is None:
  66. #Default value
  67. if not _monosite_test():
  68. ctx_type = '__loader__'
  69. elif monosite:
  70. raise RuntimeError("Not allowed to give a value for ctx_type in a \
  71. MONOSITE instance")
  72. elif ctx_type not in ['__loader__', 'lodelsites']:
  73. raise ValueError("ctx_type is not one of '__loader__' nor \
  74. 'lodelsites' authorized values")
  75. custom_confspecs = _get_confspec(ctx_type)
  76. confdir = _get_confdir(ctx_type)
  77. if not os.path.isdir(confdir):
  78. warnings.warn("Bootstraping seems to fail : unable to find confdir \
  79. : %s. Attempt to continue using default values" % confdir)
  80. LodelContext.expose_modules(globals(), {
  81. 'lodel.settings.settings': [('Settings', 'settings_loader')],
  82. 'lodel.plugins.multisite.confspecs': 'multisite_confspecs'})
  83. if ctx_type is not None:
  84. settings_loader(confdir, custom_confspecs, True) #Append specs
  85. else:
  86. settings_loader(confdir, custom_confspecs)
  87. del(globals()['settings_loader'])
  88. LodelContext.expose_modules(globals(), {
  89. 'lodel.settings': ['Settings']})