No Description
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 5.1KB

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