Нема описа
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.

lodel_admin.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #-*- coding: utf-8 -*-
  2. import sys
  3. import os, os.path
  4. import loader
  5. from lodel.settings import Settings
  6. from lodel import logger
  7. ## @brief Utility method to generate python code given an emfile and a
  8. # translator
  9. # @param model_file str : An em file
  10. # @param translator str : a translator name
  11. # @return python code as string
  12. def generate_dyncode(model_file, translator):
  13. from lodel.editorial_model.model import EditorialModel
  14. from lodel.leapi import lefactory
  15. model = EditorialModel.load(translator, filename = model_file)
  16. dyncode = lefactory.dyncode_from_em(model)
  17. return dyncode
  18. ## @brief Utility method to generate a python file representing leapi dyncode
  19. # given an em file and the associated translator name
  20. #
  21. # @param model_file str : An em file
  22. # @param translator str : a translator name
  23. # @param output_filename str : the output file
  24. def create_dyncode(model_file, translator, output_filename):
  25. dyncode = generate_dyncode(model_file, translator)
  26. with open(output_filename, 'w+') as out_fd:
  27. out_fd.write(dyncode)
  28. out_fd.close()
  29. logger.info("Dynamic leapi code written in %s", output_filename)
  30. ## @brief Refresh dynamic leapi code from settings
  31. def refresh_dyncode():
  32. # EditorialModel update/refresh
  33. # TODO
  34. # Dyncode refresh
  35. create_dyncode( Settings.editorialmodel.emfile,
  36. Settings.editorialmodel.emtranslator,
  37. Settings.editorialmodel.dyncode)
  38. def init_all_dbs():
  39. import loader
  40. loader.start()
  41. import leapi_dyncode as dyncode
  42. from lodel.settings.utils import SettingsError
  43. from lodel.leapi.leobject import LeObject
  44. from lodel.plugin import Plugin
  45. from lodel import logger
  46. ds_cls = dict() # EmClass indexed by rw_datasource
  47. for cls in dyncode.dynclasses:
  48. ds = cls._datasource_name
  49. if ds not in ds_cls:
  50. ds_cls[ds] = [cls]
  51. else:
  52. ds_cls[ds].append(cls)
  53. for ds_name in ds_cls:
  54. # Fetching datasource plugin name and datasource connection
  55. # identifier
  56. try:
  57. plugin_name, ds_identifier = LeObject._get_ds_plugin_name(
  58. ds_name, False)
  59. except (NameError, ValueError, RuntimeError):
  60. raise SettingsError("Datasource configuration error")
  61. # Fetching datasource connection option
  62. con_conf=LeObject._get_ds_connection_conf(ds_identifier, plugin_name)
  63. # Fetching migration handler class from plugin
  64. plugin_module = Plugin.get(plugin_name).loader_module()
  65. try:
  66. mh_cls = plugin_module.migration_handler_class()
  67. except NameError as e:
  68. raise RuntimeError("Malformed plugin '%s'. Missing \
  69. migration_handler_class() function in loader file" % ds_name)
  70. #Instanciate the migrationhandler and start db initialisation
  71. if con_conf['read_only'] is True:
  72. raise SettingsError("Trying to instanciate a migration handler \
  73. with a read only datasource")
  74. try:
  75. if 'read_only' in con_conf:
  76. del(con_conf['read_only'])
  77. mh = mh_cls(**con_conf)
  78. except Exception as e:
  79. msg = "Migration failed for datasource %s(%s.%s) at migration \
  80. handler instanciation : %s"
  81. msg %= (ds_name, plugin_name, ds_identifier, e)
  82. raise RuntimeError(msg)
  83. try:
  84. mh.init_db(ds_cls[ds_name])
  85. except Exception as e:
  86. msg = "Migration failed for datasource %s(%s.%s) when running \
  87. init_db method: %s"
  88. msg %= (ds_name, plugin_name, ds_identifier, e)
  89. logger.info("Database initialisation done for %s(%s.%s)" % (
  90. ds_name, plugin_name, ds_identifier))
  91. def list_registered_hooks():
  92. import loader
  93. loader.start()
  94. from lodel.plugin.hooks import LodelHook
  95. hlist = LodelHook.hook_list()
  96. print("Registered hooks are : ")
  97. for name in sorted(hlist.keys()):
  98. print("\t- %s is registered by : " % name)
  99. for reg_hook in hlist[name]:
  100. hook, priority = reg_hook
  101. msg = "\t\t- {modname}.{funname} with priority : {priority}"
  102. msg = msg.format(
  103. modname = hook.__module__,
  104. funname = hook.__name__,
  105. priority = priority)
  106. print(msg)
  107. print("\n")